我一直在尝试获取 facebook 之类的任何网址的计数。例如,如果我想获取 google like count,我将使用下面给出的链接
"http://api.facebook.com/restserver.php?method=links.getStats&urls=www.google.com&format=json"
此链接在浏览器中运行良好
但我在 Logcat 中得到 UnknownHostException api.facebook.com
下面给出了我获取 json 字符串的代码
public String readJson(String url)
{
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200)
{
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null)
{
builder.append(line);
}
}
else
{
Log.e(TAG, "Failed to download file");
}
}
catch (ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return builder.toString();
}
难道我做错了什么?请在这件事上给予我帮助
谢谢你
01-23 18:32:40.927: W/System.err(1186): java.net.UnknownHostException: Host is unresolved: api.facebook.com:80
01-23 18:32:40.937: W/System.err(1186): at java.net.Socket.connect(Socket.java:1038)
01-23 18:32:40.937: W/System.err(1186): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
01-23 18:32:40.937: W/System.err(1186): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
01-23 18:32:40.947: W/System.err(1186): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
01-23 18:32:40.947: W/System.err(1186): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
01-23 18:32:40.947: W/System.err(1186): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1152)
01-23 18:32:40.947: W/System.err(1186): at com.and.face.facebookactivity.readJson(facebookactivity.java:150)
01-23 18:32:40.958: W/System.err(1186): at com.and.face.facebookactivity.onCreate(facebookactivity.java:106)
01-23 18:32:40.958: W/System.err(1186): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-23 18:32:40.967: W/System.err(1186): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-23 18:32:40.967: W/System.err(1186): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-23 18:32:40.967: W/System.err(1186): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-23 18:32:40.977: W/System.err(1186): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-23 18:32:40.977: W/System.err(1186): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 18:32:40.977: W/System.err(1186): at android.os.Looper.loop(Looper.java:123)
01-23 18:32:40.977: W/System.err(1186): at android.app.ActivityThread.main(ActivityThread.java:4627)
01-23 18:32:40.987: W/System.err(1186): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 18:32:40.987: W/System.err(1186): at java.lang.reflect.Method.invoke(Method.java:521)
01-23 18:32:40.987: W/System.err(1186): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-23 18:32:40.998: W/System.err(1186): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-23 18:32:40.998: W/System.err(1186): at dalvik.system.NativeStart.main(Native Method)