I need a little help with this, I've used HTTP requests before but through JavaScript when I was using AJAX, it looks similar but I'm not entirely sure how to do all of it properly. I've seen some people's codes and stuff, but I'm not sure about two things. First, I want to send an int through the POST in order to identify which query should be called and what data should be sent back through the response. Second, I'm not sure about the URL, do I need a domain? I just need to connect to a server on my machine which I'm using with XAMPP. I'm not entirely sure how to do this? Do I just place a URL with my machine's IP with a port or something?
So here's what I've attempted:
public class Request {
private static Request instance;
private static String URL;
private String requestResult;
private String error;
private Request(){
this.URL = "http://IPAddress or Server Address/Android Webservice/webservice.php"; /* not sure what I should use here, I'm using an apache server */
this.error = new String();
this.requestResult = new String();
}
public static Request getRequest(){
if(instance == null){
instance = new Request();
}
return instance;
}
public void sendHttpPostRequest(String option){
HttpClient client = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(URL);
HttpResponse serverResponse;
String jsonToSend = "message={identifer:" + option + "}";
NameValuePair parameter = new BasicNameValuePair("data", jsonToSend);
try {
StringEntity entity = new StringEntity(jsonToSend);
postRequest.addHeader("content-type", "application/x-www-form-urlencoded");
postRequest.setEntity(entity);
serverResponse = client.execute(postRequest);
} catch (Exception e){
}
}
public String getURL() {
return this.URL;
}
public String getResult(){
return this.requestResult;
}
}
I've got it in a class, because I want to be able to call it anywhere, plus I need to call it in multiple views in my android application. I would say the most important part in here is the `sendHttpPostRequest. However I'm not sure how to go about using that response to do anything. Normally with AJAX I'd just get the response text and go from there, but that doesn't work here. I've seen various examples but I'm having a hard time getting them, and some of them do a lot of stuff that I'm not sure I need.
For instance this is my PHP file:
<?php
$obj = json_decode($_POST['message']);
$obj = json_decode($dataReceived,true);
$data = array("data","some other data","the last piece of data");
json_encode('data'=>$data);
echo $data;
?>
For now it looks like this because this is a simple test, but what the end goal should be is to return an an associative array which would be the result of a query from my database. I know how to do that easily, but I'm not sure how to send the request to get this file to return that stuff. I don't know how to parse a JSON within Java, especially an associative array and I'm also not sure how my server should be configured to allow connections from android devices.
So what exactly needs to go in the URL to connect to my PC? How do I parse JSON response and then convert it into data that I can place in my android application? How do I go about moving anywhere with this device, and retrieving this data from a location far from my server?
I'm hoping that this doesn't require payment of any sort, I mean the server is on my computer after all.