发布一个JSON到一个RESTful Web服务,得到了HTTP 200(OK),但它只是返回我原来的JSON对象 [英] Posting a Json to a Restful webservice, getting the HTTP 200 (OK) but it returns just my original Json object
本文介绍了发布一个JSON到一个RESTful Web服务,得到了HTTP 200(OK),但它只是返回我原来的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我张贴此JSON这个RESTful Web服务和职位是成功的(HTTP code:200),但是我回来作为响应原始JSON对象的发布。这是一个普通的行为呢?
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.
推荐答案
你所读的的BufferedReader
是原始请求的实体:
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()));
这篇关于发布一个JSON到一个RESTful Web服务,得到了HTTP 200(OK),但它只是返回我原来的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文