Header Ads Widget

Ticker

10/recent/ticker-posts

Android Studio app show the error message to user


Guys I some one had this  problem, It is in implement error handling in my application. I want to display the error message to the user of my application when the response code from rest is not 200. In other words: If the connection is wrong, I want to display the message, that the user have to check his internet connection and try again. If everything is fine I want to do everything as usual so load the content.

I write something like this:





Toast errorToast = Toast.makeText(NewsActivity.this, "Error, pls chech your internet connection and try again!", Toast.LENGTH_SHORT);
errorToast.show();

and this:

if(response.getStatusLine().getStatusCode() == 200){}

But i don't know If this is good code and where should I insert it. I will be very grateful for your help and advice.







public class NewsActivity extends Activity {
private static final String URL = "http://10.0.2.2:8083/rest/aktualnosci";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
new FetchItems().execute();

}

private class FetchItems extends AsyncTask {
protected JSONArray doInBackground(String... params) {

HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet(URL);
httpget.setHeader("Content-type", "application/json");

JSONArray json = new JSONArray();

try {

HttpResponse response = httpclient.execute(httpget);

json = new JSONArray(EntityUtils.toString(response.getEntity()));

return json;

}

} catch (Exception e) {

Log.v("Błędne wczytanie", e.getMessage());

}

return json;
}

protected void onPostExecute(JSONArray result) {

ListView lst = (ListView) findViewById(R.id.aktualnosci_list);

ArrayList listItems = new ArrayList();

String contentToEdit;

String titleContainer;

TextView newsHeaderTextView = null;

for (int i = 0; i < result.length(); i++) {

try {

titleContainer = result.getJSONObject(i).getString("title").toString();

listItems.add(titleContainer);

contentToEdit=result.getJSONObject(i).getString("body").toString();

contentToEdit= Html.fromHtml(contentToEdit).toString();

listItems.add(contentToEdit);
} catch (Exception e) {
Log.v("Błędne wczytanie1", e.getMessage());
}
}

ArrayAdapter ad = new ArrayAdapter(NewsActivity.this, android.R.layout.simple_list_item_1, listItems);

lst.setAdapter(ad);

}
}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;
}

}






   java android-studio

 If you want to check the network connection call that Toast before calling your AsyncTask. And If you want to show the Toast based on the response from the server call it in onPostExecute. –   

you want to check internet connection..??? 

You can add this in doInBackground method.





 runOnUiThread(new Runnable() {

public void run() {

if (response.getStatusLine().getStatusCode() != 200) {

Toast errorToast = Toast.makeText(NewsActivity.this, "Error, pls chech your internet connection and try again!", Toast.LENGTH_SHORT);

errorToast.show();

}
}
});








 I think affecting the UI thread using Runnable is a bad practice.

 You can not show Toast message in doInBackground method without UI Thread. – 

 I can't add it in doInBackground class because response is not "visible". 





/* I thought Your Code Working Fine Made Some Changes As Per Need */

public class NewsActivity extends Activity {

private static final String URL = "http://10.0.2.2:8083/rest/aktualnosci";

String jsonArrayString = "";

String message = "Error, pls check your internet connection and try again!";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_news);

new FetchItems().execute(this);

}

private class FetchItems extends AsyncTask{

Context temp;

@Override

protected String doInBackground(Context... params) {

temp = params[0];

HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet(URL);

httpget.setHeader("Content-type", "application/json");

JSONArray json = new JSONArray();

try {
HttpResponse response = httpclient.execute(httpget);

if(response.getStatusLine().getStatusCode() == 200) {

json = new JSONArray(EntityUtils.toString(response.getEntity()));

jsonArrayString += json.toString();

return jsonArrayString;
}

}

catch (Exception e) {

Log.v("Błędne wczytanie", e.getMessage());

}

return message;

}

@Override

protected void onPostExecute(String s) {

ListView lst = (ListView) findViewById(R.id.aktualnosci_list);

ArrayList listItems = new ArrayList();

String contentToEdit;

String titleContainer;

TextView newsHeaderTextView = null;

if(!message.equals(s))

{
JSONArray result = null;

try {

result = new JSONArray(s);

} catch (JSONException e) {

e.printStackTrace();

}

for (int i = 0; i < result.length(); i++) {

try {

titleContainer = result.getJSONObject(i).getString("title").toString();

listItems.add(titleContainer);

contentToEdit=result.getJSONObject(i).getString("body").toString();

contentToEdit= Html.fromHtml(contentToEdit).toString();

listItems.add(contentToEdit);



} catch (Exception e) {

Log.v("Błędne wczytanie1", e.getMessage());
}
}

ArrayAdapter ad = new ArrayAdapter(NewsActivity.this, android.R.layout.simple_list_item_1, listItems);

lst.setAdapter(ad);

}

else

{

Toast.makeText(temp,message,Toast.LENGTH_LONG).show();

}

}
}

}