1

I have created my server in amazon ec2 instance. Through my android app i am connecting to the server with HttpUrlConnection. But i get response code as -1. Does anyone has any idea ?? Here is my code. private String getHttpResponse(final String srcUrl) {

    HttpURLConnection urlConnection = null;
    FileOutputStream fos = null;
    try {
        URL url = new URL(srcUrl);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setRequestProperty("Content-Type", "application/json");
        mETag = readETagFromPrefForCategory();

        urlConnection.setRequestProperty("If-None-Match", mETag);

        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("xyz", "abc".toCharArray());
            }
        });

        urlConnection.connect();

        mETag =  urlConnection.getHeaderField("ETag");

        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) {
            Log.v("http","not modifed");
            return readLocalJson();
        }

        if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) {
            Log.w(TAG, "Bad response [" + urlConnection.getResponseCode() + "] from (" + srcUrl + ")");
            return null;
        }}//end of try block 
user1479604
  • 117
  • 1
  • 3
  • 12

3 Answers3

0

An answer in this post seemed to solve it for a few people:

Android Https Status code -1

Hope that helps.

Community
  • 1
  • 1
Kieren Hughes
  • 574
  • 3
  • 8
  • Thanks for the reply.. I tried System.setProperty("http.keepAlive", "false"); but sadly it didn't work for me :(.. Any other help is appreciated.. – user1479604 Sep 26 '12 at 06:53
0

The problem might be that your headers HTTP version is not properly formatted. Check this SO link where I have answered a similar question which worked for me.

Java Http~URLConnection response code -1

Community
  • 1
  • 1
Ruraj
  • 467
  • 2
  • 8
  • 13
0

Why are you setting "Content-Type" on a GET request?

Julian Reschke
  • 40,156
  • 8
  • 95
  • 98