dongmei9203 2014-04-09 07:10
浏览 51
已采纳

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

I am going to implement the client-to-Server module which request is sent from Android devices to php . When it comes to the implementation , I have no clue why the server side cannot receive the json sent from Android device . As for the server, the database insert module is OK but cannot catch the json values . Would you please tell me what is wrong with the client side ?

The below is my code

Client side Android

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;  
    }

Server side 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;
  • 写回答

2条回答 默认 最新

  • duanlei1957 2014-04-09 07:16
    关注

    You need to use json_decode() function in php. header('Content-type: application/json'); would not help in this case

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。