Reputation: 676
I am developing an android app which fetch data from internet. When internet connection remains on then the app works fine. But when internet connection connection remains off then it crashes. I have searched on google but I could not my solution . I also added these permissions in Android Menifest file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
My whole codes are below,
public class NoticeBoard extends Activity {
ArrayList<String> title_array = new ArrayList<String>();
ArrayList<String> notice_array = new ArrayList<String>();
ListView list;
base_adapter3 adapter;
TextView f;
TextView msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_notice);
list = (ListView) findViewById(R.id.list_notice2);
msg=(TextView)findViewById(R.id.error_msg);
new test_ays().execute();
}
class test_ays extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String str = null ;
try {
HttpClient httpclient = new DefaultHttpClient();
//HttpPost httppost = new HttpPost("http://10.0.2.2/BSDI/show.php");
HttpPost httppost = new HttpPost("http://logger.net46.net/android_bsdi_tuhin/show.php");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
String response = result.toString();
msg.setText(response);
try {
JSONArray new_array = new JSONArray(response);
for (int i = 0, count = new_array.length(); i < count; i++) {
try {
JSONObject jsonObject = new_array.getJSONObject(i);
title_array.add(jsonObject.getString("title").toString());
notice_array.add(jsonObject.getString("notice").toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter = new base_adapter3(NoticeBoard.this, title_array, notice_array);
list.setAdapter(adapter);
// f=(TextView) findViewById(R.id.textTuh);
// f.setText(title_array);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
My logcat errors are below,
03-02 06:55:28.570: E/AndroidRuntime(788): FATAL EXCEPTION: main
03-02 06:55:28.570: E/AndroidRuntime(788): Process: com.example.bsdiassistant, PID: 788
03-02 06:55:28.570: E/AndroidRuntime(788): java.lang.NullPointerException
03-02 06:55:28.570: E/AndroidRuntime(788): at com.example.bsdiassistant.NoticeBoard$test_ays.onPostExecute(NoticeBoard.java:80)
03-02 06:55:28.570: E/AndroidRuntime(788): at com.example.bsdiassistant.NoticeBoard$test_ays.onPostExecute(NoticeBoard.java:1)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.AsyncTask.finish(AsyncTask.java:632)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.Looper.loop(Looper.java:137)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-02 06:55:28.570: E/AndroidRuntime(788): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 06:55:28.570: E/AndroidRuntime(788): at java.lang.reflect.Method.invoke(Method.java:515)
03-02 06:55:28.570: E/AndroidRuntime(788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-02 06:55:28.570: E/AndroidRuntime(788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-02 06:55:28.570: E/AndroidRuntime(788): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 2
Views: 1987
Reputation: 133560
When there is no internet connection there is no response returned. May be you can cache the response locally if you want to. Once the response is cached you need not make a network request. You can parse the json and display the data.
Check if result is null or not
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result!=null)
{
// do something
}
Upvotes: 1
Reputation: 4522
try this code:
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result!=null) // add this
{
String response = result.toString();
msg.setText(response);
try {
JSONArray new_array = new JSONArray(response);
for (int i = 0, count = new_array.length(); i < count; i++) {
try {
JSONObject jsonObject = new_array.getJSONObject(i);
title_array.add(jsonObject.getString("title").toString());
notice_array.add(jsonObject.getString("notice").toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
adapter = new base_adapter3(NoticeBoard.this, title_array, notice_array);
list.setAdapter(adapter);
// f=(TextView) findViewById(R.id.textTuh);
// f.setText(title_array);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 2
Reputation: 7556
Because you started the AsyncTask
in onCreate
,so once your app started, AsyncTask
will start,which needs the internet connection, you should check the status code of the response before processing it, use
response.getStatusLine().getStatusCode()
Upvotes: 0