我正在尝试通过以下方式从我的 Android/Java 源代码访问Basecamp API ....
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
}
}
此 URL 在 中显示响应WebView
,但当我尝试通过HttpClient
如上所示访问时显示未经授权的异常。
这是通过 Android/Java访问Basecamp API的正确方法吗?或请为我提供正确的方法。