Fetch Data that sent from android application with the help of JSONObject - php

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

Related

android How to send POST json request to Laravel Framework

Hi im doing both android and laravel framework..
but when i do post method and send json to laravel api via POST.. it will give a 500 result.. please help..
this is one of my asynctask in android sending the post data.. assume sendJsonObj is a jsonobject with values..
URL url = new URL("http://dexter-laravelframe.rhcloud.com/register");
urlConnect = (HttpURLConnection) url.openConnection();
urlConnect.setRequestMethod("POST");
urlConnect.setConnectTimeout(10000);
urlConnect.setRequestProperty("Content-Type", "application/json");
urlConnect.setRequestProperty("x-csrf-token", Model_Application.token_csrf);
urlConnect.connect();
OutputStreamWriter outWrite = new OutputStreamWriter(urlConnect.getOutputStream());
outWrite.write(sendJsonObj.toString());
outWrite.flush();
outWrite.close();
int code = urlConnect.getResponseCode();`
on my Laravel framework..
routes.php
Route::post('/register', 'UserController#register');
UserController.php
public function register()
{
$json = Input::json()->all();
$resultJson = json_encode($json);
echo $resultJson;
}
this will give a 500 response code from Laravel api.. i want it to read the json data.. and send it back.. if i used Mozilla plugin rest client, and send the json data in body.. it gives 200..

decode json object sent form android app to php server

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:)

Post data more than one record from android app to php webservice

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();

How to insert data on to the server in Android Application

I am developing an android application where the user data should be pushed onto the server .How can i do that ? I was able to get data from the server but unable to insert it. Code snippet would be good.I have tried this but it doesn't work.
Java Code :
String data="xyz.org/json.php"+"?"+"&"+URLEncoder.encode("data", "UTF-8") + "="+"order";
URL url;
url = new URL(data);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
In json.php :
$data = urldecode($_GET['data']);
if($data=="order")
{
$query="insert into xyz values('$x','$y','$z')";
$query_run=mysql_query($query);
}
you need a httppost or httpget dependance on the your url method for that you can use Httppost and get follow this url you get some idea and also check this url for asynctask beacuse you have to put all the network related you have to put in the asynctask.

Handling JSON POST request in PHP

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);

Categories