Right, i have been working on trying to figure out basic authentication in android so that i can pull json data down from a url. So far the basic authentication has seemed to cause me a lot of trouble. Finally after a good three days research I found an example online that was using a similar technique to pull xml down. This is what i have:
String MY_APP_TAG = "basic.authentication";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String username = "username";
String host = "www.example.com";
String password = "password";
String urlBasePath = "http://www.example.com/api/core/v1/my";
String urlApiCall_FindAllRepositories = urlBasePath;
try {
HttpClient client = new DefaultHttpClient();
AuthScope as = new AuthScope(host, 80);
UsernamePasswordCredentials upc = new UsernamePasswordCredentials(
username, password);
((AbstractHttpClient) client).getCredentialsProvider()
.setCredentials(as, upc);
BasicHttpContext localContext = new BasicHttpContext();
BasicScheme basicAuth = new BasicScheme();
localContext.setAttribute("preemptive-auth", basicAuth);
HttpHost targetHost = new HttpHost(host, 80, "http");
HttpGet httpget = new HttpGet(urlApiCall_FindAllRepositories);
HttpResponse response = client.execute(targetHost, httpget,
localContext);
HttpEntity entity = response.getEntity();
Object content = EntityUtils.toString(entity);
String decoded_password = new String(Base64.decode(content.toString(), Base64.DEFAULT));
System.out.println("RESULT: " + decoded_password);
Log.d(MY_APP_TAG, content.toString());
System.out.println(content.toString().getBytes().toString());
} catch (Exception e) {
e.printStackTrace();
Log.d(MY_APP_TAG, "Error: " + e);
}
}
}
As you can see from the code I am passing the authentication and trying to decode the result using base 64. The strange thing is that this is what i am getting back:
07-04 21:55:01.726: D/gralloc_goldfish(639): Emulator without GPU emulation detected.
07-04 21:58:19.256: I/dalvikvm(753): threadid=3: reacting to signal 3
07-04 21:58:19.297: I/dalvikvm(753): Wrote stack traces to ‘/data/anr/traces.txt’
07-04 21:58:19.656: W/DefaultRequestDirector(753): Authentication error: Unable to respond to any of these challenges: {}
07-04 21:58:19.676: I/System.out(753): RESULT: �N�7���|�22 �
07-04 21:58:19.676: D/basic.authentication(753): ������������1Â0��÷¼ÂÊÒ!
07-04 21:58:19.676: D/basic.authentication(753): &ÄÄʬÄ4L¶£¿²îÎÊ
07-04 21:58:19.676: D/basic.authentication(753): 2Ëzc¦ùN&]]jð@6ÚOçð ��ñIf¸PÄKì^¨yMè¡é(½{ÕjÑ< `J£/C _RÇÝöLÿÃãaÃ÷²������
07-04 21:58:19.676: I/System.out(753): [B@412d87a0
07-04 21:58:19.746: I/dalvikvm(753): threadid=3: reacting to signal 3
07-04 21:58:19.756: I/dalvikvm(753): Wrote stack traces to ‘/data/anr/traces.txt’
07-04 21:58:19.946: D/gralloc_goldfish(753): Emulator without GPU emulation detected.
So as you can see I am getting some weird code back but its also stating that there is an authentication error.
can anyone help me please to try and figure out what is going on?
Two things are happening here: you are performing a network operation on the main thread, and you are getting an ANR (application not responding) from Android. Change your app to use AsyncTask or similar. The second thing is that the data you are getting back is probably not what you expect, since Bae64 decoding seems to produce garbage. Dump the raw response and check/compare with results in a browser.