1

I have tried to get post response like

StringEntity  entity;
private static final int TIMEOUT_MILLISEC = 20000;
private static final String serverurl = "http://appcodetechnology.net/meravenue/api/";

below function m calling

  post("master_details.php");

//

public String post(String request)
    {
        String result = null;
        try
        {   
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
            HttpClient client = new DefaultHttpClient(params);
            System.out.println("Getting url: "+request);
            HttpPost post = new HttpPost(serverurl+request);

             String body  = "{\"action\":\"get_all_cities\"}";

                try {
                    //entity = new StringEntity("action=" + java.net.URLEncoder.encode(body, "utf-8"));                     
                    //entity = new StringEntity("data=" + java.net.URLEncoder.encode(body, "utf-8"));                       
                     entity = new StringEntity(java.net.URLEncoder.encode(body, "utf-8"));                          
                     entity.setContentType("application/x-www-form-urlencoded");

                } catch (UnsupportedEncodingException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
               // entity.setContentType("application/x-www-form-urlencoded");
                post.setEntity(entity);


            HttpResponse response = client.execute(post);
            HttpEntity entity = response.getEntity();
            if(entity != null)
            {
                result = EntityUtils.toString(entity);
            }
            else
            {
                Log.d("Response Failed","Response from server is failed");
            }

        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return result;
    }

and response getting

{"result":false,"message":"Invalid method name.","error_code":"406"}

no any error i m getting.

for below API i m working

URL : http://appcodetechnology.net/meravenue/api/master_details.php
Method : POST
Request Body : {"action":"get_all_cities"}

but not getting response

Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45

1 Answers1

1

Use this to post a json object:

                JSONObject json = new JSONObject();
                json.put("YOUR_KEY", YOUR_VALUE);
                post.setEntity(new ByteArrayEntity(json .toString().getBytes("UTF8")));

Or just ref this page to find how to post json:

How to send a JSON object over Request with Android?

Community
  • 1
  • 1
kvh
  • 2,118
  • 19
  • 29