Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing HttpClient and HttpPost in Android with post parameters
    primarykey
    data
    text
    <p>I'm writing code for an Android application that is supposed to take data, package it as Json and post it to a web server, that in turn is supposed to respond with json.</p> <p>Using a GET request works fine, but for some reason using POST all data seems to get stripped and the server does not receive anything.</p> <p>Here's a snippet of the code:</p> <pre><code>HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 5000); DefaultHttpClient httpClient = new DefaultHttpClient(params); BasicCookieStore cookieStore = new BasicCookieStore(); httpClient.setCookieStore(cookieStore); String uri = JSON_ADDRESS; String result = ""; String username = "user"; String apikey = "something"; String contentType = "application/json"; JSONObject jsonObj = new JSONObject(); try { jsonObj.put("username", username); jsonObj.put("apikey", apikey); } catch (JSONException e) { Log.e(TAG, "JSONException: " + e); } HttpPost httpPost = new HttpPost(uri); List&lt;NameValuePair&gt; postParams = new ArrayList&lt;NameValuePair&gt;(); postParams.add(new BasicNameValuePair("json", jsonObj.toString())); HttpGet httpGet = null; try { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParams); entity.setContentEncoding(HTTP.UTF_8); entity.setContentType("application/json"); httpPost.setEntity(entity); httpPost.setHeader("Content-Type", contentType); httpPost.setHeader("Accept", contentType); } catch (UnsupportedEncodingException e) { Log.e(TAG, "UnsupportedEncodingException: " + e); } try { HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); if (httpEntity != null) { InputStream is = httpEntity.getContent(); result = StringUtils.convertStreamToString(is); Log.i(TAG, "Result: " + result); } } catch (ClientProtocolException e) { Log.e(TAG, "ClientProtocolException: " + e); } catch (IOException e) { Log.e(TAG, "IOException: " + e); } return result; </code></pre> <p>I think I have followed the general guidelines on how to create the parameters and post them, but apparently not.</p> <p>Any help or pointers to where I can find a solution, are very welcome at this point (after spending a few hours realizing no post data was ever sent). The real server is running Wicket on Tomcat, but I've also tested it out on a simple PHP page, with no difference.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload