我一直试图像任意网址一样获取Facebook。 例如,如果我想获取谷歌像计数,我将使用下面给出的链接
"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)
答案 0 :(得分:1)
这是获得响应的代码:
private String getStringData(String s){
URL url;
StringBuffer jsonstring = null;
HttpURLConnection connection;
try {
url = new URL(s);
Log.i("System out", "url:" + url);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(1000 * 5); // Timeout is in seconds
InputStreamReader is = new InputStreamReader(connection
.getInputStream());
BufferedReader buff = new BufferedReader(is);
jsonstring = new StringBuffer();
String line = "";
do {
line = buff.readLine();
if (line != null)
jsonstring.append(line);
} while (line != null);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonstring.toString().trim();
}
确保您在清单文件中添加了<uses-permission android:name="android.permission.INTERNET" />
权限。
答案 1 :(得分:0)
确保您将<uses-permission android:name="android.permission.INTERNET" />
添加到manifest.xml