Reputation: 1774
i am trying to build a web service for my android app. But i am having problem to sending parametres to php. I am using these code blocks to Http-Post.
public String requestJson(String method, JSONObject parameters) {
try {
int TIMEOUT_MILLISEC = 10000;
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
String tmpUrl = serviceUrl.trim() + (serviceUrl.trim().endsWith("/") ? method : "/" + method);
HttpPost request = new HttpPost(tmpUrl);
request.setHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8");
if (parameters != null)
{
request.setEntity(new StringEntity(parameters.toString(), HTTP.UTF_8));
}
HttpResponse response = client.execute(request);
final int statusCode = response.getStatusLine().getStatusCode();
final String jsonResponse = EntityUtils.toString(response.getEntity());
if (statusCode != HttpStatus.SC_OK) {
Log.w(TAG, "Error in web request: " + statusCode);
Log.w(TAG, jsonResponse);
return null;
} else {
Log.i(TAG, jsonResponse);
return jsonResponse;
}
} catch (Exception e) {
Log.e(TAG, "Error in fetching JSON due to -> " + e.toString());
}
return null;
}
public String testFunction() {
try {
JSONObject param = new JSONObject();
param.put("testID", 123);
JsonCevabi = requestJson("test.php", param);
if (JsonCevabi != null) {
JSONObject jo = new JSONObject(JsonCevabi);
JSONArray ja = new JSONArray(jo.getString("d"));
@SuppressWarnings("unchecked")
List<SampleEntity> resultList = (List<SampleEntity>) new Gson().fromJson(ja.toString(), new TypeToken<List<SampleEntity>>() {
}.getType());
return JsonCevabi;
}
else
return null;
} catch (Exception e) {
return null;
}
}
And my php code like this;
<?php
$id = json_decode($_POST['testID']);
$json = "{d:[{sample1:'".$id."',sample2:'value12'}]}";
$response = $_GET["callback"] . $json;
echo $response;
?>
What i am trying to do is sending TestID parametre to php. And i am expenting to return me to this value.
But i am getting {d:[{sample1:'',sample2:'value12'}]}
What i want is {d:[{sample1:'123',sample2:'value12'}]}
What i am missing?
EDIT: i found similar question but did not help; Android JSON HttpClient to send data to PHP server with HttpResponse
Upvotes: 0
Views: 2542
Reputation: 7862
@Java Code to send data:
StringEntity entity = new StringEntity("jsonRequest="+data.toString(), HTTP.UTF_8);
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
HttpEntity entityPost = response.getEntity();
result = EntityUtils.toString(entityPost);
return result;
@PHP code to receive data
$jsonRequest = json_decode($_REQUEST['jsonRequest'], true);
@check if the data is coming we can log the contents
date_default_timezone_set('EST');
ini_set("log_errors", 1);
ini_set("error_log", "site.log");
error_log( "Hello, errors!".print_r($jsonRequest[id],1) );
Upvotes: 1
Reputation: 1774
Changing php code to following solved my problem.
<?php
$postdata = $HTTP_RAW_POST_DATA;
$data = json_decode($postdata);
$id = $data->testID;
$json = "{d:[{sample1:'".$id."',sample2:'value12'}]}";
$response = $_GET["callback"] . $json;
echo $response;
?>
More detailed information can be found link above.
Upvotes: 1
Reputation: 10733
As @chandresh_cool said there is a problem with POST paramethers.
You should edit your StringEntity like this:
new StringEntity("testID="+parameters.toString(), HTTP.UTF_8)
.
Then in your server the $id variable contains the decoded JSON, so you should do:
$id = json_decode($_POST['testID'], true);
$json = "{d:[{sample1:'".$id['testID']."',sample2:'value12'}]}";
Upvotes: 2
Reputation: 11830
Looks like
$_POST['testID']
is a problem. Are you sending variable using post method surely?
Upvotes: 1