Skip to content Skip to sidebar Skip to footer

How To Use Httpget Request In Android?

I am using this code, but it catches an error in a try-catch. Where is a mistake? When I print a error, content.setText(ex.getMessage()); is empty. Here is the relevant code: publi

Solution 1:

I think you get NetworkOnMainThreadException. You should perform network communication on background thread. Consider using IntentService or AsyncTask.

Solution 2:

I have same opinion with Nickolai.

Work around:

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicypolicy=newStrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Use code above, only when you are writing code for yourself.

If your application are produced to others,you need a better way.

Better way:

You should use a handle to access http-request, more info in developer.android.com

Solution 3:

I use this myself:

ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
String responseString = out.toString();

Post a Comment for "How To Use Httpget Request In Android?"