0
我想要處理每一個可能的例外。例如提醒所有更多鈔票系統錯誤連接服務器
如果服務器連接的IP不正確,那麼就應該拋出異常,我應該提醒用戶特定的處理它消息
如果服務器發生故障,那麼它應該表現出異常,我自動顯示警告用戶,時間是OUT
我的應用程序在上述情況下做了什麼
它給出了不一致,並且它同時崩潰。
它詳細介紹了誤差在兩個地方
HttpResponse httpResponse = httpClient.execute(httpPost);
和
json = jParser.makeHttpRequest(URL,"POST", params);
但是當我嘗試通過try/catch
來處理。我是無法處理。
我jsonParser /連接器的代碼是
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
int TIME_OUT_IN_SECONDS = 120;
HttpPost httpPost;
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) throws TimeoutException {
// Making HTTP request
try {
// check for request method
//if(method == "POST")
if(method.equals("POST")){
// request method is POST
// defaultHttpClient
Log.d("JSONParser","POST METHOD ");
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
HttpConnectionParams.setSoTimeout(httpParameters, 3000);
DefaultHttpClient httpClient = new DefaultHttpClient();
try
{
httpPost = new HttpPost(url);
}
catch(Exception d)
{
Log.d("httpPost",d.toString());
}
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.d("JSONParser is",is.toString());
}
}
}
else if(method == "GET"){
Log.d("JSONParser","GET METHOD ");
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.d("JSONParser is",is.toString());
}
}
catch(ConnectTimeoutException e){
Log.e("Timeout Exception: ", e.toString());
e.printStackTrace();
}
catch(SocketTimeoutException ste){
Log.e("Socket Timeout Exception: ", ste.toString());
ste.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
和我的請求產生的地方是
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(FoodMenu.this);
pDialog.setMessage("Loading . Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = null;
try {
json = jParser.makeHttpRequest(URL,"POST", params);
if(json.toString()==null)
{Log.d("All Products: ", "ssss");}
else
Log.d("All Products: ", json.toString());
} catch (TimeoutException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
response=2;
}
// Check your log cat for JSON reponse
try {
products = json.getJSONArray(TAG_dishes);
Log.i("Response ",products.toString());
// looping through All Products
Log.i("LENGTHHHH "," "+products.length());
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
String CatNameString = c.getString(TAG_CatName);
String CatId = c.getString(TAG_CatId);
String DishImageNob = c.getString(TAG_DishImage);
Log.i("CatNameString ",CatNameString);
Log.i("CatId::",CatId);
Log.i("DishImageNob",DishImageNob);
obj=new DataHolder();
obj.setData(CatNameString,CatId,DishImageNob);
HashMap<String, Object> map = new HashMap<String, Object>();
// adding each child node to HashMap key => value
map.put(ITEMTITLE, obj);
// adding HashList to ArrayList
ProductList.add(map);
}
}
catch (JSONException e) {
e.printStackTrace();
response=2;
// Toast.makeText(getBaseContext(), "Unable to Fulfill request", Toast.LENGTH_LONG).show();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
if (pDialog.isShowing()) {
pDialog.dismiss();
// progressDialog.setCancelable(true);
}
if(response==1)
{ Toast.makeText(getBaseContext(), "No Record Found", Toast.LENGTH_LONG).show();
}
if(response==2)
{
Toast.makeText(getBaseContext(), "Unable to Fulfill request.Problem with Connection", Toast.LENGTH_LONG).show();
}
我的logcat到第一個問題是
10-21 16:12:38.440: W/System.err(14182): java.net.SocketException: No route to host
10-21 16:12:38.440: W/System.
err(14182): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
10-21 16:12:38.440: W/System.err
(14182): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:369)
10-21 16:12:38.440: W/System.err(14182): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:208)
10-21 16:12:38.440: W/System.err(14182): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:431)
10-21 16:12:38.440: W/System.err(14182): at java.net.Socket.connect(Socket.java:901)
10-21 16:12:38.440: W/System.err(14182): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
10-21 16:12:38.440: W/System.err(14182): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
10-21 16:12:38.440: W/System.err(14182): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-21 16:12:38.440: W/System.err(14182): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-21 16:12:38.440: W/System.err(14182): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-21 16:12:38.440: W/System.err(14182): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-21 16:12:38.440: W/System.err(14182): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-21 16:12:38.450: W/System.err(14182): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-21 16:12:38.450: W/System.err(14182): at com.ilmasoft.local.bundu_khan.JSONParser.makeHttpRequest(JSONParser.java:78)
10-21 16:12:38.450: W/System.err(14182): at com.ilmasoft.local.bundu_khan.FoodMenu$LoadAllProducts.doInBackground(FoodMenu.java:229)
10-21 16:12:38.450: W/System.err(14182): at com.ilmasoft.local.bundu_khan.FoodMenu$LoadAllProducts.doInBackground(FoodMenu.java:1)
10-21 16:12:38.450: W/System.err(14182): at android.os.AsyncTask$2.call(AsyncTask.java:252)
10-21 16:12:38.450: W/System.err(14182): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-21 16:12:38.450: W/System.err(14182): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-21 16:12:38.450: W/System.err(14182): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
10-21 16:12:38.460: W/System.err(14182): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
10-21 16:12:38.460: W/System.err(14182): at java.lang.Thread.run(Thread.java:1020)