doudu5498 2016-04-30 09:35
浏览 92
已采纳

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

this is my php script

<?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,

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

    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.

  • 写回答

3条回答 默认 最新

  • dongri1989 2016-05-04 22:07
    关注

    in the place of

    result = bufferedReader.readLine();
    

    use

    result = bufferedReader.readLine().toString();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀