Php JSON检索很好,但也有JSONException错误
我尝试用php在MySQL数据库中保存用户电话号码和随机4位数字。一切都很好,两者都有:电话号码和随机号码都保存在我的数据库中,我可以检索“SUCCESS”json详细信息。但活动也会使此toast“解析JSON数据时出错”。我在{catch(JSONException e)}中发现了这条消息。。。。。我知道我忘了在某处添加一些东西,如果有人能帮助我,我将不胜感激Php JSON检索很好,但也有JSONException错误,php,android,mysql,json,Php,Android,Mysql,Json,我尝试用php在MySQL数据库中保存用户电话号码和随机4位数字。一切都很好,两者都有:电话号码和随机号码都保存在我的数据库中,我可以检索“SUCCESS”json详细信息。但活动也会使此toast“解析JSON数据时出错”。我在{catch(JSONException e)}中发现了这条消息。。。。。我知道我忘了在某处添加一些东西,如果有人能帮助我,我将不胜感激 public class SignUpActivity extends AsyncTask<String, Void, Str
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();
}
}
公共类注册活动扩展异步任务{
私人语境;
公共注册活动(上下文){
this.context=上下文;
}
受保护的void onPreExecute(){
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
字符串phoneNumber=arg0[0];
串链;
字符串数据;
BufferedReader BufferedReader;
字符串结果;
试一试{
data=“?phonenumber=“+urlcoder.encode(phonenumber,“UTF-8”);
链接=”https://androidtest22.000webhostapp.com/bubble/signupbubble.php“+数据;
URL=新的URL(链接);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
bufferedReader=新的bufferedReader(新的InputStreamReader(con.getInputStream());
结果=bufferedReader.readLine();
返回结果;
}捕获(例外e){
返回新字符串(“异常:+e.getMessage());
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
字符串jsonStr=result;
//Toast.makeText(context,jsonStr,Toast.LENGTH_SHORT).show();
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
字符串查询结果=jsonObj.getString(“查询结果”);
if(查询结果等于(“成功”)){
Toast.makeText(上下文,“数据插入成功。注册成功。”,Toast.LENGTH_SHORT.show();
}else if(查询结果等于(“失败”)){
Toast.makeText(上下文,“无法插入数据。注册失败。”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(上下文“无法连接到远程数据库”,Toast.LENGTH_SHORT.show();
}
}捕获(JSONException e){
e、 printStackTrace();
Toast.makeText(上下文,“解析JSON数据时出错”,Toast.LENGTH_SHORT.show();
}
}否则{
Toast.makeText(上下文,“无法获取任何JSON数据”,Toast.LENGTH_SHORT.show();
}
}
}此处应注意以下几点:
无法解析主机“androidtest22.000webhostapp.com”:没有与主机名关联的地址
。检查您的WiFi连接和设备连接如果尝试使用新的JSONObject(jsonStr.trim())会怎么样 它抛出
JSONException
,因为它打印的JSON字符串无效。我已经检查了URL:https://androidtest22.000webhostapp.com/bubble/signupbubble.php?phonenumber=1265
并显示
83{"query_result":"SUCCESS"}
您必须从结果中删除该数字。在上面的例子中是83。您可能忘记删除调试程序时使用的echo
解决方案:从结果中删除该号码,然后重试
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();
}
83{"query_result":"SUCCESS"}