2013-11-23 23 views
0

我正在開發一個連接到PHP並從數據庫獲取數據的應用程序。jParser.makeHttpRequest當我沒有連接到互聯網時不會捕獲錯誤

我的問題是當我通過jParser.makeHttpRequest與互聯網連接獲取數據,並關閉我的互聯網,並再次這樣做jParser.makeHttpRequest返回它返回的最後一件事。它不會給沒有互聯網錯誤或類似的東西。我該如何解決這個問題?

jParser = new JSONParser(); 
JSONObject json = jParser.makeHttpRequest(url_get_login_infos, "GET", _params); 
int success = json.getInt(TAG_SUCCESS); 
String message = json.getString(TAG_MESSAGE); 

回答

0

對象被垃圾回收器摧毀。第二次調用數據時,您的對象仍然存在。 在您調用數據並測試結果之前,使用null重置您的分析器。發表您的一些代碼,更多的答案

這裏的一些使用JSON至極代碼工作正常

 protected String doInBackground(String... args) { 

     //affectation des valeurs pour passage à PHP 
     String email = getEmail(); 

     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("email", email)); 

     // getting JSON Object 
     String url = SingletonParametres.getUrlConnexion() + "get_selectuser.php"; 
     JSONObject json = null; 
     try { 
      json = jsonParser.makeHttpRequest(url, "GET", params); 
     } catch (HttpException e) { 
      e.printStackTrace(); 
     } 
     // check for success tag 
     try { 
      String Variablepourvoir = json.getString(TAG_SUCCESS); 
      success = json.getInt(TAG_SUCCESS); 
      switch (success) { 
       case 1: {    /* user retrouvé avec succes */ 
        // successfully select user 
        setCodeRetour(1); 
        setMsgRetour("user existe MyApplication ok"); 
        // sauvegarde des données User dans la classApplication 
        // successfully received product details 
        JSONArray JsonUserArray = json.getJSONArray("user"); // JSON Array 

        // get first user object from JSON Array 
        JSONObject JsonUserObject = JsonUserArray.getJSONObject(0); 

        // user with this email found 
        //save data in MyApplication 

        MyApplication.setIdUser(JsonUserObject.getInt("iduser")); 
        MyApplication.setEmail(JsonUserObject.getString("email")); 
        MyApplication.setPseudo(JsonUserObject.getString("pseudo")); 
        if (JsonUserObject.getInt("autorisation_upload") ==1) { 
         MyApplication.setAutorisationUpload(true); 
        } else { 
         MyApplication.setAutorisationUpload(false); 
        } 

         break; 
       } 
       case 0: {    /* oops ! ... un probleme est survenu */ 
        setCodeRetour(ECHEC); 
        setMsgRetour("user non trouvé MyApplication echec"); 
        break; 

       } 
      } 



     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 
+0

我把jParser =新JSONParser();只是在jParser.makeHttpRequest之前發生了同樣的事情 – halil12

+0

女巫值是在你的TAG_SUCCESS?爲什麼不測試它?當你說「返回它返回的最後一件東西」或「發生了同樣的事情」時,你的意思是什麼? –

+0

當我嘗試捕獲HttpException AndroidStudio說異常'org.apache.http.HttpException'永遠不會在相應的嘗試塊中拋出。 – halil12

0

試試這個,你有添加此條件:

if(json != null){ 
    //excecute code 

    }else{ 
     getActivity().runOnUiThread(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(getActivity(), "conection error", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
} 

寫敬酒這樣,如果你在片段中工作。

在你的代碼

,使這個:

String url = SingletonParametres.getUrlConnexion() + "get_selectuser.php"; 
JSONObject json = null; 
json = jsonParser.makeHttpRequest(url, "GET", params); 
if(json != null) { 

    try { 
      String Variablepourvoir = json.getString(TAG_SUCCESS); 
      success = json.getInt(TAG_SUCCESS); 
      switch (success) { 
       case 1: { 

      ..... 
      ..... 
}else{ 
    getActivity().runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     Toast.makeText(getActivity(), "conection error", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 
相關問題