Java 从android向php linux服务器发送json数据
我开发了一个Android应用程序,使用JSON将数据发送到php web服务。我用wampp服务器做的,一切都很顺利。但现在,我正试图将它部署到一个带有LinuxSever(GoDaddy.com)的web主机中,奇怪的事情正在发生 以下是我的java代码:Java 从android向php linux服务器发送json数据,java,php,android,json,linux,Java,Php,Android,Json,Linux,我开发了一个Android应用程序,使用JSON将数据发送到php web服务。我用wampp服务器做的,一切都很顺利。但现在,我正试图将它部署到一个带有LinuxSever(GoDaddy.com)的web主机中,奇怪的事情正在发生 以下是我的java代码: URL url = new URL(JSON_POST_VENDA); HttpURLConnection con = (HttpURLConnection) url.openConnection();
URL url = new URL(JSON_POST_VENDA);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
// To upload data to a web server, configure the connection for output
con.setDoOutput(true);
con.setDoInput(true);
// For best performance, you should call either setFixedLengthStreamingMode(int)
// when the body length is known in advance, or setChunkedStreamingMode(int)
// when it is not
con.setChunkedStreamingMode(0);
con.setRequestMethod("POST");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("Content-type", "application/json;charset=utf-8");
con.setRequestProperty("accept-charset", "UTF-8");
// bla bla bla ... (just adding JSONObject and array)
os = new OutputStreamWriter(con.getOutputStream(), "utf-8");
os.write(vendaJson.toString());
os.close();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK){
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String sLinha = null;
StringBuilder builder = new StringBuilder();
while ((sLinha = reader.readLine()) != null){
builder.append(sLinha+"\n");
}
reader.close();
Log.d("Post da Venda", builder.toString());
return builder.toString();
} else {
Log.d("Post de Venda", "Não conseguiu conectar!");
return "500";
}
和我的php服务器代码:
if($_SERVER["REQUEST_METHOD"] == "POST") {
//primeiro parametro recupera o objeto json do post content
//segundo parametro transforma o objeto em array assossiativo
//$jsondata = json_decode(file_get_contents("php://input"), true);
$param = file_get_contents("php://input");
echo "Receved: ".$param;
$jsondata = json_decode($param, true);
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
在“Log.d(“Post da Venda”,builder.toString());”行中登录的内容是:
D/Post da Venda:已接收:上次错误:未发生错误
我确信服务器正在工作,因为我已经使用JaSON for Chrome对其进行了测试,并得到以下响应:
收到:{“液化石油气”:130,“皮尔斯”:6,“生产商”:[{“液化石油气”:120,“生产商”:7,“液化石油气”:0,“液化石油气”:120,“液化石油气”:1,“描述”:1,“描述”:“Barbell Ouro Amarelo 2mm Ouro”,“总计”:120}],“idvenda”:0,“服务商”:[{“液化石油气”:10,“servico”:2,“液化石油气”:0,“液化石油气”:10,“液化石油气”:1,“描述”:1,“描述:”Cao“AplicaèOuro”;“总计”:10,“布鲁特罗”:130,“数据”:“2016-03-05”,“功能”:7,“tt_itens”:2,“vl_Descon”:0,“senha”:5,“observacao”:“}最后一个错误:未发生任何错误
会发生什么?
我一直在寻找聊天室的演员,但对我来说什么都不管用
任何帮助都将受到感谢
谢谢你好吧,我通过使用解决了这个问题。实现起来非常简单
谢谢你所做的一切。出了什么问题?您的服务器没有收到json数据,或者什么?因为您指定的
Log.d()。$param变量在echo调用中为空。我是sugin php版本5.5(只是一个信息)。