gpt4 book ai didi

android - 将 Json 发布到 Restful web 服务,获取 HTTP 200(OK)但它只返回我原来的 Json 对象

转载 作者:行者123 更新时间:2023-11-29 00:46:24 25 4
gpt4 key购买 nike

所以我将这个 json 发布到这个 restful 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
}

任何想法都非常受欢迎。

最佳答案

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

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

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

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

关于android - 将 Json 发布到 Restful web 服务,获取 HTTP 200(OK)但它只返回我原来的 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6118768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com