[英]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);
}
}
我正在使用此類從我的應用程序發送發布請求,但是當我嘗試在手機上運行它時,應用程序崩潰。
這是我在登錄活動中使用的代碼:
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 / 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主機名80.179.219.93,servname為NULL,ai_family 0
現在,當我嘗試單擊“登錄”按鈕時,沒有任何反應。
您不應該重新實現execute()
方法,這可能與崩潰有關。 不過,回溯的日志將有助於澄清。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.