I have the following code for to perform xml download via asynctask for android application targeting for android version>3. The code work pretty good if the network/internet connection is good. However, if internet connection is not good, the application will force close. I have tried throw in different kind of error catching but still unable to solve the force close on lowsy internet connection.
Anyone has any suggestion that I can try
private class DownloadWebPageXML extends AsyncTask<String, Void, InputStream> {
#Override
protected InputStream doInBackground(String... urls) {
Log.d("mylogitem", "AsyncTask started!");
InputStream content = null;
String myurl = urls[0];
AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
HttpGet httpGet = new HttpGet(myurl);
try {
HttpResponse execute = client.execute(httpGet);
content = execute.getEntity().getContent();
} catch (Exception e) {
xmldownloaderror = e.getMessage();
Log.d("mylogitem", e.getMessage());
} finally {
Log.d("mylogitem", "Closing AndroidHttpClient");
client.close();
}
return content;
}
#Override
protected void onPostExecute(InputStream result) {
//do xml reader on inputstream
}
}
add a null check on variable execute, in between these two lines
HttpResponse execute = client.execute(httpGet);
if(execute == null){ return null;} // null check to see if execute is null
content = execute.getEntity().getContent();
another thing in onPostExecute, first line should check if InputStream result is null!
#Override
protected void onPostExecute(InputStream result) {
if(result == null){
Log.d("TEMP_LOG",Content is null);
return;
}
//do xml reader on inputstream
}
check and post your findings
hmm... I recommend to set connection times.
HttpClient client = new DefaultHttpClient();
HttpResponse response;
BufferedReader bufferedReader = null;
HttpParams params = client.getParams();
HttpConnectionParams.setConnectionTimeout(params, 20000);
HttpConnectionParams.setSoTimeout(params, 20000);
I have found the root cause. It is not in the dobackground.
In my case, lousy connection will sometime return not xml data type but rather loading error,
and this is passed as the inputstream to my xmlparser in postexecute.
I did not put in much error catcher in my xmlparser. xmlparser is expecting xml document but received non-xml content, thus throwing null in which i did not cover with error catcher.
Thank you for the suggestion. I have place it in my code as well.
Related
I'm trying to obtain an JSON answer type but is to big and at 50 MB Android Studio throw new Exception OutOfMemory
class MyClass extends AsyncTask<Void, Void, Void>
{
String result="";
#Override
protected Void doInBackground(Void... params){
HttpClient httpClient=new DefaultHttpClient();
String URL="http://82.79.121.114:1001/api/search/category/3,1,1";
try{
HttpGet httpGet = new HttpGet(URL);
httpGet.setHeader("Authorization", "Bearer " + AccesToken);
HttpResponse httpResponse=httpClient.execute(httpGet);
//Log.e("EROARE!!!!!!","EROARE!!!!!");
HttpEntity httpEntity=httpResponse.getEntity();
InputStream is=httpEntity.getContent();
result=convert(is);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid){
Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show();
if(result.length() == 0 || result == null)
{
Toast.makeText(MainActivity.this,result.Toast.LENGTH_SHORT).show();
if(result.length() == 0 || result == null)
{
Toast.makeText(MainActivity.this,"Nu merge!!!",Toast.LENGTH_SHORT).show();
}
}
public String convert(InputStream is) throws IOException {
BufferedReader reader = null;
StringBuffer buffer = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8192);
int read;
char[] chars = new char[1024];
while ((read=reader.read(chars)) != -1)
buffer.append(chars, 0, read);
}
finally {
if (reader != null)
reader.close();
}
return buffer.toString();
}
Your JSON object is simply too large, as most of the devices do not have such big heap. If you own the server-side, you should change the response you send to the clients and make it few separate responses, handled in a sequence.
In addition, I recommend you to rethink why you send so much data at once. It will take a very long time to load even on an average internet connection.
If indeed the OutOfMemory problem is related with the size of the data returned by your REST service then you are doing it wrong both on server and client side. A mobile application should care about users data traffic and also about their battery so instead of loading the entire JSON in one shot maybe you can split it in pages and load only the first page first. Once the user is interested in more (maybe you are using a ListView there to show those categories) then you load the next page and so on. Please see the Endless List Patter for Android Here:
https://github.com/codepath/android_guides/wiki/Endless-Scrolling-with-AdapterViews
Some quick background. We have multiple devices running a scanner app which checks against a database to see whether an id has been scanned in or not. I can scan in with Device A as many times as I like without issue. I then pick up Device B and scan in, also as many or few times as I like. If I pick Device A back up and scan, the HttpClient will hang for approximately 60 seconds refusing to send any further requests. The code below has commented the point of failure.
// Asynchronous get request
private class aGETRequest extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
client.setKeepAliveStrategy(null);
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse httpResponse = client.execute(httpGet); //Hangs Here
HttpEntity httpEntity = httpResponse.getEntity();
InputStreamReader isr = new InputStreamReader(httpEntity.getContent());
BufferedReader buffer = new BufferedReader(isr);
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
httpEntity.consumeContent();
isr.close();
} catch (Exception e) {
e.printStackTrace();
}
httpGet.abort();
client.getConnectionManager().shutdown();
}
return response;
}
#Override
protected void onPostExecute(String result) {
results(result);
}
}
The client hangs and even snooping traffic shows no requests sent at all from Device A after the failure. You ready for the best part? If the devices are going through a Proxy server, it works. W.T.F?
Android is java 6 compat . right.
BufferedReader on java 7 makes me nervous and the while read loop appears to be whats hanging.....
I would try a different different read loop class thats solid on java 6 or i would find someone else's pattern for httpclient that's solid.
My wild guess is that your code is never getting out of the following...
while ((s = buffer.readLine()) != null)
Maybe the server is returing chunked encoding or something like that with a diff protocol ( pattern of length=0 followed by \r\n or something.
I tested this code and it works fine on 2.2 and 2.3.3, but it crashes on 4.0.
The problem seems to be with the http request. Any ideas why?
public class Rezultat extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//http post
InputStream is=null;
try{
String url="http://google.com";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
e.printstacktrace() will tell you:
http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html
The exception that is thrown when an application attempts to perform a
networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or
higher. Applications targeting earlier SDK versions are allowed to do
networking on their main event loop threads, but it's heavily
discouraged. See the document Designing for Responsiveness.
private class DownloadFromUrlTask extends AsyncTask<String, Void, Bitmap> {
protected void onPreExecute() {
mDialog = ProgressDialog.show(ChartActivity.this,"Please wait...", "Retrieving data ...", true);
}
protected String doInBackground(String... urls) {
//All your network stuff here.
return result
}
}
You're performing a (potentially slow) network operation on the main thread. If your target SDK is 11 or higher this will throw a NetworkOnMainThreadException , because this behaviour can block the UI and lead to an unresponsive app.
You could use an AsyncTask to get around this, loading the data in its doInBackground(..).
You should normally post your stack trace when asking about a crash but in this case you're doing network operations on the main UI thread. This is not recommended and now throws an exception. Instead do it in another thread, perhaps via an AsyncTask.
I've been trying to figure this out on my own for quite a while.. by trial/error as well as research, but I just can't seem to get it figured out. I'm pretty bad with Async and network connections and stuch, so it might be something simple that I'm over looking. Anyway... I'll paste some relevant code samples and explanations.
Quick background of my problem. I'm working with the Rotten Tomatoes API for my app, and am using GSON for the parsing of their data. I was initially targeting 2.3, and this worked fine. Then I decided to have support for ICS, and of course ran into the "no network operation on the UI thread" - so I started to delve into AsyncTask.
Here is my InputStream method:
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
Which was working fine in my main activity, and now is giving me issues when trying to 'convert' it into AsyncTask. I've been calling it like this:
InputStream source = retrieveStream( url parameter );
Then I tried moving that method into my AsyncTask class, and calling it like this:
private PerformMovieSearch performSearch = new PerformMovieSearch(this);
InputStream source = performSearch.retrieveStream(movieQueryUrl);
But that doesn't cut it, still get the error about performing network actions on the UI. What I need to figure out is how to call 'retrieveStream' from the AsyncTask I guess. Currently that class looks like this:
package net.neonlotus.ucritic;
[imports]
public class PerformMovieSearch extends AsyncTask<String, Void, String> {
private final Context context;
private ProgressDialog progressDialog;
public PerformMovieSearch(Context context){
this.context = context;
}
#Override
protected String doInBackground(String... urls) {
retrieveStream(urls[0]);
return null;
}
#Override
protected void onPreExecute() {
progressDialog= ProgressDialog.show(context, "Please Wait","Searching movies", true);
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progressDialog.dismiss();
MyActivity.mListAdapter.notifyDataSetChanged();
}
public InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
} catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
}
The "doinbackground" is what needs to be changed... but I can't seem to find a straight way to get that working properly. I was executing using
new PerformMovieSearch(this).execute(movieQueryUrl);
I know that is a lot of stuff, potentially confusing... but if anybody knows how to essentially do the retrieveStream method asynchronously, that would be great. Like I said, Ive tried many things, did plenty of research, just could not come up with anything useful.
the point is, you didn't understand how asynctask works!
You MUST read the guide Processes and Threads: http://developer.android.com/guide/components/processes-and-threads.html
But ok, let me try help you.
On doInBackground you are correctly calling the method retrieveStream, but you are doing nothing with the stream. So, you have to process the stream and then, return it. As you said you are expecting an JSON, I'm assuming you will receive a String, so the code of your retrieveStream should like this:
public String retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
String jsonString = EntityUtils.toString(getResponseEntity);
return jsonString;
} catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
Look that I changed the return type to String. And maybe, you should change the name to retrieveMoviesJSON or something like this.
And you should change your AsyncTask to something like this:
class PerformMovieSearch AsyncTask<String, Void, ArrayList<Movie>>() {
#Override
protected void onPreExecute() {
progressDialog= ProgressDialog.show(context, "Please Wait","Searching movies", true);
}
#Override
protected ArrayList<Movie> doInBackground(String... params) {
String moviesJson = retrieveStream[params[0]];
JSONObject moviesJson = new JSONObject(moviesJson);
ArrayList<Movie> movies = new ArrayList<Movie>();
/*
* Do your code to process the JSON and create an ArrayList of films.
* It's just a suggestion how to store the data.
*/
return movies;
}
protected void onPostExecute(ArrayList<Movie> result) {
progressDialog.dismiss();
//create a method to set an ArrayList in your adapter and set it here.
MyActivity.mListAdapter.setMovies(result);
MyActivity.mListAdapter.notifyDataSetChanged();
}
}
And you can call as the same way you were doing.
Is it clear? Need more explanation?
[]s
Neto
What sort of behavior are you seeing that is unexpected. From scanning your code, it looks like it probably compiles and runs but I would guess that your ListAdapter never gets updated with fresh data (i.e. you're probably trying to display the results in a ListView or GridView but nothing is showing up). Is that correct? Or are you still getting the Network on Main Thread error?
You are retrieving data using your HTTP Client and then not doing anything with it. One way you could solve it is to structure your code such that:
1) Your class that extends AsyncTask has a constructor that takes a ListAdapter object
2) Your main Activity would create an instance of the AsyncTask and pass in a reference to its ListAdapter object
3) Your doInBackground method would handle all the network activity and return the result (the data you pulled from the web service) so that it gets passed down to the onPostExecute method
4) In onPostExecute, you will have the data that was returned from doInBackground, and you'll have the ListAdapter that was provided in the constructor, so parse the data, populate the ListAdapter, and invalidate it so that the list gets redrawn.
Keep in mind that AsyncTask lets you interact with the UI thread in both the onPreExecute and onPostExecute methods, so those are the only places that you can draw to the screen (i.e. populating the adapter and invalidating it so that it will redraw)
I am relatively a new Android developer and I am not able to understand how to do this. I have been looking through all the forums, I made some advance but still here I am.
So, what I want to do is a common function that send a POST request to a webpage (it only sends one POST argument) and returns the result as a string.
I have the main thread here
public class AppActivity extends Activity {
HTTPPostData PostData = new HTTPPostData("id");
PostData.execute();
txtLabel.setText(PostData.Result);
}
and I have my HTTPPostData asynchronous class
public class HTTPPostData extends AsyncTask<String, Long, Object> {
String Value = null;
String Result = null;
public HTTPPostData(String query) {
Value = query;
}
#Override
protected String doInBackground(String... params) {
byte[] Bresult = null;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.mypage.com/script.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("cmd", Value));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
Bresult = EntityUtils.toByteArray(response.getEntity());
Result = new String(Bresult, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
}
return Result;
}
}
I want to use this function several times (inside the same Activity or share it with other Activities of the same application). I am a little bit messed up at this moment so I need your help. What I understand is that I am asking for the result before the doInBackground() is done, and I get an empty result.
Thanks in advance for your help
Regarding this:
HTTPPostData PostData = new HTTPPostData("id");
PostData.execute();
txtLabel.setText(PostData.Result);
Your problem is that you're treating asynctask like it's just a regular function. It's good that you move webpage loading off the main thread, but if you depend on the result for the very next instruction, then it's not doing you much good, you're still blocking the main program waiting for the result. You need to think of AsyncTask like a 'fire and forget' operation, in which you don't know when, if ever, it will come back.
The better thing to do here would be something like:
HTTPPostData PostData = new HTTPPostData("id");
PostData.execute();
txtLabel.setText("Loading...");
and then in the asynctask:
protected void onPostExecute(String result) {
txtLabel.setText(result);
}
This lets your main thread get on with doing it's business without knowing the result of the asynctask, and then as soon as the data is available the asynctask will populate the text label with the result.