[英]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.