gpt4 book ai didi

java - 将json数据从android发送到php linux服务器

转载 作者:太空宇宙 更新时间:2023-11-04 12:55:38 24 4
gpt4 key购买 nike

我开发了一个 Android 应用程序,它使用 JSON 将数据发送到 php 网络服务。我是用 wampp 服务器做的,一切正常。但是现在,我正尝试将它部署在带有 Linux 服务器 (GoDaddy.com) 的虚拟主机中,现在发生了奇怪的事情。

这是我的java代码:

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 对其进行了测试,并得到了以下响应:

收到:{"vl_liquido":130,"piercer":6,"produtos":[{"vl_liquido":120,"produto":7,"vl_desconto":0,"vl_unitario":120,"qtd ":1,"descricao":"Barbell Ouro Amarelo 2mm Ouro","vl_total":120}],"idvenda":0,"servicos":[{"vl_liquido":10,"servico":2,"vl_desconto ":0,"vl_unitario":10,"qtd":1,"descricao":"Aplicação Piercing Nariz","vl_total":10}],"vl_bruto":130,"data":"2016-03-05 ","funcionario":7,"tt_itens":2,"vl_desconto":0,"senha":5,"observacao":""}最后一个错误:没有发生错误

会发生什么?我搜索过聊天集转换,但没有任何效果。

如有任何帮助,我们将不胜感激

谢谢

最佳答案

好吧,我使用 OkHttp 解决了这个问题.实现起来非常简单。

谢谢你所做的一切。

关于java - 将json数据从android发送到php linux服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35816872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com