gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:34 26 4
gpt4 key购买 nike

我正在关注这个网站 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);
}

}

我试图从该站点下载 Android 应用程序项目 http://uploadingit.com/file/d4632ekfpwjiupyn/GetPhpServerMessageDemo2.zip在我的手机上运行,​​它的工作。但是当我开始一个新项目(Minimuim Requied SDK:API8,目标 SDK:API17,编译:API17)并复制这两个 java 代码时。我添加了权限android.permission.INTERNET,所以我不知道问题在哪里,我只知道当运行HttpResponse response = httpclient.execute(method); 出现错误并返回字符串“网络问题”。

最佳答案

更新:如果您使用的是 kotlin,请使用协程进行线程处理。 asnctask 不再使用。

您正在 ui 线程上运行与网络相关的操作。你将得到 NetworkOnMainThreadException post honeycomb。

使用ThreadAsynctask

调用异步任务

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

异步任务

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

关于java - Android 4.2 错误上的 HttpClient.execute(HttpPost),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19162272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com