I am sending json object from android as such:
//Create JSONObject here
JSONObject json = new JSONObject();
json.put("key", String.valueOf(args[0]));
String postData=json.toString();
// Send POST output.
printout = new DataOutputStream(urlConn.getOutputStream ());
printout.writeUTF(URLEncoder.encode(postData,"UTF-8"));
Log.i("NOTIFICATION", "Data Sent");
printout.flush ();
printout.close ();
When it is sent to the server it looks like the following code snippet. ???%7B%22key%22%3A%22value%22%7D
I should add the first ??? are in a diamond each. When I decode the whole json object I get null. In the php server I have
$somevar=json_decode(json, true);
which returns null. Can someone point me on how to retrieve the json value? Thanks so much:)
Related
I am working on android app with api developed in PHP. I am trying to send an object to api, so I am serializing it with Gson library, and trying to deserialize in PHP code using Unserialize() function. But, its giving error as (!)Notice: unserialize(): Error at offset 0 of 468 bytes in C:\wamp64\www\digiclass\admin\api\upload_pending_results.php on line 5 and finally giving json response as {"error":false,"data":false} Here is my PHP code:
<?php
//an array to display response
$response = array();
$serObject = $_POST['serObject'];
$response['error'] = false;
$response['data'] = unserialize($serObject);
echo json_encode($response);
?>
currently, I am not performing any operation on data, rather only want to see if the data is deserializing correctly. I have tried to see the serialized object, it is converted as below.
%7B%22accessToken%22%3A%22b2c1f3d5e8218cfa81d23c4b9b7d6cbc20f82c67ca4b9d92be9bb7680031360a9d95bf1e3ecf42678f1c8b87a4eb5622%22%2C%22clas%22%3A%229%22%2C%22dbname%22%3A%22a_new%22%2C%22records%22%3A%5B%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%222%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A1%7D%2C%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%227%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A2%7D%2C%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%223%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A3%7D%2C%7B%22clas%22%3A%229%22%2C%22id%22%3A1%2C%22marks%22%3A%228%2F8%22%2C%22rollno%22%3A%2212%22%2C%22subject%22%3A%22Maths%22%2C%22temp%22%3A4%7D%5D%2C%22rollno%22%3A%2212%22%7D
I am not sure if the conversion is going right or what the error might be. Can anyone help?
The value you are getting is a URL encode JSON string, so first decode it...
$serObject = urldecode($serObject);
and then decode it as JSON...
$response['data'] = json_decode($serObject, true);
Hello all I am creating an android application and using HttpURLConnection Class. Here urlConn is object of HttpURLConnection class.
JSONObject jo = new JSONObject();
jo.put("name","xyz");
jo.put("email","xyz#abc.com")
I am sending this using..
OutputStreamWriter out = new OutputStreamWriter(urlConn.getOutputStream());
out.write(jo.toString());
out.close();
Now i want to fetch this data into php page that i have hosted online.
So please tell me how to fetch JSONObject data in to php page.
Read about json decode :
http://php.net/manual/en/function.json-decode.php
SO dicsussion :
Parsing JSON object in PHP using json_decode
One of my partners has an API service which should send an HTTP POST request whenever a new file is published. This requires me to have an api file which will get the POST this way:
http://myip:port/api/ReciveFile
and requesting that the JSON format request should be:
{
"FILE ":"filename.zip",
"FILE_ID":"123",
"FILE_DESC":"PRUPOUS_FILE",
"EXTRAVAR":"",
"EXTRAVAR2":"",
"USERID":" xxxxxxxxxxxx",
"PASSWORD":"yyyyyyyyyyy"
}
Meanwhile it should issue a response, in JSON format if it got the file or not
{"RESULT_CODE":0,"RESULT_DESCR":""}
{"RESULT_CODE":1001,"RESULT_DESCR":"Bad request"}
And after, when I am finished elaborating the file, I should send back the modified file same way.
The question is, now basically from what I understand he will send me the variables witch I have to read, download the file, and send a response back.
I am not really sure how to do this any sample code would be welcomed!
I'm not sure exactly what the question is, but if you mean creating a success response in JSON for if an action occurred while adding data to it which is what can be understood from the question, just create an array with the values you wish to send back to the provider and do json_encode on the array which should create json and just print it back as a response.
About receiving the information; all you have to do is use the integrated curl functions or use a wrapper (Guzzle, etc) to output the raw JSON or json_decode data into a variable and do whatever you please with it.
From what I read in the question, you wish to modify the contents of it. This can be achieved by just decoding the json and changing the variables in the array, then printing the modified JSON back as a response.
Example (this uses GuzzleHTTP as an example):
$res = $client->request('GET', 'url');
$json = $res->getBody();
$array = json_decode($json, 1); // decode the json
// Start modifying the values or adding
$array['value_to_modify'] = $whatever;
$filename = $array['filename']; // get filename
// make a new request
$res = $client->request('GET', 'url/'.$filename);
// get the body of specified filename
$body = $res->getBody();
// Return the array.
echo json_encode($body);
References:
http://docs.guzzlephp.org/en/latest/
I have table called GRN and in that so many columns are there with lots of data.
Now i want to sync that GRN data with the server's database.
So we can say whatever data we have in android app for GRN that i need to insert in server's database during sync.
I know how to pass 1 record in post but i want to pass all the data in post during web service calling from Android app.
How to post data to a webservice using json
In this link we can see how to pass some parameter in post.
Does anybody know?
Try putting all the data in a Json string (use a library, like org.json). Then you will need to parse it on the server side.
With org.json, you will simply do something like this:
JSONObject o = new JSONObject();
o.put("key1", "value1");
o.put("key2", "value2");
String dataToSend = o.toString();
To make it an array, do something like this:
JSONArray a = new JSONArray();
for([Some loop]){
JSONObject o = new JSONObject();
o.put("key", "value");
a.put(o);
}
String dataToSend = a.toString();
I am currently developing Android application, which will uses web services. I used PHP for backend. I am currently trying authentication via JSON to PHP. But I am stuck at some point, hope u guys will help.
I successfully write code to create JSON data in android also db connections in php using mysql, but i am confusing about how to handle JSON data. I am using POST request for sending JSON data.
I like to ask how i handle JSON data in PHP. More specific, I like to know how to grab POST request in PHP which contain JSON data??
Thanks in advance.
Thanking you.
EDIT:
I am using following code for sending POST request in android
HttpPost post = new HttpPost(address);
json.put("username", username);
json.put("password", pwd);
StringEntity se = new StringEntity("json"+json.toString());
Log.i(DEB_TAG, "The JSON Request is:"+json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
Log.i(DEB_TAG, "The post request is "+post.toString());
response = client.execute(post);
if(response != null){
InputStream in = response.getEntity().getContent();
Log.i(DEB_TAG, "The result is"+in.toString());
}
and using following code for parsing JSON request in php:
$string = $_POST['josnHeader'];
$obj = json_decode($string);
$username = $obj->{'username'};
$password = $obj->{'password'};
Is it correct or I am doing any wrong implementation??
Have you taken a look in your $_POST array in PHP?
$json = $_POST["var_name"];
$array = json_decode($json);
$json = $_REQUEST["your_param"];
$dtoObject = json_decode(stripslashes($json),true);