Question

I have been trying to fetch facebook like count of any web url. for example if I want to fetch google like count, I am gonna use the link given below

"http://api.facebook.com/restserver.php?method=links.getStats&urls=www.google.com&format=json"

This links works fine in browser

But I am getting UnknownHostException api.facebook.com in Logcat

My code to fetch json string is given below

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();
    }

Am I doing something wrong? Please help me with this

Thank You

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)
Was it helpful?

Solution

here is the code to get the response :

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();
}

make sure you have added <uses-permission android:name="android.permission.INTERNET" /> permission in manifest file.

OTHER TIPS

make sure that you added the <uses-permission android:name="android.permission.INTERNET" /> to your manifest.xml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top