Issue
First of all i have seen two problems first second
but they are different ,
i use Async task
to communicate with server side and it works fine except two situations
first one if the server stopped for any reason that caused crashes in my application
second situation is when i am connected to network but there is no internet on this access point
My Question is how can i handle this error to avoid crashes ?
this is my code
public class news_tab extends Fragment{
ListView newsLIST;
String result_news,result_fav;
String [] Hot_news,newsName1,newsText1 ,agentName;
int [] Hot_news_id,agent_id;
int [] fav_id;
private Context context;
ArrayList<String> newsName,newsText,agentNamelist;
public news_tab()
{
}
public news_tab (Context context)
{
this.context=context;
newsName=new ArrayList<String>();
newsText=new ArrayList<String>();
agentNamelist=new ArrayList<String>();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.news, container, false);
rootView.setBackground(Drawable.createFromPath("@drawable/back"));
newsLIST=(ListView)rootView.findViewById(R.id.newsLV);
getFavouriteNews();
news();
return rootView;
}
public void getFavouriteNews() {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
if(isNetworkAvailable()) {
HttpRequestSender http = new HttpRequestSender((Activity) context);
result_fav = http.SEND_POST_get_news_fav("**********/dal/login.asmx/getdata_newsfav");
}
else
{
result_fav="";
Toast.makeText(context,"Check the internet Connection",Toast.LENGTH_SHORT).show();
}
// result = http.SEND_Check("******/Login?email="
// +username.getText().toString()+"&password="+password1.getText().toString());
Log.d("String", result_fav);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
if(result_fav.contains("text_news")) {
// x.cancel();
// Toast.makeText(context, "Welcome Favourite", Toast.LENGTH_SHORT).show();
// JSONObject jb = null;
try {
JSONArray JA = new JSONArray(result_fav);
ArrayList <String> newsName , newsText,agentNamelist,urlList;
fav_id= new int [JA.length()];
for (int i = 0; i < JA.length(); i++) {
JSONObject JO = (JSONObject) JA.get(i);
fav_id[i]= (int) JO.get("news_id");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
else
{
// Toast.makeText(context,"No News",Toast.LENGTH_SHORT).show();
}
super.onPostExecute(aVoid);
}
}.execute();
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
and finally i got this error
03-29 10:15:45.500 24585-24585/com.Offferly.ahmad.hakimi E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.Offferly.ahmad.hakimi, PID: 24585
java.lang.NullPointerException
at com.Offferly.ahmad.hakimi.tab.news_tab$2.onPostExecute(news_tab.java:217)
at com.Offferly.ahmad.hakimi.tab.news_tab$2.onPostExecute(news_tab.java:179)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
03-29 10:15:45.540 2952-20003/? E/WakeLock: release without a matched acquire!
Solution
if the server stopped for any reason that caused you are getting null response in your result_fav variable.. You need to handle null in onPostExecute() method like this
if(!TextUtils.isEmpty(result_fav)){
// your code
}else{
// Show toast Null Response
}
In second situation :
Remove Toast message in doInBackground() method .can not show toast msg in doInBackground() method
You can try this in your getFavouriteNews() method :
if(isNetworkAvailable()) {
//then call your async task
}
else
{
//Show Toast no internet
}
Answered By - Rohit
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.