Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to create a <code>HttpHost</code> and <code>HttpContext</code> object with required credentials and give it to execute method. </p> <p>This is a sample code where your authentication is independent of backend auth. http client of android will take care of converting it to appropriate format. Check this sample code, this is only for your reference and not to be used directly in your code. :)</p> <p>This code is in your activity:</p> <pre><code>@Override public void onResume(){ super.onResume(); AsyncTask&lt;String, Void, Void&gt; httpTask = new TestHttpThread(); httpTask.execute("test_url","test_user","test_password"); } </code></pre> <p>Sample <code>AsyncActivity</code>:</p> <pre><code>private class TestHttpThread extends AsyncTask&lt;String, Void, Void&gt;{ @Override protected Void doInBackground(String... params) { if(params.length &gt; 0){ String url = params[0]; String username = params[1]; String password = params[2]; try { AndroidHttpClient httpClient = AndroidHttpClient.newInstance("test user agent"); URL urlObj = new URL(url); HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol()); AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort()); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(username, password); CredentialsProvider cp = new BasicCredentialsProvider(); cp.setCredentials(scope, creds); HttpContext credContext = new BasicHttpContext(); credContext.setAttribute(ClientContext.CREDS_PROVIDER, cp); HttpGet job = new HttpGet(url); HttpResponse response = httpClient.execute(host,job,credContext); StatusLine status = response.getStatusLine(); Log.d(TestActivity.TEST_TAG, status.toString()); httpClient.close(); } catch(Exception e){ e.printStackTrace(); } } return null; } } </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