在PHP从机器人发送读取JSON对象

问题描述:

我可以读的PHP应用程序的JSON对象时,这个对象是由一个Android应用程序发送?
我不知道我是否允许使用json_de code()在PHP方便,
这里是code从机器人发送一个JSON对象到PHP应用程序

Can I read a JSON Object in the PHP application when this object is sent by an android application? I don't know if I am allowed to use json_decode() in the easy php, here is the code for sending a json object from android to php application

尝试{
            Log.i(-------------, _ __ _ __ 4 _ __ _ );

try { Log.i("-------------", "______4____");

        httpParams = new BasicHttpParams();
        Log.i("-------------", "_____________5_______________");

        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        Log.i("-------------", "________________6____________");
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        Log.i("-------------", "________________7____________");
        httpclient = new DefaultHttpClient(httpParams);
        Log.i("-------------", "_______________8_____________");
        httppost = new HttpPost(url);
        Log.i("-------------", "_________________9___________");
        // preciser le type d'envois
        httppost.setHeader("Content-Type", "application/json");
        Log.i("-------------", "__________________10__________");
        json = new JSONObject();
        Log.i("-------------", "________________11____________");
        json.put("action", action);
        Log.i("-------------", "_________________12___________");
        json.put("User", User);
        Log.i("-------------", "_________________13___________");
        json.put("Password", Password);
        Log.i("-------------", "_________________14___________");
        json.put("IMEI", "356299046324945");
        Log.i("-------------", "__________________15__________");

        httppost = new HttpPost(url);
        Log.i("-------------", "___________________16_________");
        httppost.setHeader("json", json.toString());
        Log.i("-------------", "____________________17________");
        HttpResponse response = httpclient.execute(httppost);
        Log.i("-------------", "_______________________18_____");
        HttpEntity entity = response.getEntity();
        Log.i("-------------", "________________________19____");



        //******************************************

        if (entity != null) {       
            Log.i("-------------", "___________________20__________");
            String result = null;
            Log.i("-------------", "_________________21____________");
            try{
                Log.i("-------------", "__________________22___________");
                InputStream instream = entity.getContent();
                Log.i("-------------", "___________________23__________");
            reader = new BufferedReader(new InputStreamReader(instream,"iso-8859-1"),8);
            Log.i("-------------", "____________________24_________");
         sb = new StringBuilder();
            Log.i("-------------", "_____________________25________");
             line = null;
                Log.i("-------------", "________________26_____________");
            while ((line = reader.readLine()) != null) {
                Log.i("-------------", "___________________27__________");
                sb.append(line + "\n");
                Log.i("-------------", "_________________28____________");
            }
            Log.i("-------------", "______________________29_______");
            instream.close();
            Log.i("-------------", "____________________30_________");
            result=sb.toString();

            Log.i("-------------", "___________________31_________");
        //  Log.i("log_tag", "Error converting result "+result.toString());
            Log.i("-------------", "_________________32____________");
        }

这里是在PHP读取JSON对象code

here is the code of reading json object in the php

$decoded = json_decode($_GET['json'], true);
    //$decoded = json_decode($_POST['json']);
    // do something with data here
    $Longitude = $decoded['action'];
    $Lattetude = $decoded['User'];
    $l= $decoded['Password'];
    $la = $decoded['IMEI'];

但在执行的时候,我得到这个错误

but when executing I am getting this error

 Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

我认为PHP应用程序没有提取从$ _GET JSON对象()

i think that php application did not extract the json object from the $_GET()

您正在做这样的:

httppost.setHeader("json", json.toString());

这台标题,不是POST参数。在你的PHP,你要做到这一点:

which sets a header, NOT a POST parameter. In your PHP, you want to do this:

$decoded = json_decode($_SERVER['HTTP_JSON'], true);