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?
Asked
Active
Viewed 3,161 times
-1
-
What is the size of the URL for the GET? – Alexandre Santos Aug 01 '14 at 07:36
-
http://stackoverflow.com/questions/3091485/what-is-the-limit-on-querystring-get-url-parameters – Tobi Aug 01 '14 at 07:38
2 Answers
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
-
-
Im not trying to to this in android. I want to do this in pure java.:) – SHdinesh Madushanka Aug 01 '14 at 09:00