我正在尝试在我的Android应用程序中使用这个API。它非常简单,你只需提供一个短网址,它将返回原始网址。
这是我第一次使用API。所以,我是这样做的:
try {
HttpClient client = new DefaultHttpClient();
String url = "http://tiny.pl/htk" //The tiny URL
String getURL = "http://untiny.me/api/1.0/extract?url="+url+"&format=text"; //The API service URL
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
output.setText(EntityUtils.toString(resEntityGet)); //This is a TextView
}
else {
output.setText("null reponse");
}
} catch(Exception e) {
output.setText("exception");
}
我的问题是:
- 在处理API时,使用
HttpGet
是否是正确的方法? - 如果是,那么我在上面犯了什么错误?我总是在我的TextView中得到“exception”。
onClick()
方法内部。这里没有涉及到单独的线程。关于错误,我没有收到任何错误提示! - iTurki