HttpClient.execute(HttpPost)在Android 4.2的错误 [英] HttpClient.execute(HttpPost) on Android 4.2 error
问题描述
我下面这个网站 http://givemepass.blogspot.hk/2011 /12/http-server.html 尝试使用Android应用程序连接的PHP服务器以获取信息。
I am following this website http://givemepass.blogspot.hk/2011/12/http-server.html to try to use the android application connect the PHP server to get message.
GetServerMessage.java
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
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下载Android应用程序项目/d4632ekfpwjiupyn/GetPhpServerMessageDemo2.zip 并在我的手机上运行,它的工作。
但是,当我开始一个新项目(Minimuim科目编号SDK:API8,目标SDK:API17,与编译:API17)和复制这两个Java codeS。我已经添加了权限 android.permission.INTERNET对
,所以我不知道问题出在哪里,我只知道运行时的Htt presponse响应= httpclient.execute(法);
有一个错误,返回字符串网络问题
I tried to download the Android Application Project from that site http://uploadingit.com/file/d4632ekfpwjiupyn/GetPhpServerMessageDemo2.zip and run on my phone, it's work.
But when I start a new project (Minimuim Requied SDK: API8, Target SDK: API17, Compile with: API17) and copy these two java codes. I've added the permission android.permission.INTERNET
, so I don't know where is the problem, I only know that when run HttpResponse response = httpclient.execute(method);
there is a error and returned String "Network problem".
推荐答案
您是在UI线程上运行的网络相关的操作。您将获得 NetworkOnMainThreadException
后蜂窝。
You are running network related operation on the ui thread. You will get NetworkOnMainThreadException
post honeycomb.
使用一个发
或的AsyncTask
调用的AsyncTask
Invoke asynctask
new TheTask().execute("http://192.168.1.88/androidtesting.php");
的AsyncTask
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.execute(HttpPost)在Android 4.2的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!