I am using asynhttpClient for basic authentication
http://loopj.com/android-async-http/
that is looj lib..
below is my code:
usernameRandomPassword = userName + ":" + password;
            Log.d("username=",usernameRandomPassword);
            Log.d("url=",url);
            String authorization = "Basic " + Base64.encodeToString(usernameRandomPassword.getBytes("UTF-8"), Base64.NO_WRAP);
            httpClient.addHeader("Authorization",authorization);
            httpClient.addHeader("Content-type", "application/json");
            httpClient.setTimeout(20000);
            httpClient.get( url, new AsyncHttpResponseHandler() {
                    @Override
                    public void onStart() {
                        System.out.println("on satrt");
                        super.onStart();
                    }
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        System.out.println("on onSuccess statusCode="+statusCode);
                        toastmessgae("onSuccess status code="+statusCode);
                        super.onSuccess(statusCode, headers, responseBody);
                    }
                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                        System.out.println("on onFailure="+statusCode);
                        toastmessgae("onFailure status code="+statusCode);
                        super.onFailure(statusCode, headers, responseBody, error);
                    }
                    @Override
                    public void onFinish() {
                        System.out.println("on onFinish");
                        super.onFinish();
                    }
                });
        } catch (UnsupportedEncodingException e) {
        }
but i always receive in console 401, below are logs
Authentication scheme ntlm not supported.
Unable to respond to any of these challenges: {ntlm=WWW-Authenticate: NTLM, negotiate=WWW-Authenticate: Negotiate}
The credentials are correct i checked on direct link.
I have spent already a complete day on this, can any one help me? If you share some example,it will be really helpful.
Thanks in advance..
here is the answer through code:
add below code to your android file
            DefaultHttpClient httpclient = new DefaultHttpClient();
            // register ntlm auth scheme
            httpclient.getAuthSchemes().register("ntlm", new NTLMSchemeFactory());
            httpclient.getCredentialsProvider().setCredentials(
                    // Limit the credentials only to the specified domain and port
                    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT),
                    // Specify credentials, most of the time only user/pass is needed
                    new NTCredentials(username, password, "", "")
            );
            HttpUriRequest httpget = new HttpGet(your_URL);
            HttpResponse response = httpclient.execute(httpget);
            String responseBody = EntityUtils.toString(response.getEntity());
            Log.i(tag,"responseBody =>>>>>>>>>>"+responseBody);
now download lib and java file from
https://github.com/masconsult/android-ntlm
and copy jcifs-1.3.17.jar to your lib folder and JCIFSEngine and NTLMSchemeFactory to your package. (you can change package if you want..)
Thats it your app is ready to run.
More useful Links:
http://www.developergarden.com/en/marketplace/components/details/cmp/android-ntlm-authentication/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With