I want to display an error message when the MYSQL database can not be access due to sudden network disconnection how can i do this. Currently my application crash when this happens
I am using the below class to display the values to a Listview
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
public class ViewActivity extends Activity {
ListView Listview;
String url_get_All ="http://102.128.2.5/ABC/DisplayAll.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
Listview = (ListView) findViewById(R.id.listView);
new GetAll().execute(new APConnect());
View empty = findViewById(R.id.empty);
Listview.setEmptyView(empty);
}
public void setlistArray(JSONArray jsonArray)
{
this.Listview.setAdapter(new ListAdapter(jsonArray,this));
}
class GetAll extends AsyncTask<APConnect, Long, JSONArray>
{
@Override
protected JSONArray doInBackground(APConnect... params) {
return params[0].Getconnect(url_get_All);
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
super.onPostExecute(jsonArray);
setlistArray(jsonArray);
}
}
}
I have used the below Adapter class
public class ListAdapter extends BaseAdapter {
private JSONArray listArray;
private LayoutInflater inflater = null;
Activity cust;
public ListAdapter(JSONArray jsonarray, Activity cust)
{
listArray = jsonarray;
this.cust = cust;
inflater = (LayoutInflater) this.cust.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.listArray.length();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemsinListCall call;
// The view need to be setup
if(convertView == null)
{
convertView = inflater.inflate(R.layout.custodianrow_views,null);
call = new ItemsinListCall();
call.ID = (TextView) convertView.findViewById(R.id.id_view);
call.Name = (TextView) convertView.findViewById(R.id.name_view);
call.Desin = (TextView) convertView.findViewById(R.id.Desig_view);
call.Depar= (TextView) convertView.findViewById(R.id.Depar_view);
convertView.setTag(call);
}
else
{
call = (ItemsinListCall) convertView.getTag();
}
try {
JSONObject jsonObject = this.listArray.getJSONObject(position);
call.ID.setText(jsonObject.getString("id"));
call.Name.setText(jsonObject.getString("name"));
call.Desin.setText(jsonObject.getString("Desin"));
call.Depar.setText(jsonObject.getString("Depar"));
} catch (JSONException e) {
e.printStackTrace();
}
return convertView;
}
private class ItemsinListCall
{
private TextView ID;
private TextView Name;
private TextView Desin;
private TextView Depar;
}
}
And i have used the APconnect to connect to the webserver to access the PHP code
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import java.util.List;
public class APConnect {
public JSONArray Getconnect(String url)
{
HttpEntity httpEntity = null;
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JSONArray jsonArray = null;
try {
String entityResponse = EntityUtils.toString(httpEntity);
Log.e("Entity Responce",entityResponse);
jsonArray = new JSONArray(entityResponse);
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonArray;
}
}
I am getting the below error message
java.lang.NullPointerException
at com.example.nms.ListAdapter.getCount(ListAdapter.java:35)
at android.widget.setlistArray.setAdapter(ListView.java:486)
at com.example.nams.ViewActivity.setlistArray(ViewActivity.java:80)
at com.example.nams.ViewActivity$GetAll.onPostExecute(ViewActivity.java:140)
at com.example.nams.ViewActivity$GetAll.onPostExecute(ViewActivity.java:110)
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:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
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:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
Change this
return this.listArray.length();
To
return listArray==null?0:listArray.length();
And change
public void setlistArray(JSONArray jsonArray)
{
this.Listview.setAdapter(new ListAdapter(jsonArray,this));
}
to
public void setlistArray(JSONArray jsonArray)
{
if(jsonArray == null){
//Show toast error
}else
this.Listview.setAdapter(new ListAdapter(jsonArray,this));
}
This is just my suggestion, you can make it better. You should split your task to many difference classes and one class will handle one task