我开发了一个Android应用程序,使用JSON将数据发送到php web服务。我用wampp服务器做的,一切都很好。但现在,我正试图将它部署到一个带有Linux服务器(GoDaddy.com)的web主机中,奇怪的事情正在发生。
这是我的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:recved:最后一个错误:未发生错误
我确信服务器正在工作,因为我已经使用JaSON for Chrome对其进行了测试,得到了以下响应:
接受者:{“vl-liquido”:130,“刺穿者”:6,“produtos”:{“vl-liquido”:120,“produto”:120,“produto”:7,“vl-descon”:0,“vl-安盟”:120,“qtd”:1,“descricao”:“Barbell-Ouro-Amarelo-Amarelo-Barbell-Ouro”,“vl-总共”:120}],“idvenda”:0,“servicos”:“[{“vl-liquido”:10,“servicos”:10,“servico”:2,“vl-descon”:0,“vl-安盟”:0,“vl-安盟”:10,“vl-安盟”:10,“qtd”:10,“qtd”:1,“descricao”:“aplicca”描述:1穿孔Nariz,“vl U总计”:10}],“vl_bruto”:130,“data”:“2016-03-05”,“functionario”:7,“tt_itens”:2,“vl_descon”:0,“senha”:5,“observaco”:“}上一个错误:未发生错误
会发生什么事?
我坐过聊天室的座位,但没用。
任何帮助都是值得的,
谢谢你
最佳答案
好吧,我用OkHttp来解决这个问题。实现起来非常简单。
谢谢你的一切。