【发布时间】:2011-03-29 08:57:44
【问题描述】:
因为我试图发布评级活动的结果。从用户那里读取评分值后,我通过 HTTP POST 调用 Web 服务并在其中设置 JSON 对象。由于我的网络服务器需要身份验证,我也能够验证这一点。
但真正的问题是响应代码应该是 200 但我收到 201。请告诉我哪里出错了!
代码:
public static String sendJson(final int rating, final String url) {
Thread t = new Thread() {
public void run() {
Looper.prepare(); // For Preparing Message Pool for the child
HttpResponse response;
// proxy
final String PROXY = "xxx.xxx.xxx.xxx";
// proxy host
final HttpHost PROXY_HOST = new HttpHost(PROXY, 8080);
HttpParams httpParameters = new BasicHttpParams();
mHttpClient = new DefaultHttpClient(httpParameters);
HttpConnectionParams.setConnectionTimeout(mHttpClient.getParams(),
10000); //Timeout Limit
mHttpClient.getParams().setParameter(
ConnRoutePNames.DEFAULT_PROXY, PROXY_HOST);
System.out.println("Sending proxy request: " + mHttpClient);
// mHttpClient = new DefaultHttpClient();
JSONObject json = new JSONObject();
try {
String reqUrl = new String(aBaseUrl + url);
HttpPost post = new HttpPost(reqUrl);
post.getParams().setParameter(
ConnRoutePNames.DEFAULT_PROXY, PROXY_HOST);
post.addHeader(BasicScheme
.authenticate(new UsernamePasswordCredentials(
userName, password), "UTF-8", false));
System.out.println("Sending Post proxy request: " + post);
json.put("rating", rating);
// json.put("password", pwd);
// StringEntity se = new StringEntity( "JSON: " +json.toString());
StringEntity se = new StringEntity(json.toString());
System.out.println("JSON VALUE 2222 " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
// post.setEntity(new
// ByteArrayEntity(json.toString().getBytes("UTF8")));
post.setEntity(se);
response = mHttpClient.execute(post);
/* Checking response */
statusCode = response.getStatusLine().getStatusCode();
System.out.println("Http Execute finish " + statusCode);
if(statusCode==200)
{
HttpEntity entity = response.getEntity();
String getResponseText = EntityUtils.toString(entity);
System.out.println(" Post Response Text from Server : "
+ getResponseText);
}
if (response != null) {
// InputStream in = response.getEntity().getContent();
// //Get the data in the entity
// HttpEntity entity = response.getEntity();
// String getResponseText =
// EntityUtils.toString(entity);
// System.out.println(" Post Response Text from Server : "
// + getResponseText);
}
} catch (Exception e) {
e.printStackTrace();
// createDialog("Error", "Cannot Estabilish Connection");
}
Looper.loop(); // Loop in the message queue
}
};
t.start();
return url;
}
【问题讨论】: