[英]My app crashes when I try to send a post request android
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);
}
}
I'm using this class to send a post request from my app, but when I try to run it on my phone, the app crashes. 我正在使用此类从我的应用程序发送发布请求,但是当我尝试在手机上运行它时,应用程序崩溃。
This is the code I'm using in my Login Activity: 这是我在登录活动中使用的代码:
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();
I found a lot of solutions, but I didn't succeed to make it work. 我找到了很多解决方案,但未能成功实现。 Log Error: 记录错误:
02-08 19:58:00.252: D/before(4048): From HERE 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): call createSocket() return a new socket. 02-08 19:58:00.252:D / before(4048):从此处开始02-08 19:58:00.262:D / libc(4048):[NET] getaddrinfo主机名www.raffle.coder.co.il,servname NULL,ai_family 0 02-08 19:58:00.272:D / libc(4048):[NET] getaddrinfo主机名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 hostname 80.179.219.93, servname NULL, ai_family 0 02-08 19:58:00.272:D / libc(4048):[NET] getaddrinfo主机名80.179.219.93,servname为NULL,ai_family 0
Now when I try to click on "login" button nothing happneds. 现在,当我尝试单击“登录”按钮时,没有任何反应。
You shouldn't be reimplementing the execute()
method, that may have something to do with the crash. 您不应该重新实现execute()
方法,这可能与崩溃有关。 A log of the backtrace would help clarify, though. 不过,回溯的日志将有助于澄清。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.