I am sending post request from android to the url using httpclient like this one below –
String url = “http://www.myurl.com/test.php?format=json”;
Now I am getting response back in form of plist instead of json, which is default type of response from php file if no format query string is described in url.
Can we specify query string in url while posting? It looks like php file is not getting format query string.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("method", "add"));
nameValuePairs.add(new BasicNameValuePair("device_id", "123"));
nameValuePairs.add(new BasicNameValuePair("device_token", "3221"));
nameValuePairs.add(new BasicNameValuePair("session_id", "1212"));
nameValuePairs.add(new BasicNameValuePair("event_id","12345" ));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url); httppost.addHeader("Content-Type","application/x-www-form-urlencoded");
httppost.addHeader("Accept","application/json");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpclient.execute(httppost);
/* Checking response */
if (response != null) {
InputStream in = response.getEntity().getContent();
String a = convertStreamToString(in);
Log.i("Read from Server "+response.getStatusLine().getStatusCode(), a);
}else{
Log.i("response is ", "null");
}
Thanks
It’s impossible to know without seeing your server code, but your server may not like that you’re combining HTTP POST parameters in your request body with GET parameters in the URL. Try putting
into the section where you’re building up the request parameters instead and see what happens.