Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an HTTP request with android

I have searched everywhere but I couldn't find my answer, is there a way to make a simple HTTP request? I want to request a PHP page / script on one of my websites but I don't want to show the webpage.

If possible I even want to do it in the background (in a BroadcastReceiver)

like image 593
Mats Hofman Avatar asked Aug 17 '10 19:08

Mats Hofman


People also ask

How do I request post on Android?

Build HttpURLConnection and URL objects. In your code, begin your new method by creating a URL object and assign it a URL for the HttpURLConnection object to connect to. URL url = new URL("http://exampleurl.com/"); HttpURLConnection client = (HttpURLConnection) url. openConnection();

How will you use HttpClient to make server requests?

uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response; String responseString = null; try { response = httpclient. execute(new HttpGet(uri[0])); StatusLine statusLine = response. getStatusLine(); if(statusLine. getStatusCode() == HttpStatus.

What is http client Android?

HttpClient is used when you want to receive and send data from the server over the internet. So for this you need to create a http client using HttpClient class. First, you will create the object of Http client and the URL to the constructor of HttpPost class that post the data.

How do I make a Web request?

The most common HTTP request methods have a call shortcut (such as http. get and http. post), but you can make any type of HTTP request by setting the call field to http. request and specifying the type of request using the method field.


2 Answers

UPDATE

This is a very old answer. I definitely won't recommend Apache's client anymore. Instead use either:

  • Retrofit
  • OkHttp
  • Volley
  • HttpUrlConnection

Original Answer

First of all, request a permission to access network, add following to your manifest:

<uses-permission android:name="android.permission.INTERNET" /> 

Then the easiest way is to use Apache http client bundled with Android:

    HttpClient httpclient = new DefaultHttpClient();     HttpResponse response = httpclient.execute(new HttpGet(URL));     StatusLine statusLine = response.getStatusLine();     if(statusLine.getStatusCode() == HttpStatus.SC_OK){         ByteArrayOutputStream out = new ByteArrayOutputStream();         response.getEntity().writeTo(out);         String responseString = out.toString();         out.close();         //..more logic     } else{         //Closes the connection.         response.getEntity().getContent().close();         throw new IOException(statusLine.getReasonPhrase());     } 

If you want it to run on separate thread I'd recommend extending AsyncTask:

class RequestTask extends AsyncTask<String, String, String>{      @Override     protected String doInBackground(String... uri) {         HttpClient httpclient = new DefaultHttpClient();         HttpResponse response;         String responseString = null;         try {             response = httpclient.execute(new HttpGet(uri[0]));             StatusLine statusLine = response.getStatusLine();             if(statusLine.getStatusCode() == HttpStatus.SC_OK){                 ByteArrayOutputStream out = new ByteArrayOutputStream();                 response.getEntity().writeTo(out);                 responseString = out.toString();                 out.close();             } else{                 //Closes the connection.                 response.getEntity().getContent().close();                 throw new IOException(statusLine.getReasonPhrase());             }         } catch (ClientProtocolException e) {             //TODO Handle problems..         } catch (IOException e) {             //TODO Handle problems..         }         return responseString;     }          @Override     protected void onPostExecute(String result) {         super.onPostExecute(result);         //Do anything with response..     } } 

You then can make a request by:

   new RequestTask().execute("http://stackoverflow.com"); 
like image 50
Konstantin Burov Avatar answered Nov 07 '22 10:11

Konstantin Burov


unless you have an explicit reason to choose the Apache HttpClient, you should prefer java.net.URLConnection. you can find plenty of examples of how to use it on the web.

we've also improved the Android documentation since your original post: http://developer.android.com/reference/java/net/HttpURLConnection.html

and we've talked about the trade-offs on the official blog: http://android-developers.blogspot.com/2011/09/androids-http-clients.html

like image 38
Elliott Hughes Avatar answered Nov 07 '22 10:11

Elliott Hughes