Http Post Json对Restful webservice没有成功

时间:2011-05-24 18:31:18

标签: android json http rest post

我需要将这个json发布到服务器,有人有解决方案吗?

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();                                 
}

请咨询。

1 个答案:

答案 0 :(得分:0)

问题是这里没有构建Json对象是一个示例来证明:

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();