-1

I have a json array prepared for send to a php server. When I tried to send it by GET method it tells the URL is too long. So I decided to send it by POST. I would like to know is there any way to do it successfully?

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64

2 Answers2

0

As you give no details about any library you use, this is somehow vage. But you could have a look at http://www.mkyong.com/webservices/jax-rs/restful-java-client-with-jersey-client/ under point 3. to see how this can be achieved.

Tobi
  • 31,405
  • 8
  • 58
  • 90
-1

i used this code to send my json string

 private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {



            return POST(urls[0]);
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
       }
    }
    public static String POST(String url){
    InputStream inputStream = null;
    String result = "";
    try {

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = "what ever your string is";





     // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the content   
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // 10. convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }
   // 11. return result
    return result;

  }

    private static String convertInputStreamToString(InputStream inputStream) throws IOException   
    {
    // TODO Auto-generated method stub

        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
      String line = "";
      String result = "";
       while((line = bufferedReader.readLine()) != null)
        result += line;

        inputStream.close();
        return result;

    }



}

use this to execute request

new HttpAsyncTask().execute("your URL");
jaimin
  • 563
  • 8
  • 25