Java HttpUrlConnection返回-1状态代码

Java HttpUrlConnection返回-1状态代码,java,android,httpurlconnection,Java,Android,Httpurlconnection,我无法连接到服务器。下面的代码返回-1作为响应代码,返回null作为响应消息 protected Boolean doInBackground(MyTaskParams... params) { URL url = null; String load = params[0].jobj.toString(); try { url = new URL(params[0].serverUrl); } catch (MalformedURLExcept

我无法连接到服务器。下面的代码返回-1作为响应代码,返回null作为响应消息

  protected Boolean doInBackground(MyTaskParams... params) {
    URL url = null;
    String load = params[0].jobj.toString();
    try {
        url = new URL(params[0].serverUrl);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    try {
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setConnectTimeout(3000);
        urlConnection.setReadTimeout(3000);
        urlConnection.setRequestMethod("POST");
        urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        urlConnection.setChunkedStreamingMode(0);
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.connect();

        OutputStreamWriter osw = new OutputStreamWriter(urlConnection.getOutputStream());
        osw.write(load);
        osw.flush();
        osw.close();

        if(urlConnection.getResponseCode()==200 && urlConnection.getResponseMessage().contains("true") ) return true;
        else return false;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}
而去bug url和主体内容(在代码中加载)是正确的。当我尝试使用Fiddler的那些参数连接服务器时,它工作得非常好,我得到了200个状态码。你知道怎么回事吗

Im将jData对象作为jobj传递:

        JSONObject User=new JSONObject();
        JSONObject Mobile=new JSONObject();
        JSONObject jData=new JSONObject();
        try {
            User.put ("UserLogin", "test");
            User.put ("UserPassword", "test");
            Mobile.put ("MobileIDD", IDD);
            Mobile.put("MobileToken", token);
            jData.put("Mobile", Mobile);
            jData.put("User", User);
        } catch (JSONException e) {
            e.printStackTrace();
        }
@编辑
使用
System.setProperty(“http.keepAlive”、“false”)的解决方案没有帮助。

我解决了这个问题。是这样的:

if(urlConnection.getResponseCode()==200 && urlConnection.getResponseMessage().contains("true") ) return true;
当我把它改成:

int code = urlConnection.getResponseCode();
if (code==200.....)

它开始工作。

在确定响应代码、respinse消息和返回页面的位置添加代码。显示
load
类型。也许你应该显示
load
内容。我编辑了第一篇文章。谢谢,请按正确的顺序将所有代码放在一个代码块中。您尚未显示读取返回页面的代码。完成。我正在osw.close()之后检查响应消息的状态代码。您是否说urlConnection.getResponseMessage()返回null?如果是这样,那么.contains()调用将引发NullPointerException。