public class HttpHandle extends AsyncTask<String, Void, Void> {
List<NameValuePair> parm;
HttpClient client;
HttpPost post;
String responseBody;
HttpResponse response;
XMLParser parser;
org.w3c.dom.Document doc;
public HttpHandle(String url,int full) {
client = new DefaultHttpClient();
post = new HttpPost(url);
parm = new ArrayList<NameValuePair>(full);
this.responseBody = "";
}
public void addInfo(String key, String value)
{
parm.add(new BasicNameValuePair(key, value));
}
public String getRes()
{
return this.responseBody;
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
try {
post.setEntity(new UrlEncodedFormEntity(parm,HTTP.UTF_8));
response = client.execute(post);
this.responseBody = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute() {
// Call onRefreshComplete when the list has been refreshed.
super.onPostExecute(null);
}
}
我正在使用此课程从我的应用发送帖子请求,但当我尝试在手机上运行时,应用程序崩溃了。
这是我在登录活动中使用的代码:
String user = username.getText().toString();
String pass = password.getText().toString();
HttpHandle handle = new HttpHandle("url",2);
handle.addInfo("username", user);
handle.addInfo("password", pass);
String responseBody = handle.execute();
我找到了很多解决方案,但我没有成功让它发挥作用。 记录错误:
02-08 19:58:00.252:D /之前(4048):从这里开始 02-08 19:58:00.262:D / libc(4048):[NET] getaddrinfo hostname www.raffle.coder.co.il,servname NULL,ai_family 0 02-08 19:58:00.272:D / libc(4048):[NET] getaddrinfo hostname www.raffle.coder.co.il,servname NULL,ai_family 0 02-08 19:58:00.272:I / global(4048):调用createSocket()返回一个新套接字。 02-08 19:58:00.272:D / libc(4048):[NET] getaddrinfo主机名80.179.219.93,servname NULL,ai_family 0
现在当我尝试点击“登录”按钮时,没什么好开心的。
答案 0 :(得分:0)
您不应该重新实现可能与崩溃有关的execute()
方法。然而,回溯的日志将有助于澄清。