質問

私は、Web URLのカウントのようにFacebookを取得しようとしています。 たとえば、GoogleのようなGoogleのように取得したい場合は、下記のリンクを使用するつもりです

"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)
.

役に立ちましたか?

解決

これは応答を得るためのコードです:

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" />権限を追加していることを確認してください。

他のヒント

manifest.xml <uses-permission android:name="android.permission.INTERNET" />を追加したことを確認してください。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top