【问题标题】:JSON exception to parse photo_refrence from Google places api从 Google Places api 解析 photo_refrence 的 JSON 异常
【发布时间】:2013-05-15 02:52:07
【问题描述】:

我制作了一个 json android 应用程序,以列表视图显示照片的参考,但它给了我一个错误 我的代码是\

package com.example.jsonapp;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

公共类 MainActivity 扩展 ListActivity { // 发出请求的url 私有静态字符串 url = "https://maps.googleapis.com/maps/api/place/nearbysearch /json?location=mylatandlong&radius=5000&types=restaurant&sensor=false&key=myownkey";

// JSON Node names
private static final String TAG_RESULTS = "results";
private static final String TAG_PHOTOS = "photos";
private static final String TAG_REFRENCE = "photo_reference";

JSONArray results = null;
JSONArray photos = null;

Context c;

  // Progress dialog
ProgressDialog pDialog;
ArrayList<HashMap<String, String>> contactList;


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



    new LaodPhotos().execute(TAG_REFRENCE);

    //ListView lv = getListView();

}//the end of on create

public class LaodPhotos extends AsyncTask<String, String, String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading profile ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    @Override
    protected String doInBackground(String... arg0) {
        contactList = new ArrayList<HashMap<String, String>>();
        JSONParser jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(url);
        try{
            results = json.getJSONArray(TAG_RESULTS);
            photos = json.getJSONArray(TAG_PHOTOS);

            for(int i=0; i<results.length(); i++){
                JSONObject c = results.getJSONObject(i);
                if(c != null){
                    for(int j=0; j<photos.length(); j++){
                        JSONObject pc = photos.getJSONObject(j);
                        String photo = pc.getString(TAG_REFRENCE);
                         // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                         map.put(TAG_REFRENCE, photo);
                         contactList.add(map);
                    }
                }   
            }

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

    @Override
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {

                ListAdapter adapter = new SimpleAdapter(c, contactList, R.layout.list_item,
                        new String[]{TAG_REFRENCE}, new int[]{R.id.photoRef});

                setListAdapter(adapter);

            }//end of run method
        });//end of runOnUiThread
    }//end of onPost method
    }


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

}

原木猫\ E/AndroidRuntime(23042): java.lang.NullPointerException E/AndroidRuntime(23042): 在 android.widget.SimpleAdapter.(SimpleAdapter.java:85)

任何人都可以帮助我知道如何使它成功,我想要 photo_reference 能够将其显示为图像,但首先需要知道如何拥有 photo_reference?!

【问题讨论】:

  • 只是提醒一下-onPostExecute 已经在 UI 线程上运行,没有理由将其包装在另一个调用中。

标签: android json google-places-api


【解决方案1】:

在将c Context 实例传递给 SimpleAdapter 之前,您忘记使用 Activity Context 初始化它。所以将 Activity Context 传递给 Adapter:

ListAdapter adapter = new SimpleAdapter(MainActivity.this,
                                      contactList, R.layout.list_item,
                                      new String[]{TAG_REFRENCE}, 
                                      new int[]{R.id.photoRef});

 MainActivity.this.setListAdapter(adapter);

并且onPostExecute 方法已经在 UI 线程上运行,则无需使用 runOnUiThread 来更新 UI 元素表单 onPostExecute

【讨论】:

  • 很好,我已经完成了,但 UI 仍然没有显示任何内容,只有一个空白屏幕。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
相关资源
最近更新 更多