我正在与php mysql travez中的一个数据库通信,以便在ListView中显示结果,但我正在尝试实现一个ExeptionConnection,用于当3G或WIFI应用程序无法连接时,返回到之前的活动并显示吐司。但不知道如何在我的代码中实现它,我只能实现JsonExeption。这是我的代码:
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;
}其中url_list_rs是我的php的url。在哪里可以实现ExeptionConnection?有人能帮上忙吗?感谢大师们!
发布于 2013-04-03 09:26:29
如果你想创建你自己的异常,你可以这样做:
//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-catch块
try {
JSONObject json = jParser.makeHttpRequest(url_list_rs, "GET",
params);
catch(ConnectionException ex) {
ex.printStackTrace();
}注意:我不确定这是否是最佳实践。
https://stackoverflow.com/questions/15776717
复制相似问题