I have an algorithm about chat program in android, but I have a problem in server-side section.
I can store my data like username, password and e-mail through json into my database from my app but I do not know that How can I check them into my app! (e.g Get the response of username checking query into my app.)
Thanks in advance.
Basically when you run httpClient.execute it will return a response, you need to use that response.
Client side:
HttpResponse resp = httpClient.execute( post );
DataInputStream is = new DataInputStream( resp.getEntity().getContent() );
Server side depends on what programming language you use. For example using python:
self.response.headers['Content-Type'] = 'text/vnd.aexp.json.resp'
self.response.set_status( 200,"OK" )
self.response.out.write( json.dumps( responseList ) )
See this example for full source code and details:
http://mylifewithandroid.blogspot.fi/2010/10/client-server-communication-with-json.html
EDIT
Check this for php server side:
http://www.happycode.info/php-json-response/
Related
I tried to google it but all the tutorials are in swift 3 or below. All I know is that you have to use a JSONEncoder in swift and have to send the JSON data from swift.
Thanks for the help.
Go through this link to know how to install Alamofire.
How to install the Alamofire 4.0 in Xcode 8.0
Use this code to send your data to server.
// https://www.yourAPIHostedWebsite.com/endpoints/
let url = "http://www.example.com/index.html?name=santosh&country=nepal"
Alamofire.request(url, method: .get)
.validate(statusCode: 200..<300)
.responseJSON { response in
print(response)
}.resume()
Now you need a code in your server to store datas. Here a very simple code which gets value. I hope you know how to store data to Mysql database on server side. If not this link might be helpful to start.
<?php
$name = $_GET["name"];
$country = $_GET["country"];
if($name || $country) {
// Now store these values to your database.
}
?>
I have an android app and am trying to send data to the PHP on the server. The server gets the php data with
$this->get('uname');
$this->get('pass');
We do use codeigniter if that matters
The Java code, inside of an Async method, I currently have is
InputStream response = null;
URLConnection connection = new URL(urls[0]).openConnection();
connection.setRequestProperty("uname" , Username);
connection.setRequestProperty("pass", Password);
response = connection.getInputStream();
When I run this, the code always returns null but it is supposed to return a JSON array either way. I have checked the URL and it is correct. What am I doing wrong? Thanks!
You code should be like this
For your android side ,as mentioned by #Ichigo Kurosaki at Sending POST data in Android
For Codeigniter side , cosider you function name is user_login
function user_login(){
$uname = $this->input->get_post('uname'); //get_post will work for both type of GET/POST request
$pass = $this->input->get_post('pass');
$result=$this->authenticate->actLogin($uname,$pass ); //considering your authenticating user and returning 1,0 array as success/failure
$this->output
->set_content_type('application/json')
->set_output(json_encode($result));
exit;
}
i am developing some stuff with cocos2d-x and my webserver.
My target is to send Data from mobile Device(Iphone, cocos2d-x) to my server (php) and back to mobile Device.
My Idea is to do this with CCHttpClient, but how can i get an aswere from my server?
Device -> HttpClient -> Data methode "POST" -> Server, deal with data and send variabel "x" back
So how can i send anything back?
best regards
TO
PS: I am German, so sorry for some English-mistakes XD
i think u are just should do echo json_encode( $data ); exit; Try that.
i have question about sending and getting data from server, i just want to know is it doable what i want to achieve. So here is the thing:
i have some data that i want to send to server, like some id, name, surname
and i have server with ip 256.257.258.259, server waits for request with values encoded in url, so i can send data to server somehow like this:
$data = array(
'id'=>'c456ki98765',
'name'=>'john',
'surname'=>'smith');
$urlstring = http_build_query($data);
$url = 'http://256.257.258.259/folder/automatic?'.($urlstring);
i have a question there, should i use GET request to send this kind of data?
and would it look something like this?
$request = new HttpRequest('url', HttpRequest::METH_GET);
and when server gets my data it sends back response - for example json data - 'number' string or 'error' string if id is wrong. And i have another question there, how can i get data that server sends me after i have sent request?
how can i get that 'number'or 'error'? i hope i have made my question clear
You can use either a POST or a GET request depending on your requirements
Your $request is fine for setting up the request but then you need to send it:
$request->send();
and will need to do something with the response code:
$request->getResponseCode()
Hopefully the below links will come in handy (especially the bottom on in this case):
http://www.php.net/manual/en/function.http-get.php
http://www.php.net/manual/en/function.http-post-data.php
http://www.php.net/manual/en/httprequest.send.php
I need to reply back to a response received.
How should I reply using the response?
I want to reply to a response received in my MDM System. The device contacts the server and delivers status as idle. Now I need to send a xml command directly to the device.
You can't push from PHP Webserver to HTTPClient, the HTTPClient must contact you first (Unless you write a sockets module, then php could be the wrong language) - Only said it as I'm not certain what your trying to achieve.
Anyway straight to the point - Once the device contacts the server, and gives the status 'idle', you can just immediately write out an echo response
if(isset($_POST['status']) && $_POST['status'] == 'idle') {
echo '<?xml version="1.0" encoding="ISO-8859-1"?><root>
<data>blablabla</data>
</root>';
}
etc. Depends on how the devices actually contacts the server, is it sending a post command? Is it actually connecting as a HTTPClient or just a direct TCP/IP? Is there anymore info you can provide?