Android 4.2上的HttpClient.execute(HttpPost)错误

时间:2013-10-03 14:47:41

标签: java android

我正在关注这个网站http://givemepass.blogspot.hk/2011/12/http-server.html尝试使用android应用程序连接PHP服务器来获取消息。

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,Compile with:API17)并复制这两个java代码时。我添加了权限android.permission.INTERNET,所以我不知道问题出在哪里,我只知道在运行HttpResponse response = httpclient.execute(method);时出现错误并返回字符串“网络问题”。

2 个答案:

答案 0 :(得分: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";
         }

}
}

在api 23中不推荐使用更新HttpClient。使用HttpUrlConnection

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

答案 1 :(得分:1)

如Sotirios Delimanolis所说,你需要在单独的Thread上运行长时间运行的任务,如网络,呼叫。 AsyncTask可能是要走的路。

相关问题