JSON sent from Android to compute engine server returns null

一笑奈何 提交于 2019-12-12 03:24:16

问题


I am sending JSON object from my Android app to my server. In my development local machine it works fine. I get the data and decode it. But when I deployed my server to compute engine, I don't receive the data on the server end. The data is sent from the Android client. I also tested the JSON from my browser and I get 200 response code. Here are the code snippets:

//Client

//Create JSONObject here This is the JSON I am sending {"test", "1234"}

JSONObject json = new JSONObject();
json.put("test", args[0]);

String postData=json.toString();

// Send POST output.
OutputStreamWriter os = new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8");
os.write(postData);
Log.e("NOTIFICATION", "Sent");
os.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String msg="";
String line = "";
while ((line = reader.readLine()) != null) {
    msg += line; 
}
Log.i("msg=",""+msg);

//Server

<?php

   $json = file_get_contents("php://input");
   var_dump($json); //this returns String(0)
   $decoded = json_decode($json, TRUE);
   $pasid = $decoded['test'];

   echo"test if it works";
   var_dump($pasid); //returns null

No matter what I do the Android app sends a string but on the server side I am getting empty string. I can't figure out why so far.


回答1:


your code is very confusing this is not good way to send data from app to remote database you can do like this `

form the app

String uri="But your http URL";
URL url = new URL(uri);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
            con.setDoOutput(true);
            OutputStreamWriter writer=new     OutputStreamWriter(con.getOutputStream());
StringBuilder sb=new StringBuilder();
sb.append("username="+"Naham");
sb.append("password="+"passNaham");
            writer.write(sb.toString());
            writer.flush();

Know the simple php web service


    $data = array("result" => 0, "message" => "Error!");
    if ($_SERVER['REQUEST_METHOD'] == "POST") {
                $user_name = isset($_POST['username']) ? $_POST['user_name'] : "";
                $user_password = isset($_POST['user_password']) ?     $_POST['user_password'] : "";

               // do some thing here
    $data = array("result" => 1, "message" => "welcome");
    } else
        $data = array("result" => 0, "message" => "Error!");
    /* Output header */
    header('Content-type: application/json');
    echo json_encode($data);
    ?>





回答2:


after send post request to server you can get post body with these codes:

<?php

  $post_body = file_get_contents("php://input");
  $json = json_decode($post_body);
  $pasid = $json->{"test"};

  echo $pasid;
?>



回答3:


Turns out the issue is caused by of HttpRLConnection. Just like the accepted answer in this SO question, just remove `

urlConnection.setChunkedStreamingMode(0);

where `urlConnection = HttpUrlCooenction();

`



来源:https://stackoverflow.com/questions/36558261/json-sent-from-android-to-compute-engine-server-returns-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!