简体   繁体   中英

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.272: D/libc(4048): [NET] getaddrinfo hostname 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. A log of the backtrace would help clarify, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM