1

I am very newbie to android and trying to consume a .net weservice by sending a json object.

I am creating a json object and adding all the parameters to it like

 JSONObject json = new JSONObject();

   json.put("name","koli"); 
   json.put("email","[email protected]");

And calling the request in doInBackground method of AsyncTask class

String response = HttpClient.SendHttpPost(params[0],json);

This is how i am trying to making the request

public static String SendHttpPost(String URL, JSONObject jsonData) {

        Log.d("jsonData", ""+jsonData);
        StringBuilder stringBuilder = new StringBuilder();
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(URL);
        httpPostRequest.setHeader("User-Agent", "com.altaver.android_PostJson2");
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-Type", "application/json");
       StringEntity se = null;
        try {
            se = new StringEntity(jsonData.toString(),"UTF-8");

            se.setContentType("application/json");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        httpPostRequest.setEntity(se);
        HttpResponse response = null;
        try {
            response = client.execute(httpPostRequest);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

In the above method what does

StringEntity se = null;
            try {
                se = new StringEntity(jsonData.toString(),"UTF-8");

                se.setContentType("application/json");
            } catch (UnsupportedEncodingException e) {

this snippet will do.

and why i am not able to send my jsonobject?

Thanks:)

4
  • The code is correct, what's the problem? Commented Oct 8, 2013 at 7:48
  • @shem JSON Object which i am sending from android is NULL at service side Commented Oct 8, 2013 at 8:06
  • Do the jsonData looks ok when you print it in the Log? Commented Oct 8, 2013 at 8:22
  • @shem yea it is proper Commented Oct 8, 2013 at 8:34

2 Answers 2

2

Try this.. in your doInBackground

JSONObject jObjOut = null;

                try {
                    HttpPost request = new HttpPost(url);
                    JSONObject returnedJObject;
                    returnedJObject = new JSONObject(JSONdata.toString());
                    JSONStringer json = new JSONStringer();
                    StringBuilder sb=new StringBuilder();


                    if (returnedJObject!=null)
                    {
                        Iterator<String> itKeys = returnedJObject.keys();
                        if(itKeys.hasNext())
                            json.object();
                        while (itKeys.hasNext())
                        {
                            String k=itKeys.next();
                            json.key(k).value(returnedJObject.get(k));
                            //Log.e("keys "+k,"value "+returnedJObject.get(k).toString());
                        }             
                    }
                   json.endObject();


                   StringEntity entity;

                   entity = new StringEntity(json.toString());

                   entity.setContentType("application/json;charset=UTF-8");
                  // entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
                   request.setHeader("Accept", "application/json");
                   request.setEntity(entity);

                   HttpResponse response =null;
                   DefaultHttpClient httpClient = new DefaultHttpClient();

                   HttpConnectionParams.setSoTimeout(httpClient.getParams(), 30000);
                   HttpConnectionParams.setConnectionTimeout(httpClient.getParams(),50000);

                   response = httpClient.execute(request);

                   InputStream in;
                   in = response.getEntity().getContent();

                   BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                   String line = null;
                   while((line = reader.readLine()) != null){
                       sb.append(line);
                   }


                   return new JSONObject(sb.toString());  
Sign up to request clarification or add additional context in comments.

Comments

1

try chaging:

se = new StringEntity(jsonData.toString(),"UTF-8");

to this:

se = new StringEntity(jsonData.toString());

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.