-1

I am trying to read the buffer (android application) and set the value to my TextView 'httpStuff'. But i dont think i am getting some response from the URI.

I don't get any runtime errors. I tried many flavour of the same logic. Nothing seems to be working.

INTERNET permission is already set in the manifest. SdkVersion="15". Any help ?

HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.mybringback.com");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity(); 
InputStream is = entity.getContent(); 
BufferedReader in = new BufferedReader(new InputStreamReader(is));

httpStuf.setText( in.readLine());
wattostudios
  • 8,666
  • 13
  • 43
  • 57
user1425108
  • 1
  • 1
  • 1
  • 1
  • I added the below code in Oncreate() and it worked ! since API Level 11, does not allow network operation (include HttpClient and HttpUrlConnection) get executed on UI thread. if you do this, you get NetworkOnMainThreadException. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); – user1425108 May 31 '12 at 02:57

4 Answers4

2

I think you are missing the while loop and also, when you say only in.readLine(), may be it is returning you an empty line from the response, though it is having enough data.So make sure to read the reader entirely like this and check its contents.

while ((line = rd.readLine()) != null) {
    httpStuf.setText(line+"\r\n");
}

Hope this will help you.

UVM
  • 9,776
  • 6
  • 41
  • 66
1

This code worked for me

  InputStream is = response.getEntity().getContent();
  String strResponse = inputStreamToString(is);


private String inputStreamToString(InputStream is)
{

    String line = "";
    StringBuilder total = new StringBuilder();
    // Wrap a BufferedReader around the InputStream
    BufferedReader rd = new BufferedReader(new InputStreamReader(is), 1024 * 4);
    // Read response until the end
    try
    {

        while ((line = rd.readLine()) != null)
        {
            total.append(line);
        }
    } catch (IOException e)
    {
        Log.e(TAG, "error build string" + e.getMessage());
    }
    // Return full string
    return total.toString();
}
ductran
  • 10,043
  • 19
  • 82
  • 165
  • I added the below code in Oncreate() and it worked ! since API Level 11, does not allow network operation (include HttpClient and HttpUrlConnection) get executed on UI thread. if you do this, you get NetworkOnMainThreadException. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); – user1425108 May 31 '12 at 02:57
  • You can use AsyncTask to fix that. Here is an example: http://stackoverflow.com/a/8706638/719212 – ductran May 31 '12 at 03:19
0

try to get the status code of response and Then you can compare with the (HTTP status)

int responseCode=response.getStatusLine().getStatusCode()

Zaz Gmy
  • 4,236
  • 3
  • 19
  • 30
0

I am using this method to simply catch the HTTP response and it works fine for me.

public String httpGetResponse(String url) {
        try {
            Log.i("HTTP Request", "httpGet Request for : " + url);
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            //get.setHeader("Connection", "keep-alive");

            HttpResponse response = client.execute(get);

            InputStream is = response.getEntity().getContent();
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(is));
            StringBuilder str = new StringBuilder();

            String line = null;

            while ((line = bufferedReader.readLine()) != null) {
                str.append(line + "\n");
            }
            return str.toString();
        } catch (Exception e) {
            Log.e("HTTP error", "Error in function httpGetResponse : "
                    + e.getMessage());
            return null;
        }
    }
laurent
  • 88,262
  • 77
  • 290
  • 428
ZAQ
  • 11
  • 2
  • I added the below code in Oncreate() and it worked ! since API Level 11, does not allow network operation (include HttpClient and HttpUrlConnection) get executed on UI thread. if you do this, you get NetworkOnMainThreadException. StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); – user1425108 May 31 '12 at 02:57