服务器无法从Android读取json值:服务器收到未定义的IINdex和json null


Server cannot read json values from Android : Undifined IINdex and json null received at Server

我将实现客户端到服务器模块,该模块的请求从Android设备发送到php。说到实现,我不知道为什么服务器端不能接收到安卓设备发送的json。对于服务器,数据库插入模块是可以的,但无法捕获json值。你能告诉我客户方面出了什么问题吗?

下面是我的代码

客户端安卓

public String postData(String argument , String url) {
        String result="";
        // Create a new HttpClient and Post Header
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 60000 * 20;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        int timeoutSocket = 60000 * 20;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1);
        HttpConnectionParams.setSocketBufferSize(httpParameters, 8*1024);

        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

        //Log.d("url" , url);
        HttpPost httppost = new HttpPost(url);

        try {   
            System.out.println("Response:"+ "start execute");
            JSONObject json = new JSONObject();
            json.put("userid","loka");
            json.put("password","yoor");
            json.put("email","johnsmith@example.com");   

            //Log.d("test" , jsonString);
            httppost.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
            httppost.setHeader( "Content-Type", "application/json" );
            //httpPost.setEntity(new UrlEncodedFormEntity(params));
            HttpResponse response = httpclient.execute(httppost);
            int status = response.getStatusLine().getStatusCode();
            //If php json response required
            if(status==200){
                HttpEntity getResponseEntity = response.getEntity();
                InputStream httpResponseStream = getResponseEntity.getContent();
                 result = slurp(httpResponseStream , 8192);
                System.out.println("result: "+  result);
            }else{
                System.out.println("2");
                result = String.valueOf(status);
                HttpEntity getResponseEntity = response.getEntity();
                InputStream httpResponseStream = getResponseEntity.getContent();
                 result = slurp(httpResponseStream , 8192);
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();            
            return e.getMessage();
        } catch (ConnectTimeoutException e){ 
            e.printStackTrace();            
            return e.getMessage();
        }catch (IOException e) {
            e.printStackTrace();
            return e.getMessage();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;  
    }

服务器端php

<?php

header('Content-type: application/json');
ini_set('display_errors','On'); 
require_once 'lib_mysql/insert.php';
$json = file_get_contents('php://input');
 $result =insertUser($json);
 if($result) {
 echo "success";
 }
 else{
echo "fail"; 

}

?>


include_once("connection.php");
function insertUser($jsonString){
 $usern = $jsonString->userid; //Undefined Variables
 $pass = $jsonString->password; //Undefined Variables
 $email = $jsonString->email; //Undefined Variables
$conn = getDBConn();
$data = false;

您需要在php中使用json_decode()函数。header('Content-type: application/json');在这种情况下没有帮助

尝试这种方式,希望这能帮助你

Relspace

httppost.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));

带有

httppost.setEntity(new StringEntity(json.toString(), HTTP.UTF_8));