将 Json 发布到 Restful Web 服务,获取 HTTP 200(正常),但它只返回我的原始 Json 对象
所以我将此 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在 BufferedReader 中读取的内容是原始请求的实体:
上面的行是违规者。您想要从响应中读取实体,如下所示:
What you are reading in your
BufferedReader
is the entity of the original request:The above line is the offender. You want to read in the entity from the response, like this: