2017-05-10 30 views
-1

我試圖用php保存用戶電話號碼和MySQL數據庫中的隨機4位數字。一切都很好,兩者:電話號碼和隨機數字都保存在我的數據庫,我可以檢索「SUCCESS」json細節。但是活動也會讓這個吐司「解析JSON數據時出錯」。我發現這個消息{catch(JSONException e)} .....我知道忘了添加一些東西,我會很感激,如果有人可以幫助我。JSON恢復正常,但也有JSONException錯誤

public class SignUpActivity extends AsyncTask<String, Void, String> { 

private Context context; 

public SignUpActivity(Context context) { 
    this.context = context; 
} 

protected void onPreExecute() { 

} 

@Override 
protected String doInBackground(String... arg0) { 
    String phoneNumber = arg0[0]; 


    String link; 
    String data; 
    BufferedReader bufferedReader; 
    String result; 

    try { 
     data = "?phonenumber=" + URLEncoder.encode(phoneNumber, "UTF-8"); 

     link = "https://androidtest22.000webhostapp.com/bubble/signupbubble.php" + data; 
     URL url = new URL(link); 
     HttpURLConnection con = (HttpURLConnection) url.openConnection(); 

     bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
     result = bufferedReader.readLine(); 
     return result; 
    } catch (Exception e) { 
     return new String("Exception: " + e.getMessage()); 
    } 
} 

@Override 
protected void onPostExecute(String result) { 
    String jsonStr = result; 
    //Toast.makeText(context,jsonStr, Toast.LENGTH_SHORT).show(); 
    if (jsonStr != null) { 
     try { 
      JSONObject jsonObj = new JSONObject(jsonStr); 
      String query_result = jsonObj.getString("query_result"); 
      if (query_result.equals("SUCCESS")) { 
       Toast.makeText(context, "Data inserted successfully. Signup successful.", Toast.LENGTH_SHORT).show(); 
      } else if (query_result.equals("FAILURE")) { 
       Toast.makeText(context, "Data could not be inserted. Signup failed.", Toast.LENGTH_SHORT).show(); 
      } else { 
       Toast.makeText(context, "Couldn't connect to remote database.", Toast.LENGTH_SHORT).show(); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
      Toast.makeText(context, "Error parsing JSON data.", Toast.LENGTH_SHORT).show(); 
     } 
    } else { 
     Toast.makeText(context, "Couldn't get any JSON data.", Toast.LENGTH_SHORT).show(); 
    } 
} 

}

回答

0

幾點應該注意:

  1. 即使拋出一個異常,你是返回一個字符串,所以如果有錯誤,你將嘗試轉換生成的字符串轉換爲JSON,這會拋出JSONException異常。
  2. 您沒有關閉連接流。
  3. 您正在傳遞一個上下文作爲參數,這不是一個好的做法,因爲它很容易導致內存泄漏,或者像幻影對象那樣,請改用自定義接口。
  4. 如果您有:​​。檢查您的WiFi連接和設備連接。
  5. 你的webservice在JSON之前返回一個整數,你可以看到here,這使得解析變得不可能。

最後,代碼:

SignUpTask.java

public class SignUpTask extends AsyncTask<String, Void, String> { 

    private static final String TAG = SignUpTask.class.getSimpleName(); 

    private final OnResponse onResponse; 

    public SignUpTask(final OnResponse onResponse) { 
     this.onResponse = onResponse; 
    } 

    @Override 
    protected String doInBackground(final String... phones) { 
     if (phones == null || phones.length < 1) { 
      throw new IllegalArgumentException("You must pass a phone to this task"); 
     } 

     final String phone = phones[0]; 
     HttpURLConnection connection = null; 

     try { 
      final String url = "https://androidtest22.000webhostapp.com/bubble/signupbubble.php" + 
       "?phonenumber=" + URLEncoder.encode(phone, "UTF-8"); 

      connection = (HttpURLConnection) new URL(url).openConnection(); 
      return new Scanner(connection.getInputStream(), "UTF-8").next(); 
      } catch (final IOException e) { 
       Log.e(TAG, "Error - Phone Number: " + phone, e); 
     } finally { 
      if (connection != null) { 
       connection.disconnect(); 
      } 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(final String result) { 
     if (result == null || result.isEmpty()) { 
      onResponse.onFailure(new RuntimeException("Invalid result")); 
      return; 
     } 

     try { 
      onResponse.onSuccess(new JSONObject(result)); 
     } catch (final JSONException e) { 
      onResponse.onFailure(e); 
     } 
    } 

    public interface OnResponse { 

     void onSuccess(JSONObject result); 

     void onFailure(Throwable throwable); 
    } 
} 

在你YourActivity.java

private void request() { 
    new SignUpTask(new SignUpTask.OnResponse() { 
     @Override 
     public void onSuccess(final JSONObject result) { 
      String queryResult = null; 
      try { 
       queryResult = result.getString("query_result"); 
      } catch (final JSONException e) { 
       Log.e(TAG, "Error", e); 
      } 

      if ("SUCCESS".equals(queryResult)) { 
       showToast("Data inserted successfully."); 
      } else if ("FAILURE".equals(queryResult)) { 
       showToast("Data could not be inserted."); 
      } else { 
       showToast("Couldn't connect to remote database."); 
      } 
     } 

     @Override 
     public void onFailure(final Throwable throwable) { 
      Log.e(TAG, "Error", throwable); 
      showToast("Couldn't get any JSON data."); 
     } 
    }).execute("999999999"); 
} 

private void showToast(final String message) { 
    Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 
} 
0

如果嘗試使用新的JSONObject什麼(jsonStr 。修剪( ));

0

它拋出JSONException,因爲它打印無效的JSON字符串。我檢查的網址:https://androidtest22.000webhostapp.com/bubble/signupbubble.php?phonenumber=1265,它顯示的

83{"query_result":"SUCCESS"} 

的輸出必須從結果中去除數量。在上面的情況下,它是83.可能你忘了刪除在調試程序時使用的echo

解決方案:從結果中刪除數字,然後重試。

相關問題