2014-12-07 28 views
-1

我有这个代码的问题,我必须做一个连接到一个php文件的帖子。我必须插入一个线程吗?如果是,为什么?哪里?http post android不返回响应字符串

public class Login extends Activity { 

    public String RispostaLogin; 
    public String response,responseBody; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 

     Button pulsante = (Button) findViewById(R.id.button2); 
     pulsante.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 


       Log.d("myapp", "cliccato"); 
       sendPostRequest(); 


      } 
     }); 


    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_login, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public void sendPostRequest() 
    { 


     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("https://api.uniparthenope.it/user/radius/auth"); 

     try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("user", "user")); 
      nameValuePairs.add(new BasicNameValuePair("passw", "pass")); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

      responseBody=EntityUtils.toString(response.getEntity()); 


      Log.d("myapp", responseBody); 



     } catch (ClientProtocolException e1) { 
      e1.printStackTrace(); 
     } catch (UnsupportedEncodingException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 



    } 
} 
+0

试试这个教程 http://hmkcode.com/android-internet-连接使用http-get-httpclient/ – Hasan 2014-12-07 18:31:41

+0

可能的重复[android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – 323go 2014-12-07 18:32:34

+0

*如果*你记得添加互联网权限,你肯定会崩溃'NetworkOnMainThreadException.'查看链接的问题。 – 323go 2014-12-07 18:33:21

回答

-1

让我们点几件事情:

的Android防止你在主线程上做一个HttpConnection的。你可以打破这个规则,但这是不实际的,因为用户界面会冻结,用户无法与你的应用进行交互。直到收到答复。

要你需要通过添加到您的清单文件,以获取Internet权限是HttpConnection:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 

为了防止使用HttpConnection的过程中不被冻结运行在后台线程的UI AsyncTaskThread

我创建了一个消耗php文件的例子!在github。它是功能和测试在Android手机上。它返回:

{ 
    "result": false 
} 

只需在主活动类中正确设置用户名和密码即可。

要允许在主线程上的HTTP连接下面这些行添加到您的onCreate方法在你的活动:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

StrictMode.setThreadPolicy(policy); 
+0

为什么有人会投票呢? – hasan83 2014-12-08 08:20:35

+0

因为你应该开始提及使用一个线程或asynctask我认为。你不这么认为吗? – greenapps 2014-12-09 14:41:21

+0

没错。它在我创建的github项目中。但我忘了提及它。我现在会。 – hasan83 2014-12-09 14:43:17