从Android发送到计算引擎服务器的JSON返回null [英] JSON sent from Android to compute engine server returns null
问题描述
我正在将JSON对象从我的Android应用发送到服务器.在我开发的本地计算机上,它工作正常.我得到数据并解码.但是,当我将服务器部署到计算引擎时,我没有在服务器端收到数据.数据从Android客户端发送.我还从浏览器测试了JSON,并获得了200个响应代码.以下是代码段:
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:
//客户
//在此处创建JSONObject 这是我发送的JSON {"test","1234"}
//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);
//服务器
<?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
无论我做什么,Android应用程序都会发送一个字符串,但是在服务器端,我会得到一个空字符串.我至今不知道为什么.
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.
推荐答案
原来,该问题是由HttpRLConnection引起的.就像中接受的答案一样这个问题,只需删除"
Turns out the issue is caused by of HttpRLConnection. Just like the accepted answer in this SO question, just remove `
urlConnection.setChunkedStreamingMode(0);
urlConnection.setChunkedStreamingMode(0);
其中 `urlConnection = HttpUrlCooenction();
where `urlConnection = HttpUrlCooenction();
`
这篇关于从Android发送到计算引擎服务器的JSON返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!