First create a PHP file, let's say with the following contents:
$q = filter_input(INPUT_POST,"q");
if($q == "SAY_HELLO"){
echo "Hello World";
}else{
echo "No Command Given";
}
Now let's take a look at the java code. First open Android Studio and start a new project. In order to retrieve data from the internet you need to create a child class of AsyncTask. When extending AsyncTask you must specify three parameters < Params, Progress, Result > and define the method doInBackground(Params...params). Params is what is taken when the task is started. Progress is what is what is passed into the onProgressUpdate(Progress...values) method. This method is used to decide what should happen when there's an update in the progress of the task (I won't be going into that here). Result is the value returned from the task. Seeing as we just want to receive a response there isn't much need for the Progress. We can then define our class as so:
public class Request extends AsyncTask{ .... }
The next thing we need to do is implement the doInBackground(Params...params) method. In order to connect to the server we'll need to use an HttpURLConnection and pass in the url to the php file we created.
@Override
protected Object doInBackground(String...params){
URL url = new URL(params[0]); //the url to the php file will be passed in as a parameter
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
....
}
We'll next need to allow the urlConnection upload a request body, specify the request method, as well as the content type:
@Override
protected Object doInBackground(String...params) throws Exception{
....
urlConnection.setDoOutput(true); //Allow the urlConnection to send requests
urlConnection.setRequestMethod("POST"); //Set the request method to POST
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
....
}
Next comes making the actual query. We'll have the query parameter be passed into the method:
@Override
protected Object doInBackground(String...params) throws Exception{
....
String query = "q="+params[1];
urlConnection.setFixedLengthStreamingMode(query.getBytes().length); //set the amount of data to be sent to the server
urlConnection.setConnectTimeout(2000); //how long to wait for a response in milliseconds
....
}
Almost done! Now we just need to output the data and read the response:
@Override
protected Object doInBackground(String...params) throws Exception{
....
DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.writeBytes(query); //send the query to the server
out.flush(); //make sure that the query was sent
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String inputLine = in.readLine();
in.close(); //close the connection
out.close();
return inputLine;
}
Great! Now, in our Android Studio project open the MainActivity.java file and in the onCreate(Bundle) method put this:
Request req = new Request();
req.execute("http://www.example.com/hello.php","SAY_HELLO"); //start the request with the url and query paramater
String res = req.get(); //wait for the request to finish and get the result
((TextView)findViewById(R.id.textView)).setText(res);//display the result in a textview
If your layout.xml file is similar to the one below:
You should see "Hello World" in a text area on your phone or simulator.

No comments:
Post a Comment