【问题标题】:How to call the JSON webservice?如何调用 JSON 网络服务?
【发布时间】:2014-09-24 11:28:11
【问题描述】:

我正在尝试调用此 Web 服务,但它的响应为空

<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");

请有人帮助我如何调用此网络服务并获得响应?

【问题讨论】:

  • 网络服务怎么样?我所看到的只是一个 javascript 脚本。 HttpClient 不会调用 javascript
  • 请告诉我怎么称呼它

标签: android json web-services


【解决方案1】:

试试这个代码。首先创建你的 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");

希望对你有帮助。

【讨论】:

    【解决方案2】:
    URL url = new URL("http://www.example.com/");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try {
        urlConnection.setDoOutput(true);
        urlConnection.setChunkedStreamingMode(0);
    
        OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
        writeStream(out);
    
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        readStream(in);
    } finally {
        urlConnection.disconnect();
    }
    

    实现方法writeStream将内容写入POST数据,实现readStream从服务器读取响应数据

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多