我没有从连接中获得响应代码

时间:2019-10-01 16:38:23

标签: android android-asynctask httpurlconnection

我正在使用AsyncTask连接到服务器。我关注了该帖子:Http Get using Android HttpURLConnection

但是我总是得到一个IOException(返回xxx -33)。我究竟做错了什么?要查看apache日志,我看到从未收到过POST请求。

 public abstract class SenderHttp extends AsyncTask<String, Void, String> {

@Override
protected String doInBackground(String... params) {
    Log.i("TAG", params[0]);
    Log.i("TAG", params[1]);

    String data = "";
    HttpURLConnection httpURLConnection = null;
    int responseCode=-33;
    try {
        httpURLConnection = (HttpURLConnection) new URL(params[0]).openConnection();

        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setConnectTimeout(10000);
        httpURLConnection.setReadTimeout(10000);

        httpURLConnection.connect();
        responseCode = httpURLConnection.getResponseCode();

    } catch (IOException e) {
        e.printStackTrace();
        return "xxx" + " "+ responseCode;
    }
    return " "+ responseCode;
}

0 个答案:

没有答案