开发者

Http Post Json to Restful webservice not being successful

I need to post this json to a serve开发者_如何学Pythonr, does anybody have a solution to this ?

Var req = 
        { 
        getItInputTO =
            {
            “zipCode”:”(value)”
            "financingOption":"(value)",
            “make":"(value)”,   
                "baseAmountFinanced":"(value)",
                "modelYear":"(value)",
                "trimCode":"(value)",
                "totalMSRP":"(value)",
                “aprRate”: "(value)" ( 
    }
        }

I have used this code but so far I can't seem to get a response:

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

PLEASE ADVICE.


The issue is that the Json object wasn't constructed right here is a sample to demonstrate that:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜