2013-10-03 69 views
3

我正在關注本網站http://givemepass.blogspot.hk/2011/12/http-server.html,嘗試使用android應用程序連接PHP服務器來獲取消息。Android 4.2上的HttpClient.execute(HttpPost)錯誤

GetServerMessage.java

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
public class GetServerMessage { 

    public String stringQuery(String url){ 
     try 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost method = new HttpPost(url); 
      HttpResponse response = httpclient.execute(method); 
      HttpEntity entity = response.getEntity(); 
      if(entity != null){ 
       return EntityUtils.toString(entity); 
      } 
      else{ 
       return "No string."; 
      } 
     } 
     catch(Exception e){ 
      return "Network problem"; 
     } 
    } 
} 

GetPhpServerMessageDemoActivity.java

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class GetPhpServerMessageDemoActivity extends Activity { 
    /** Called when the activity is first created. */ 
    private TextView textView; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     textView = (TextView)findViewById(R.id.text_view); 
     GetServerMessage message = new GetServerMessage(); 
     String msg = message.stringQuery("http://192.168.1.88/androidtesting.php"); 
     textView.setText("Server message is "+msg); 
    } 

} 

我試圖從該網站http://uploadingit.com/file/d4632ekfpwjiupyn/GetPhpServerMessageDemo2.zip下載Android應用程序項目,我的手機上運行,​​它的工作。 但是當我啓動一個新項目(Minimuim Requied SDK:API8,Target SDK:API17,編譯:API17)並複製這兩個java代碼。我已經添加了權限android.permission.INTERNET,所以我不知道問題出在哪裏,我只知道在運行HttpResponse response = httpclient.execute(method);時出現錯誤並返回String「Network problem」。

+0

是吧'NetworkOnMain'? –

回答

4

您正在ui線程上運行網絡相關操作。你會得到NetworkOnMainThreadException貼蜂窩。

使用ThreadAsynctask

調用的AsyncTask

new TheTask().execute("http://192.168.1.88/androidtesting.php"); 

的AsyncTask

http://developer.android.com/reference/android/os/AsyncTask.html

class TheTask extends AsyncTask<String,String,String> 
    { 

@Override 
protected String onPostExecute(Void result) { 
    // TODO Auto-generated method stub 
    super.onPostExecute(result); 
    // update textview here 
    textView.setText("Server message is "+result); 
} 

@Override 
protected void onPreExecute() { 
    // TODO Auto-generated method stub 
    super.onPreExecute(); 
} 

@Override 
protected String doInBackground(String... params) { 
    try 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost method = new HttpPost(params[0]); 
      HttpResponse response = httpclient.execute(method); 
      HttpEntity entity = response.getEntity(); 
      if(entity != null){ 
       return EntityUtils.toString(entity); 
      } 
      else{ 
       return "No string."; 
      } 
     } 
     catch(Exception e){ 
      return "Network problem"; 
     } 

} 
} 

更新的HttpClient在API 23. HttpUrlConnection已被棄用。

http://developer.android.com/reference/java/net/HttpURLConnection.html

1

你需要像Sotirios Delimanolis說的那樣運行長時間運行的任務,如網絡,在單獨的Thread上調用。 AsyncTask可能是要走的路。

相關問題