我開發了一個Android應用程序,使用JSON將數據發送到php web服務。我使用wampp服務器做到了,一切都很順利。但是現在,我正試圖將它部署在一個使用Linux服務器(GoDaddy.com)的虛擬主機中,現在發生了奇怪的事情。發送json數據從android到php linux服務器
這裏是我的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記錄(」 後達文達」,builder.toString()) ;」路線是:
d /後達文達:Receved:最後一個錯誤:未出錯
我敢肯定服務器正常工作,東陽我用賈森爲Chrome進行了測試,並得到了這種反應:
獲得:{「vl_liquido」:130,「piercer」:6,「produtos」:[{「vl_liquido」:120,「produto」:7,「vl_desconto」:0,「vl_unitario」 「qtd」:1,「descricao」:「Barbell Ouro Amarelo 2mm Ouro」,「vl_total」:120}],「idvenda」:0,「servicos」:[{「vl_liquido」:10,「servico」 「vl_desconto」:0,「vl_unitario」:10,「qtd」:1,「descricao」:「AplicaçãoPiercing Nariz」,「vl_total」:10}],「vl_bruto」:130,「data」:「2016-03 -05「,」funcionario「:7,」tt_itens「:2,」vl_desconto「:0,」senha「:5,」observacao「:」「}最後一個錯誤:發生錯誤
會發生什麼情況? 我已經找到了來回聊天投射,但沒有爲我工作。
任何幫助將apreciated,
謝謝
那麼是怎麼回事?你的服務器沒有收到JSON數據或什麼?因爲您指定的'Log.d()'只有在獲得HTTP狀態200時纔會被調用,這表明服務器沒有錯誤地接收到請求。 –
@NielsMasdorp,是的服務器沒有收到數據。回顯調用中的$ param變量爲空。我是起訴PHP版本5.5(只是一個信息)。 – mTheSame