JSONException:数据没有价值 [英] JSONException: no value for data

查看:183
本文介绍了JSONException:数据没有价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从这个链接获取JSON数据:<一href=\"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\"相对=nofollow>链接

I want to fetch json data from this link: link

&安培;这里是我的code为:

& here is my code for that:

private static String url = "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 names
private static final String TAG_DATA = "data";
private static final String TAG_SHARE = "share_count";
private static final String TAG_LIKE = "like_count";
private TextView LikeTv;
public String like;

JSONArray data = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about_us);

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
    // Creating JSON Parser instance
    JSONParser jParser = new JSONParser();

    // getting JSON string from URL
    JSONObject json = jParser.getJSONFromUrl(url);

    try {
        // Getting Array of Contacts
        data = json.getJSONArray(TAG_DATA);


        JSONObject c = data.getJSONObject(0);

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

    } catch (JSONException e) {
        e.printStackTrace();
    }
    LikeTv = (TextView) findViewById(R.id.tvLike);
    LikeTv.setText(like);

现在我得到JSONException:数据没有价值请帮助...什么是错在我的code ..

Now I am getting "JSONException: no value for data" Please help... whats wrong in my code..

推荐答案

好了....结果
我收到了你的问题的解决方案...结果
你写的方法 getJSONFromUrl() ..结果
我相信它包含 HttpPost 对象..结果
更改为 HTTPGET ,它会开始工作......

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

修改

下面是code我试过

public class MainActivity extends Activity {

    private static String url = "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 names
    private static final String TAG_DATA = "data";
    private static final String TAG_SHARE = "share_count";
    private static final String TAG_LIKE = "like_count";
    private TextView LikeTv;
    public String like;

    JSONArray data = null;
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        // Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        // getting JSON string from URL
        JSONObject json = jParser.getJSONfromURL(url);

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


            JSONObject c = data.getJSONObject(0);

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

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

class JSONParser
{
    public JSONObject getJSONfromURL(String url) {
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    // http post
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

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

    // convert response to string
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = 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 = new JSONObject(result);
    } catch (JSONException e) {
        Log.e("log_tag create object ",
                "Error parsing data " + e.toString());
    }

    return jArray;
}
}

这篇关于JSONException:数据没有价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆