Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpUrlConnection.getInputStream returns empty stream in Android

I make a GET request to a server using HttpUrlConnection. After connecting:

  1. I get response code: 200
  2. I get response message: OK
  3. I get input stream, no exception thrown but:

    • in a standalone program I get the body of the response, as expected:

    {"name":"my name","birthday":"01/01/1970","id":"100002215110084"}

    • in a android activity, the stream is empty (available() == 0), and thus I can't get any text out.

Any hint or trail to follow? Thanks.

EDIT: here it is the code

Please note: I use import java.net.HttpURLConnection; This is the standard http Java library. I don't want to use any other external library. In fact I did have problems in android using the library httpclient from apache (some of their anonymous .class can't be used by the apk compiler).

Well, the code:

URLConnection theConnection;
theConnection = new URL("www.example.com?query=value").openConnection(); 

theConnection.setRequestProperty("Accept-Charset", "UTF-8");

HttpURLConnection httpConn = (HttpURLConnection) theConnection;


int responseCode = httpConn.getResponseCode();
String responseMessage = httpConn.getResponseMessage();

InputStream is = null;
if (responseCode >= 400) {
    is = httpConn.getErrorStream();
} else {
    is = httpConn.getInputStream();
}


String resp = responseCode + "\n" + responseMessage + "\n>" + Util.streamToString(is) + "<\n";

return resp;

I see:

200
OK
the body of the response

but only

200 OK

in android

like image 247
cibercitizen1 Avatar asked Feb 22 '13 17:02

cibercitizen1


2 Answers

Trying the code of Tomislav I've got the answer.

My function streamToString() used .available() to sense if there is any data received, and it returns 0 in Android. Surely, I called it too soon.

If I rather use readLine():

class Util {
public static String streamToString(InputStream is) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }
}

then, it waits for the data to arrive.

Thanks.

like image 71
cibercitizen1 Avatar answered Nov 10 '22 03:11

cibercitizen1


You can try with this code that will return response in String:

public String ReadHttpResponse(String url){
        StringBuilder sb= new StringBuilder();
        HttpClient client= new DefaultHttpClient();     
        HttpGet httpget = new HttpGet(url);     
        try {
            HttpResponse response = client.execute(httpget);
            StatusLine sl = response.getStatusLine();
            int sc = sl.getStatusCode();
            if (sc==200)
            {
                HttpEntity ent = response.getEntity();
                InputStream inpst = ent.getContent();
                BufferedReader rd= new BufferedReader(new InputStreamReader(inpst));
                String line;
                while ((line=rd.readLine())!=null)
                {
                    sb.append(line);
                }
            }
            else
            {
                Log.e("log_tag","I didn't  get the response!");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
like image 30
Tomislav Avatar answered Nov 10 '22 05:11

Tomislav