当我试图发布评级活动的结果时。从用户读取评级值后,我正在调用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;
}
答案 0 :(得分:7)
201是正常的响应代码,表示“已创建”。您的Web服务报告成功,这是正常的。 HTTP规范定义了此代码。见http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html