Reputation: 1379
I am facing the problem where i am sending an jsonstring, but the string is empty.
When i use this php code to see if i am recieving a string and what i get:
POST:
Array
(
)
GET:
Array
(
[data] =>
)
This is my Android code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myserver.nl/locatie.php?data=");
httppost.setHeader("Content-type", "application/json");
// Create json object here...
json = new JSONObject();
try {
json.put("id", "0612838");
json.put("longitude", "-143.406417");
json.put("latitude", "32.785834");
json.put("timestamp", "10-10 07:56");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/// create StringEntity with current json obejct
try {
se = new StringEntity(json.toString());
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
try {
response = httpclient.execute(httppost);
String temp = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Does anybody see what i am doing wrong?
Upvotes: 0
Views: 590
Reputation: 77904
Use NameValuePair
:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("http://www.myserver.nl/locatie.php");
json = new JSONObject();
try {
json.put("id", "0612838");
json.put("longitude", "-143.406417");
json.put("latitude", "32.785834");
json.put("timestamp", "10-10 07:56");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/// create StringEntity with current json obejct
try {
se = new StringEntity(json.toString());
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("data", se));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
System.out.println("send about to do post");
HttpResponse response = httpclient.execute(httpost);
System.out.println("send post done");
HttpEntity entity = response.getEntity();
....
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Server side
if (isset($_POST["data"]))
{
// Takes a JSON encoded string and converts it into a PHP variable.
$sub = json_decode($_POST"data"],true);
.....
}
Upvotes: 1