我試圖用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();
}
}
}