You can try to add the encoding in post.setEntity() like this...
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
every thing else looks fine to me. If this doesn't work then you can also try to use the JSON library in android to directly encode it and pass that data to the page using
HttpPost httppost = new HttpPost("http://your/url/path/");
httppost.addHeader("Content-Type", "application/json");
httppost.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
Then in php you can access it by this code
$json = file_get_contents('php://input');
$obj = json_decode($json);
$name = $obj->{'name'};
Or
You can also try to make a get request or accessing data through $_REQUEST in your php file.
Whatever you are using is exactly what is required.
or
Try this this is a working code... It is a class which you can directly use to pass data to your file. Call the function statically using :: , and import all the required json clases
public class RestClient {
private static String convertStreamToString(InputStream is) {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static String connectSend(){
/*
HttpParams httpparams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpparams, 30000);
HttpConnectionParams.setSoTimeout(httpparams, 30000);
*/
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://path/to/your/php/file");
httppost.addHeader("Content-Type", "application/json");
JSONObject json = new JSONObject();
try {
json.put("item0", "data0");
json.put("item1", "data1");
json.put("item2", "data2");
json.put("item3", "data3");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
httppost.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
//httppost.setHeader("json", json.toString());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String result = RestClient.convertStreamToString(instream);
return result;
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "false";
}