Using HttpClient and HttpPost in Android with post parameters
You can actually send it as JSON the following way:
// Build the JSON object to pass parametersJSONObject jsonObj = new JSONObject();jsonObj.put("username", username);jsonObj.put("apikey", apikey);// Create the POST object and add the parametersHttpPost httpPost = new HttpPost(url);StringEntity entity = new StringEntity(jsonObj.toString(), HTTP.UTF_8);entity.setContentType("application/json");httpPost.setEntity(entity);HttpClient client = new DefaultHttpClient();HttpResponse response = client.execute(httpPost);
have you tried doing it without the JSON object and just passed two basicnamevaluepairs?also, it might have something to do with your serversettings
Update:this is a piece of code I use:
InputStream is = null;ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("lastupdate", lastupdate)); try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(connection); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); Log.d("HTTP", "HTTP: OK"); } catch (Exception e) { Log.e("HTTP", "Error in http connection " + e.toString()); }
I've just checked and i have the same code as you and it works perferctly.The only difference is how i fill my List for the params :
I use a : ArrayList<BasicNameValuePair> params
and fill it this way :
params.add(new BasicNameValuePair("apikey", apikey);
I do not use any JSONObject to send params to the webservices.
Are you obliged to use the JSONObject ?