2

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?

2
  • 1
    How is that a webservice? All I see is a javascript script. HttpClient will not call javascript Commented Sep 24, 2014 at 11:30
  • please tell me how to call this Commented Sep 24, 2014 at 11:43

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.