【问题标题】:How to fetch data from json in android which does not have array name?如何从没有数组名称的android中的json中获取数据?
【发布时间】:2014-06-05 09:46:30
【问题描述】:

我想从此链接获取一个网址:

 "http://graph.facebook.com/10202459285618351/picture?type=large&redirect=false"

它给出了结果:

{ “数据”: { "url": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xap1/t1.0-1/s200x200/10342822_10202261537234765_3194866551853134720_n.jpg", “is_silhouette”:假 } }

我试过了,

private class GetContacts extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Showing progress dialog
        pDialog = new ProgressDialog(AccountActivity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        String jsonStr = sh.makeServiceCall(fbPicURL, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                fbRealURL = jsonObj.getString("url");


                    // Phone node is JSON Object
                    Toast.makeText(AccountActivity.this, fbRealURL,
                            Toast.LENGTH_LONG).show();
                    // tmp hashmap for single contact

                }
             catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        /**
         * Updating parsed JSON data into ListView
         * */
        Toast.makeText(AccountActivity.this, fbRealURL,
                Toast.LENGTH_LONG).show();
    }

}

但是返回 null 并且它不会崩溃..

【问题讨论】:

    标签: android json


    【解决方案1】:

    你必须先像这样获取数据对象:

         try {
                JSONObject jsonObj = new JSONObject(jsonStr);
    
    
                fbRealURLObj = jsonObj.getJSONObject("data");
    
                fbRealURL = fbRealURLObj.getString("url");
    
                    // Phone node is JSON Object
                    Toast.makeText(AccountActivity.this, fbRealURL,
                            Toast.LENGTH_LONG).show();
                    // tmp hashmap for single contact
    
                }
    

    【讨论】:

    • 仍然没有价值
    • 您在 logcat 中是否有任何错误?在此处发布您的 logcat 错误。
    • 它给了我:来自 nativeGetEnabledTags 的意外值:0
    • 你能发布完整的代码吗,如果可以的话.. 意思是一路用 asynctask 上课,用你自己的代码?
    • 你的 asynctask 看起来不错.. 我也会这样做......你能发布完整的 logcat 输出吗
    猜你喜欢
    • 2018-09-03
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2018-03-23
    • 2015-07-22
    • 1970-01-01
    相关资源
    最近更新 更多