我正在尝试调用此Web服务,但其响应为null
<script type="text/javascript">
jQuery(document).ready(function(){
var user = {nick :"rajesh", password:"123456", device_id:"123456677"};
jQuery.ajax({
type: "post",
data :JSON.stringify(user),
url: "http://localhost:81/sazpin/user/users/index.json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
console.log(data);
}
});
});
</script>
我的httppost客户端是
HttpClient httpClient = new DefaultHttpClient();
// post header
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
请有人帮助我。如何调用此Web服务并获得响应?
试试这个代码。首先创建您的AsyncTask。
public class ResquestJSON extends AsyncTask<String, Void, String>{
JSONObject jsonObject = new JSONObject();
String response;
protected String doInBackground(String... params){
HttpPost httpPost = new HttpPost("http://192.168.10.101:8090/download");//your webservice url here
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
try {
jsonObject.put("firstParam", params[0]);
jsonObject.put("secondParam", params[1]);
StringEntity se = new StringEntity(jsonObject.toString());
httpPost.addHeader("Content_Type", "application/json");
httpPost.setEntity(se);
HttpResponse httpResponse = httpClient.execute(httpPost, httpContext);
HttpEntity entity = httpResponse.getEntity();
if (entity!=null){
response = EntityUtils.toString(entity);
entity.consumeContent();
httpClient.getConnectionManager().shutdown();
}
} catch (JSONException|IOException|IllegalArgumentException e) {
return UtilityConstants.ERROR;
}
return response;
}
现在从这样的活动中调用此AsyncTask方法。
String response = new ResquestJSON().execute(firstParam, secondParam).get();
JSONObject obj = new JSONObject(response);
String nick = obj.getString("nick");
String password = obj.getString("password");
String deviceId = obj.getString("deviceId");
希望对您有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句