【发布时间】:2017-05-10 01:17:59
【问题描述】:
我尝试使用 php 在 MySQL 数据库中保存用户电话号码和随机 4 位数字。一切都很好:电话号码和随机号码都保存在我的数据库中,我可以检索“SUCCESS”json 详细信息。但活动也使此吐司“解析 JSON 数据时出错。”。我在 {catch (JSONException e)} 中发现了这条消息.....我知道忘记在某处添加一些东西,如果有人可以帮助我,我将不胜感激。
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();
}
}
}
【问题讨论】: