0

I have an online database and a simple php script with some insert statements. I want to pass variables through the URL to update the database using the php script I have created. When I run the URL on my browser it works fine and adds the values into the database, but now I want to do this with android. So far this is pretty much all I could find to help me with this:

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.example.com/" + URL);
HttpResponse response = httpClient.execute(httpGet, localContext);

And I have no idea what to do afterwards. I've been searching for a solution but nothing I find works for me. All I need to know is what to do to launch the URL in order to update my database.

meau94
  • 11

2 Answers2

0

Did I correctly realize that you need to execute some POST-request with some parameters and you need to execute it without firing a browser?

Oleg Osipenko
  • 2,409
  • 1
  • 20
  • 28
  • Yes. I want to open the URL in my app so I can execute it but I don't to do it with launching a browser. – meau94 Nov 19 '14 at 02:47
  • If so you can use some networking libraries such as [Retrofit][1] for instance. You just need to describe your Post method and declare variables you pass to your server. Or you can use your code given above - just make the correct http-request path [1]: http://square.github.io/retrofit/ – Oleg Osipenko Nov 19 '14 at 02:50
  • Alright I'll take a look into Retrofit. Though if I had to use my code above, all I want to know is what to do with HttpResponse afterwards. If I leave it in my code like that then it pretty much just stays as an unused variable. – meau94 Nov 19 '14 at 02:58
  • You can obtain statusLine from this response to check whether or not your request was successful, you can receive any additional information from your server with this response – Oleg Osipenko Nov 19 '14 at 03:07
0

Since your are not actually trying to display a web page, you can parse an HTTP response in many ways. Here is an example using a JSON string:

How do I parse JSON from a Java HTTPResponse?

Basically, this is the important part:

BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

The server response is essentially an input file. If it's HTML, you can use JSOUP or some other HTML parser.

You should be cautious of a few things. First, you cannot do this on the UI thread. Second, the user may close the app before it's processed so you need UI progress bars or maybe you don't care...

Last, you probably need to check for network status, etc., before making the call. WebView fails gracefully, but your app may not.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36