Akman Ra
Akman Ra

Reputation: 1

Getting a Status Code of 0 when Calling HttpUrlConnection.getResponseCode() in AsyncTask

        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        con.setRequestProperty("Accept", "application/json");

        OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
        wr.write(data.toString());
        wr.flush();
        status = con.getResponseCode();

This code is part of an AsyncTask class in Android Studio. When I execute a Post request using PostMan, the status returned is 200; however, when I make the request using the android emulator, the status is returned as 0. I'm super confused, and some help would be appreciated.

Upvotes: 0

Views: 691

Answers (1)

ApriOri
ApriOri

Reputation: 2688

Status code 0 means some kind of network error. Try checking for:

  • Internet permission
  • Emulator, Connectivity

Upvotes: 0

Related Questions