Rahul Matte
Rahul Matte

Reputation: 1181

How to call the JSON webservice?

I am trying to call this web service but it's response is 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>

My httppost client is

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

Please somebody help me How to call this webservice and get the response?

Upvotes: 2

Views: 101

Answers (2)

user123
user123

Reputation: 1092

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();
}

Implement methods writeStream to write content as POST data, and readStream to read response data from server

Upvotes: 0

Crawler
Crawler

Reputation: 2018

Try this code. First create your 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;
}

Now Call this AsyncTask Method from your activity like this.

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

Hope it might help you.

Upvotes: 1

Related Questions