I have a setup where I perform my http requests in the doInBackground() method of an AsyncTask as follows:
#Override
protected HttpResponse doInBackground(HttpRequestBase... httpRequests)
{
HttpResponse httpResponse = HttpClient.execute(HttpUriRequest);
return httpResponse;
}
This HttpResponse object is then passed on to the onPostExecute() method of my AsyncTask to be passed on to a handler (the original caller of the http request) and processed as necessary, as follows:
checking the response code using httpResponse.getStatusLine().getStatusCode();
getting the response content using EntityUtils.toString(httpResponse.getEntity())).
This setup has been working fine on phones running older versions of Android.
Running my app now on Ice Cream Sandwich (Galaxy Nexus) I find that the first few http requests in my app as above work fine but then there is this one http request which consistently throws an exception with a stack trace as follows (trimmed slightly for readability):
....
at org.apache.http.util.EntityUtils.toString(EntityUtils.java:139)
at java.io.InputStreamReader.close(InputStreamReader.java:145)
at org.apache.http.conn.EofSensorInputStream.close(EofSensorInputStream.java:213)
...
at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:151)
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
android.os.NetworkOnMainThreadException
I am confused. Does this then mean that the EntityUtils.toString(HttpEntity) method is a potential culprit for throwing the new (and ever so annoying) NetworkOnMainThreadException? If so, any advice on reworking my setup to make http requests in a separate thread such that the response can be processed on the main thread?
Please try the following: modify your doInBackground function to return the value of the HTTP response.
protected String doInBackground(HttpRequestBase... httpRequests)
{
HttpResponse httpResponse = HttpClient.execute(HttpUriRequest);
if (httpResponse.getEntity() != null) {
return EntityUtils.toString(httpResponse.getEntity());
}
return "";
}
Related
This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 9 years ago.
I want to check the HTTP response of a certain URL before loading into a webview. I only want to load webview if http response code is 200. This is a workaround for intercepting http errors. I have below:
HttpGet httpRequest = new HttpGet( "http://example.com");
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
int code = response.getStatusLine().getStatusCode();
But I encountered the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo
android.os.NetworkOnMainThreadException
How to fix it? Or any workaround to interept http errors in webview? Thanks
android.os.NetworkOnMainThreadException occurs whenever you try to make long running tasks/process on Main UI Thread directly.
To resolve this issue, cover your webservice call inside AsyncTask. FYI, AsyncTask in android known as Painless Threading which means developer don't need to bother about Thread management. So Go and implement web API call or any long running tasks using AsyncTask, there are plenty of examples available on the web.
Update:
I only want to load webview if http response code is 200.
=> Based on your requirement, I would say include your code inside doInBackground() method and return status code value, Which you can check inside onPostExecute(). Now here you are getting status code value 200/201 then you can load WebView.
class HTTPRequest extends AsyncTask<int, Void, void> {
protected int doInBackground() {
try {
HttpGet httpRequest = new HttpGet( "http://example.com");
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
int code = response.getStatusLine().getStatusCode();
return code;
} catch (Exception e) {
e.printstacktrace();
}
}
protected void onPostExecute(int code) {
// TODO: check this.exception
// retrieve your 'code' here
}
}
You are getting this Exception because you are carrying out a heavy Computation i.e Acessing Network in your case on UI Thread.
You should never do this .
Rather you can move this code to background Java Thread :
Try :
private void doNetworkCompuation()
{
new Thread(new Runnable() {
#Override
public void run() {
HttpGet httpRequest = new HttpGet( "http://example.com");
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httpRequest);
int code = response.getStatusLine().getStatusCode();
}).start();
}
Try executing this code in Async Thread.
You can have a refrence from here:
How to fix android.os.NetworkOnMainThreadException?
You are not allowed to execute network requests on the main thread. You have to use a different thread for making this requests. You should use the AsyncTask, for an example look here.
I've been struggling a bit on sending JSON objects from an application on android to a php file (hosted locally). The php bit is irrelevant to my issue as wireshark isn't recording any activity from my application (emulation in eclipse/ADK) and it's almost certainly to do with my method of sending:
try {
JSONObject json = new JSONObject();
json.put("id", "5");
json.put("time", "3:00");
json.put("date", "03.04.12");
HttpParams httpParams = new BasicHttpParams();
HttpClient client = new DefaultHttpClient(httpParams);
//
//String url = "http://10.0.2.2:8080/sample1/webservice2.php?" +
// "json={\"UserName\":1,\"FullName\":2}";
String url = "http://localhost/datarecieve.php";
HttpPost request = new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes(
"UTF8")));
request.setHeader("json", json.toString());
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
if (entity != null) {
InputStream instream = entity.getContent();
}
} catch (Throwable t) {
Toast.makeText(this, "Request failed: " + t.toString(),
Toast.LENGTH_LONG).show();
}
I've modified this from an example I found, so I'm sure I've taken some perfectly good code and mangled it. I understand the requirement for multi-threading so my application doesn't hang and die, but am unsure about the implementation of it. Would using Asynctask fix this issue, or have I missed something else important?
Thankyou for any help you can provide.
Assuming that you are using emulator to test the code, localhost refers to the emulated environment. If you need to access the php hosted on your computer, you need to use the IP 10.0.2.2 or the LAN IP such as 192.168.1.3. Check Referring to localhost from the emulated environment
You can refer to Keeping Your App Responsive to learn about running your long running operations in an AsyncTask
you should use asynctask or thread, because in higher versions of android it doesn't allow long running task like network operations from ui thread.
here is the link for more description
How can I get the process percentage of a HTTP request?
I've got this code for the request:
HttpClient requestclient = new DefaultHttpClient();
HttpResponse requestresponse = requestclient.execute(new HttpGet("example.com"));
StatusLine requeststatus = requestresponse.getStatusLine();
if(requeststatus.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream requestoutput = new ByteArrayOutputStream();
requestresponse.getEntity().writeTo(requestoutput);
requestoutput.close();
String requestresult = requestoutput.toString();
return requestresult;
} else{.
requestresponse.getEntity().getContent().close();
throw new IOException(requeststatus.getReasonPhrase());
}
Thanks!
HTTP requests should be run either in an IntentService or AsyncTask. That is, always run them in the background.
AsyncTask has methods for displaying the percentage complete: publishProgress() from doInBackground(), and onProgressUpdate() in the main Activity.
An IntentService can show progress by broadcast Intents back to the Activity, or by running a progressBar, or (in JellyBean and later) by using a progress notification.
I've got a class:
public class WebReader implements IWebReader {
HttpClient client;
public WebReader() {
client = new DefaultHttpClient();
}
public WebReader(HttpClient httpClient) {
client = httpClient;
}
/**
* Reads the web resource at the specified path with the params given.
* #param path Path of the resource to be read.
* #param params Parameters needed to be transferred to the server using POST method.
* #param compression If it's needed to use compression. Default is <b>true</b>.
* #return <p>Returns the string got from the server. If there was an error downloading file,
* an empty string is returned, the information about the error is written to the log file.</p>
*/
public String readWebResource(String path, ArrayList<BasicNameValuePair> params, Boolean compression) {
HttpPost httpPost = new HttpPost(path);
String result = "";
if (compression)
httpPost.addHeader("Accept-Encoding", "gzip");
if (params.size() > 0){
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
try {
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
if (entity.getContentEncoding() != null
&& "gzip".equalsIgnoreCase(entity.getContentEncoding()
.getValue()))
result = uncompressInputStream(content);
else
result = convertStreamToString(content);
} else {
Log.e(MyApp.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private String uncompressInputStream(InputStream inputStream)
throws IOException {...}
private String convertStreamToString(InputStream is) {...}
}
I cannot find a way to test it using a standard framework. Especially, I need to simulate total internet lost from inside the test.
There are suggestions to manually turn the Internet in the emulator off while performing the test. But it seems to me as not quite a good solution, because the automatic tests should be... automatic.
I added a "client" field to the class trying to mock it from inside the test class. But implementation of the HttpClient interface seems quite complex.
The Robolectric framework allows the developers to test Http connection as far as I know. But I guess there is some way to write such a test without using so big additional framework.
So are there any short and straightforward ways of unit testing classes that use HttpClient? How did you solve this in your projects?
I added a "client" field to the class trying to mock it from inside the test class. But implementation of the HttpClient interface seems quite complex.
I am a little bit confuse about this statement. From the question title, you are asking about unit-testing httpClint, by mocking a FakeHttpClient may help you unit-testing other part of app except httpClient, but doesn't help anything for unit-testing httpClient. What you need is a FakeHttpLayer for unit-testing httpClient (no remote server, network requires, hence unit-testing).
HttpClient Dummy Test:
If you only need examine app behavior in the situation that internet is lost, then a classic Android Instrument Test is sufficient, you can programmatically turn the Internet in the emulator off while performing the test:
public void testWhenInternetOK() {
... ...
webReader.readWebResource();
// expect HTTP 200 response.
... ...
}
public void testWhenInternetLost() {
... ...
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
webReader.readWebResource();
// expect no HTTP response.
... ...
}
This requires the remote http server is completely setup and in a working state, and whenever you run your test class, a real http communication is made over network and hit on http server.
HttpClient Advanced Test:
If you want to test app behavior more precisely, for instance, you want to test a http call in you app to see if it is handle different http response properly. the Robolectric is the best choice. You can use FakeHttpLayer and mock the http request and response to whatever you like.
public void setup() {
String url = "http://...";
// First http request fired in test, mock a HTTP 200 response (ContentType: application/json)
HttpResponse response1 = new DefaultHttpResponseFactory().newHttpResponse(HttpVersion.HTTP_1_1, 200, null);
BasicHttpEntity entity1 = new BasicHttpEntity();
entity1.setContentType("application/json");
response1.setEntity(entity1);
// Second http request fired in test, mock a HTTP 404 response (ContentType: text/html)
HttpResponse response2 = new DefaultHttpResponseFactory().newHttpResponse(HttpVersion.HTTP_1_1, 404, null);
BasicHttpEntity entity2 = new BasicHttpEntity();
entity2.setContentType("text/html");
response2.setEntity(entity2);
List<HttpResponse> responses = new ArrayList<HttpResponse>();
responses.add(response1);
responses.add(response2);
Robolectric.addHttpResponseRule(new FakeHttpLayer.UriRequestMatcher("POST", url), responses);
}
public void testFoo() {
... ...
webReader.readWebResource(); // <- a call that perform a http post request to url.
// expect HTTP 200 response.
... ...
}
public void testBar() {
... ...
webReader.readWebResource(); // <- a call that perform a http post request to url.
// expect HTTP 404 response.
... ...
}
Some pros of using Robolectric are:
Purely JUnit test, no instrument test so don't need start emulator (or real device) to run the test, increase development speed.
Latest Robolectric support single line of code to enable/disable FakeHttpLayer, where you can set http request to be interpreted by FakeHttpLayer (no real http call over network), or set the http request bypass the FakeHttpLayer(perform real http call over network). Check out this SO question for more details.
If you check out the source of Robolectric, you can see it is quite complex to implement a FakeHtppLayer properly by yourself. I would recommend to use the existing test framework instead of implementing your own API.
Hope this helps.
I created an NTLM authenticating SOAP client based on KSOAP-Android and JCIFS. The implementation looks something like this:
public class NtlmServiceConnection implements ServiceConnection
{
public NtlmServiceConnection(final SoapConnectionInfo connectionInfo, String path)
{
httpclient = new DefaultHttpClient();
httpclient.getAuthSchemes().register(AuthPolicy.NTLM, new NTLMSchemeFactory());
//...
#Override
public InputStream openInputStream() throws IOException {
ByteArrayEntity re = new ByteArrayEntity(bufferStream.toByteArray());
post.removeHeaders("CONTENT-LENGTH");
post.setEntity(re);
HttpResponse rep = httpclient.execute(post);
InputStream stream = rep.getEntity().getContent();
return stream;
}
//....
}
From the looks of it KSOAP is generating the correct message because bufferStream is populated with the SOAP envelope as expected. JCIFS seems to be doing its job as well as I can see the NTLM challenge response taking place via Wireshark. The issue is that the message body is missing. It is simply null. Due to this the web service encounters a 501 and the InputStream returned is null.
Anyone have a clue why this would happen?
Note: I'm removing the CONTENT-LENGTH header below because setEntity apparently tries to set this but KSOAP has already set it. I simply remove it and allow setEntity to reset it.
I finally figured it out and blogged about it here: http://csharpening.net/blog/?p=271