[英]Error Parsing JSON data on executing PHP script
这是我的 php 脚本
<?php
try{
$con = new PDO('mysql:host=localhost;dbname=pfe'/*info db*/,'root'/*login*/, ''/*mdp*/);
}
catch (Exception $e)
{
die('Erreur : '.$e->getMessage());
}
$msg = $_GET['msg'];
$mail = $_GET['mail'];
$result = $con->prepare("INSERT INTO message ( `msg`, `mail`)
VALUES ('{$msg}', '{$mail}')");
$result->execute();
if($result == true) {
echo '{"query_result":"SUCCESS"}';
}
else{
echo '{"query_result":"FAILURE"}';
}
我认为我的脚本很好,因为我用我的浏览器尝试过它可以工作,但使用 android 不会插入数据,这是 java 类,
EditText msg,mail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
msg = (EditText) findViewById(R.id.editText);
mail = (EditText) findViewById(R.id.editText2);
}
public void signup(View v) {
String message = msg.getText().toString();
String email = mail.getText().toString();
Toast.makeText(this, "wait...", Toast.LENGTH_SHORT).show();
new Insertinto(this).execute(message, email);
}
public class Insertinto extends AsyncTask<String, Void, String> {
private Context context;
public Insertinto(Context context) {
this.context = context;
}
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... arg0) {
String msg = arg0[0];
String mail = arg0[1];
String link;
String data;
BufferedReader bufferedReader;
String result;
try {
data = "?msg=" + URLEncoder.encode(msg, "UTF-8");
data += "&mail=" + URLEncoder.encode(mail, "UTF-8");
link = "http://192.168.43.93/dysfonction.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());
}
}
这是 json
protected void onPostExecute(String result) {
String jsonStr = result;
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", Toast.LENGTH_SHORT).show();
} else if (query_result.equals("FAILURE")) {
Toast.makeText(context, "Data could not be inserted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Couldn't connect to 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();
}
}
}
}
当我执行它时显示“解析 JSON 数据时出错”,我没有找到 pbm 在哪里。
调试并检查 postExecute() 中收到的结果; 并从下次粘贴 logcat 以及..
尝试将其更改为
if($result == true) {
echo '{"query_result":"SUCCESS"}';
}else{
echo '{"query_result":"FAILURE"}';
}
这个
if($result == true) {
$data = array(
'query_result' => 'SUCCESS'
);
}else{
$data = array(
'query_result' => 'FAILURE'
);
}
echo json_encode($data);
看来你的java代码是这么清楚的。 所以从上面的 php 代码发送的响应可能是有效的。
代替
result = bufferedReader.readLine();
用
result = bufferedReader.readLine().toString();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.