如何实现ConnectionException-Android
我正在与php mysql travez中的一个数据库通信,以便在ListView中显示结果,但我正在尝试为3G或WIFI但应用程序无法连接的情况实现一个ExceptionConnection,返回到以前的活动并显示Toast。但不是如何在代码中实现它,我只能实现JsonExeption。 这是我的代码:如何实现ConnectionException-Android,android,json,return,connectionexception,Android,Json,Return,Connectionexception,我正在与php mysql travez中的一个数据库通信,以便在ListView中显示结果,但我正在尝试为3G或WIFI但应用程序无法连接的情况实现一个ExceptionConnection,返回到以前的活动并显示Toast。但不是如何在代码中实现它,我只能实现JsonExeption。 这是我的代码: protected String doInBackground(String... args) { List<NameValuePair> params =
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_list_rs, "GET",
params);
Log.d("All Products: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
daftar_rs = json.getJSONArray(TAG_DAFTAR_RS);
for (int i = 0; i < list_rs.length(); i++) {
JSONObject c = list_rs.getJSONObject(i);
//Process Code
}
} else {
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
受保护的字符串doInBackground(字符串…args){
List params=new ArrayList();
JSONObject json=jParser.makeHttpRequest(url_list_rs,“GET”,
参数);
Log.d(“所有产品:,json.toString());
试一试{
int success=json.getInt(TAG_success);
如果(成功==1){
daftar\u rs=json.getJSONArray(TAG\u daftar\u rs);
对于(int i=0;i
其中url_list_rs是我的php的url。在哪里可以实现ExceptionConnection?有人能帮忙吗?感谢大师们 如果要创建自己的异常,可以执行以下操作:
//create a new Exception class
public class ConnectionException extends Exception {
public ConnectionException(String message){
super(message);
}
}
在makeHttpRequest方法中
if(no connection) { //check connection
throw new ConnectionException ("No connection!");
} else { ... }
最后,尝试捕捉块
try {
JSONObject json = jParser.makeHttpRequest(url_list_rs, "GET",
params);
catch(ConnectionException ex) {
ex.printStackTrace();
}
注意:我不确定这是否是最佳做法