繁体   English   中英

当我尝试发送发布请求Android时我的应用程序崩溃

[英]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.

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