简体   繁体   English

执行 PHP 脚本时解析 JSON 数据时出错

[英]Error Parsing JSON data on executing PHP script

this is my 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"}';
}

my script i think is good cause i tried it with my browser it works but with android does not insert the data and this is java class,我认为我的脚本很好,因为我用我的浏览器尝试过它可以工作,但使用 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());
        }
    }

this is json这是 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();
        }
    }
}
}

when i execute it show me "Error parsing JSON data" i didn't find where is the pbm.当我执行它时显示“解析 JSON 数据时出错”,我没有找到 pbm 在哪里。

debug and check the result received in the postExecute();调试并检查 postExecute() 中收到的结果; and from next time paste the logcat as well..并从下次粘贴 logcat 以及..

Try to change this to尝试将其更改为

if($result == true) {
echo '{"query_result":"SUCCESS"}';
}else{
echo '{"query_result":"FAILURE"}';
}

this这个

if($result == true) {
   $data = array(
       'query_result' => 'SUCCESS'
   );    
}else{
   $data = array(
       'query_result' => 'FAILURE'
   );    
}
echo json_encode($data);

It seems to be your java code is so clear.看来你的java代码是这么清楚的。 So sent response from php code like above may be works.所以从上面的 php 代码发送的响应可能是有效的。

in the place of代替

result = bufferedReader.readLine();

use

result = bufferedReader.readLine().toString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM