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条)

报告相同问题?