Posting a Json to a Restful webservice, getting th

2020-07-23 20:20发布

SO I post this json to this restful web service and the post is successful (http code:200) however I get back as a response the original Json object that posted. Is this a regular behavior ?

InputStream in = null;
    int resCode;
    String text;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);




     // POST request to <service>/SaveVehicle
        HttpPost request = new HttpPost("http://origin.staging.scion.com/PE/service/rest/getit");
        //request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");
        request.setHeader("user-agent", "Yoda");
        try {
        // Build JSON string
        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();

        StringEntity entity = new StringEntity(vehicle.toString());


        request.setEntity(entity);

        // Send request to WCF service
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request); 
        resCode = response.getStatusLine().getStatusCode();
        Toast.makeText(getApplicationContext(),response.getStatusLine().getStatusCode()+"", Toast.LENGTH_LONG).show();   

        if (resCode == 200) {

            Toast.makeText(getApplicationContext(),response.getStatusLine().getStatusCode()+"", Toast.LENGTH_LONG).show();   
            BufferedReader in = 
                new BufferedReader(new InputStreamReader(entity.getContent()));
            String line="";
            StringBuffer returnFromServer = new StringBuffer();

            while ((line=in.readLine())!=null) 
            {
                returnFromServer.append(line);
            }
            //Toast what we got from server
            Toast.makeText(getApplicationContext(),returnFromServer.toString(), Toast.LENGTH_LONG).show();   


            if (entity != null) 
            {
                entity.consumeContent();
            }

        }  
        }catch (Exception e) {
            // TODO: handle exception
        }  

any Ideas are more than welcomed.

1条回答
来,给爷笑一个
2楼-- · 2020-07-23 21:14

What you are reading in your BufferedReader is the entity of the original request:

BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));

The above line is the offender. You want to read in the entity from the response, like this:

BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
查看更多
登录 后发表回答