0
我需要發佈這個json到服務器,有沒有人有解決這個問題?Http發佈Json到Restful web服務沒有成功
Var req =
{
getItInputTO =
{
「zipCode」:」(value)」
"financingOption":"(value)",
「make":"(value)」,
"baseAmountFinanced":"(value)",
"modelYear":"(value)",
"trimCode":"(value)",
"totalMSRP":"(value)",
「aprRate」: "(value)" (
}
}
我已經使用這個代碼,但到目前爲止,我似乎無法得到迴應:
try{
JSONStringer vehicle = new JSONStringer()
.object()
.key("getItInputTO")
.object()
.key("zipCode").value("90505")
.key("financingOption").value("B")
.key("make").value("Scion")
.key("baseAmountFinanced").value("12000")
.key("modelYear").value("2010")
.key("trimCode").value("6221")
.key("totalMSRP").value("15000")
.key("aprRate").value("")
.endObject()
.endObject();
URL url = new URL("http://origin.staging.scion.com/PE/service/rest?_wadl&_type=xml/getit");
URLConnection urlConn = url.openConnection();
if (!(urlConn instanceof HttpURLConnection)) {
throw new IOException ("URL is not an Http URL");
}
Map<String, String> kvPairs = new HashMap<String, String>();
kvPairs.put("req", vehicle.toString());
HttpURLConnection httpConn = (HttpURLConnection)urlConn;
//httpConn.setRequestMethod("");
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("POST");
httpConn.setDoInput(true);
httpConn.addRequestProperty("user-agent", "Yoda");
httpConn.addRequestProperty("Accept","application/json");
httpConn.addRequestProperty("Content-type", "application/x-www-form-urlencoded");
httpConn.connect();
Toast.makeText(getApplicationContext(),kvPairs.toString(), Toast.LENGTH_LONG).show();
resCode = httpConn.getResponseCode();
Toast.makeText(getApplicationContext(),httpConn.getResponseCode()+"", Toast.LENGTH_LONG).show();
if (resCode == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
請指教。
,你得到什麼錯誤或異常?請求是否到達服務器端代碼? – 2011-05-24 18:34:29
在這個實例中,我收到錯誤500. – 2011-05-24 18:47:03
@ MR Mido您在Java代碼中構建的JSON對象似乎與您發佈的代碼片段(已無效的JSON)相匹配。 – 2011-05-24 18:51:41