Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>UPDATE</h1> <p>This is a very old answer. I definitely won't recommend Apache's client anymore. Instead use either: </p> <ul> <li><a href="http://square.github.io/okhttp/" rel="noreferrer">OkHttp</a></li> <li><a href="http://developer.android.com/reference/java/net/HttpURLConnection.html" rel="noreferrer">HttpUrlConnection</a></li> </ul> <h1>Original Answer</h1> <p>First of all, request a permission to access network, add following to your manifest:</p> <pre><code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt; </code></pre> <p>Then the easiest way is to use Apache http client bundled with Android:</p> <pre><code> 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()); } </code></pre> <p>If you want it to run on separate thread I'd recommend extending AsyncTask:</p> <pre><code>class RequestTask extends AsyncTask&lt;String, String, String&gt;{ @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.. } } </code></pre> <p>You then can make a request by:</p> <pre><code> new RequestTask().execute("http://stackoverflow.com"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload