将 Json 发布到 Restful Web 服务,获取 HTTP 200(正常),但它只返回我的原始 Json 对象

发布于 2024-11-09 06:01:41 字数 2708 浏览 0 评论 0原文

所以我将此 json 发布到这个宁静的 Web 服务,并且发布成功(http 代码:200),但是我得到了发布的原始 Json 对象作为响应。这是正常行为吗?

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
        }  

任何想法都非常受欢迎。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

虚拟世界 2024-11-16 06:01:41

您在 BufferedReader 中读取的内容是原始请求的实体:

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

上面的行是违规者。您想要从响应中读取实体,如下所示:

BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

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()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文