Skip to content Skip to sidebar Skip to footer

Jsonexception: No Value For Data

I want to fetch json data from this link: link & here is my code for that: private static String url = 'https://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share

Solution 1:

Well.... I got your problem solution... The method you wrote getJSONFromUrl().. I am sure it contains HttpPost object.. change that to HttpGet and it will start working...

EDIT

Here is the code I tried with

publicclassMainActivityextendsActivity {

    privatestaticStringurl="https://graph.facebook.com/fql?q=SELECT%20url,%20normalized_url,%20share_count,%20like_count,%20comment_count,%20total_count,%20commentsbox_count,%20comments_fbid,%20click_count%20FROM%20link_stat%20WHERE%20url=%27https://www.facebook.com/BillionHands%27";

    // JSON Node namesprivatestaticfinalStringTAG_DATA="data";
    privatestaticfinalStringTAG_SHARE="share_count";
    privatestaticfinalStringTAG_LIKE="like_count";
    private TextView LikeTv;
    public String like;

    JSONArraydata=null;
    @SuppressLint("NewApi")@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicypolicy=newStrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        // Creating JSON Parser instanceJSONParserjParser=newJSONParser();

        // getting JSON string from URLJSONObjectjson= jParser.getJSONfromURL(url);

        try {
            // Getting Array of Contacts
            Log.d("JSON ","DATA "+json);
            data = json.getJSONArray(TAG_DATA);


            JSONObjectc= data.getJSONObject(0);

            // Storing each json item in variableStringshare= c.getString(TAG_SHARE);
            like = c.getString(TAG_LIKE);
            Log.i("Like Count",like);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        returntrue;
    }

}

classJSONParser
{
    public JSONObject getJSONfromURL(String url) {
    InputStreamis=null;
    Stringresult="";
    JSONObjectjArray=null;

    // http posttry {
        HttpClienthttpclient=newDefaultHttpClient();
        HttpGethttpget=newHttpGet(url);
        HttpResponseresponse= httpclient.execute(httpget);
        HttpEntityentity= response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // convert response to stringtry {
        BufferedReaderreader=newBufferedReader(newInputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuildersb=newStringBuilder();
        Stringline=null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag get data string ",
                "Error converting result " + e.toString());
    }

    try {

        jArray = newJSONObject(result);
    } catch (JSONException e) {
        Log.e("log_tag create object ",
                "Error parsing data " + e.toString());
    }

    return jArray;
}
}

Solution 2:

The "data" element in your JSON isn't an array, it is a JSONobject. So instead of:

JSONArray data = json.getJSONArray(TAG_DATA);

Try this:

JSONObject data = json.getJSONObject(TAG_DATA);

From the JSONObject, you can get items like TAG_SHARE and TAG_LIKE.

Good luck!

Post a Comment for "Jsonexception: No Value For Data"