I am trying to access Basecamp API from my Android/Java source code following way ....
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class BCActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DefaultHttpClient httpClient = new DefaultHttpClient();
//final String url = "https://encrypted.google.com/webhp?hl=en"; //This url works
final String url = "https://username:password@projectsource.basecamphq.com/people.xml"; //This don't
HttpGet http = new HttpGet(url);
http.addHeader("Accept", "application/xml");
http.addHeader("Content-Type", "application/xml");
try {
// HttpResponse response = httpClient.execute(httpPost);
HttpResponse response = httpClient.execute(http);
StatusLine statusLine = response.getStatusLine();
System.out.println("statusLine : "+ statusLine.toString());
ResponseHandler <String> res = new BasicResponseHandler();
String strResponse = httpClient.execute(http, res);
System.out.println("________**_________________________\n"+strResponse);
System.out.println("\n________**_________________________\n");
} catch (Exception e) {
e.printStackTrace();
}
WebView myWebView = (WebView) this.findViewById(R.id.webView);
myWebView.loadUrl(url);//Here it works and displays XML response
}
}
This URL displays the response in WebView
, but shows Unauthorized exception when I try to access through HttpClient
as shown above.
Is this is right way to access Basecamp API through Android/Java? or Please provide me a right way to do so.
The HttpClient can't take the login creditals from the URI.
You have to give them with specified methods.
If you use HttpClient 4.x have a look on this:
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html
But notice if you don't want to use the new version on the HttpClient (Android uses version 3.x), you should look here:
http://hc.apache.org/httpclient-3.x/authentication.html
That was the theory, now we use them:
Basically we use HTTP, but if you want to use HTTPS, you have to edit the following assignment
new HttpHost("www.google.com", 80, "http")
intonew HttpHost("www.google.com", 443, "https")
.Furthermore you have to edit the host (www.google.com) for your concerns.
Notice: Only the full qualified domain name (FQDN) is needed not the full URI.
HttpClient 3.x:
HttpClient 4.x:
Attention: You will need the new HttpClient from Apache and additionally you must rearrange the order, that the jar-file is before the Android library.
If you like to use the HttpClient 4.x as mentioned in the other answers you could also use httpclientandroidlib. This is a converted stock HttpClient without apache.commons and with Android LogCat support.
Finally I got it How to glue the code shown in above answer ...
One very important thing How your library should be arranged and which libraries you will required ...
From Here you will find this libraries.
To add them in eclipse (Below Android sdk < 16)...
To arrange them in order in eclipse ...
For above Android sdk >= 16 you will have to put these libraries into "libs" folder.
Appendix on the brilliant and very helpfull answer of CSchulz:
in http client 4.3 this:
does not work anymore (ClientContext.AUTH_CACHE is deprecated)
use:
and
see http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/protocol/ClientContext.html
and:
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/protocol/HttpClientContext.html