0

I have problem with connecting to my server. Following code returns -1 as response code and null as response message.

  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;
}

While debbuging url and body content (which is load in code) are correct. When I try to connect with the server using those params with Fiddler it works perfectly and I get 200 status code. Do you have any ideas what is wrong?

Im passing jData object as the 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();
        }

@EDIT Solution with System.setProperty("http.keepAlive", "false") didnt help.

laik
  • 111
  • 13

1 Answers1

0

I solved the problem. It was in this line:

if(urlConnection.getResponseCode()==200 && urlConnection.getResponseMessage().contains("true") ) return true;

When I changed it to:

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

It started working.

laik
  • 111
  • 13