2014-12-07 60 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