How to make a web service (preferably using PHP) that can be requested to receive JSON objects by an Android application

I am making an Android application that communicates with a remote server database and exchanges data with each other, passing a JSON object to and from.

I need to know how to write such a service on my server (preferably in PHP), to which the android application can make a request, and when the request is received, the server processes and creates a JSON object and transfers it to the Android application.

Also, I need to know when this service starts on the server, on WHICH will the android application url make a request?

For example, if the android application needs to request to break the data sample for the parameters: name: Apple Location: USA, then I think the Android application will have to request a server in the form: www.example.com? name = 'Apple "& location =' US

So how to make such a service on a remote server?

Thanks in advance!

+4
source share
2 answers

The best example you can reference is http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ . It has full code [php + android] with a simple explanation.

+9
source

You can write a method that requests a remote php file that answers a message or receives a request. if you want to use the mail request, you can use the method below. and you must add resolution to your manifest file. As you can see below, you can add parameters as a key-value pair.

public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.yoursite.com/webservice.php"); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("name", "Apple")); nameValuePairs.add(new BasicNameValuePair("locaction", "US")); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); ResponseHandler <String> res=new BasicResponseHandler(); // Execute HTTP Post Request String response = httpclient.execute(httppost,res); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } } 

http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient

0
source

Source: https://habr.com/ru/post/1435027/


All Articles