Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JSON数组数据放入Hashmap中,并通过Intent Extra传递_Java_Android_Json_Android Intent_Arrays - Fatal编程技术网

Java 将JSON数组数据放入Hashmap中,并通过Intent Extra传递

Java 将JSON数组数据放入Hashmap中,并通过Intent Extra传递,java,android,json,android-intent,arrays,Java,Android,Json,Android Intent,Arrays,在我的应用程序中,我通过find“findviewByid”成功实现了通过Intent将JSON对象传递给新活动 现在这是一个餐馆搜索应用程序,每个餐馆都有几张菜单照片。我到处找,想找到类似的东西,但无法实现 这是我的JSON文件的一部分: [ { login_id: "6", name: "Urban Spice", location: "banani", latitude: "23.790327", longitude: "90.409007", address: "House-

在我的应用程序中,我通过find“findviewByid”成功实现了通过Intent将JSON对象传递给新活动

现在这是一个餐馆搜索应用程序,每个餐馆都有几张菜单照片。我到处找,想找到类似的东西,但无法实现

这是我的JSON文件的一部分:

[
{
login_id: "6",

name: "Urban Spice",

location: "banani",

latitude: "23.790327",

longitude: "90.409007",

address: "House- 119, Road-11, Block-E, Banani",
rating: "4.00",

costfortwopeople: "0",

openingclosingtime: "",

type: "restaurant,ice cream parlour",

perks: "kids zone,home delivery,catering",

cuisine: "indian,indonesian",

phone: "01777899901,2,3,9862672",

image: - [

"http://www.petuuk.com/restaurant_images/img_2146.jpg",

"http://www.petuuk.com/restaurant_images/img_2147.jpg"
],

menu: - [

"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg",
"http://www.petuuk.com/restaurant_images/.jpg"
]
},

 {

login_id: "7",

name: "The Sky Room Dining",

location: "banani",

latitude: "23.793972",

longitude: "90.403190",

address: "ABC House, 12th Floor, 8 Kemal Ataturk Avenue, Banani",

rating: "4.00",

costfortwopeople: "0",

openingclosingtime: "",

type: "restaurant",

perks: "rooftop view,catering",

cuisine: "thai,indian",

phone: "01675019211,9822017",

image: - [
"http://www.petuuk.com/restaurant_images/img_2204.jpg",
"http://www.petuuk.com/restaurant_images/img_2205.jpg",
"http://www.petuuk.com/restaurant_images/img_2206.jpg"
],  etc..................................................................
如上所述,我很难从JSON输出中检索JSON数组“菜单”和“图像”。我能够检索其他JSON对象,如登录id、名称、位置等

我在这里尝试实现的主要目标是,加载Listview中的所有数据,用户可以在其中搜索餐厅,然后当用户单击特定餐厅时,所有加载的数据都应该进入“Intent.putExtra”中,以便在新活动中在完整的餐厅概要视图中查看

public class SearchAll extends ListActivity {

ConnectionDetector cd;
AlertDialogManager alert = new AlertDialogManager();

//Progress Dialog
private ProgressDialog pDialog;

//make json parser Object
JSONParser jsonParser = new JSONParser();

ArrayList<HashMap<String, String>> restaurant_list;

//Restaurant Json array
JSONArray restaurants = null;

private static final String URL_RESTAURANT_LIST 
  = "http://www.petuuk.com/android/allRestaurantList2.php";

//all JSON Node Names
private static final String TAG_ID = "login_id";
private static final String TAG_NAME = "name";
private static final String TAG_LOCATION = "location";
private static final String TAG_LAT = "lattitude";
private static final String TAG_LONG = "longitude";
private static final String TAG_ADDRESS = "address";
private static final String TAG_COST_2 = "costfortwopeople";
private static final String TAG_TYPE = "type";
private static final String TAG_PERKS = "perks";
private static final String TAG_CUISINE = "cuisne";
private static final String TAG_PHONE = "phone";
private static final String TAG_RATING = "rating";
private static final String TAG_IMAGE = "image";
private static final String TAG_MENU = "menu";
private static final String TAG_TIMING = "openingclosingtime";

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

    cd = new ConnectionDetector(getApplicationContext());

    //Check for Internet Connection
    if (!cd.isConnectingToInternet()) {
        //Internet connection not present
        alert.showAlertDialog(SearchAll.this, "Internet Connection Error",
                "Please Check Your Internet Connection", false);
        //stop executing code by return
        return;
    }

    restaurant_list = new ArrayList<HashMap<String, String>>();



    //get ListView
    ListView lv = getListView();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      Intent intent = new Intent(getApplicationContext(), RestaurantProfile.class);
            String loginId = ((TextView) view.
                    findViewById(R.id.login_id)).
                    getText().toString();

            String res_name = ((TextView) view.
                    findViewById(R.id.restaurant_name)).
                    getText().toString();


            intent.putExtra(TAG_ID, loginId);
            intent.putExtra(TAG_NAME, res_name);

            startActivity(intent);


        }
    });

    lv.setOnScrollListener(new EndlessScrollListener() {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {

        }
    });

    new LoadRestaurants().execute();



}


class LoadRestaurants extends AsyncTask<String, String, String> {

    //Show Progress Dialog
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SearchAll.this);
        pDialog.setMessage("Loading All Restaurants...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... arg) {
        //building parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        //Getting JSON from URL
        String json = jsonParser.makeHttpRequest(URL_RESTAURANT_LIST, "GET", params);

        //Log Cat Response Check
        Log.d("Areas JSON: ", "> " + json);

        try {
            restaurants = new JSONArray(json);

            if (restaurants != null) {
                //loop through all restaurants
                for (int i = 0; i < restaurants.length(); i++) {
                    JSONObject c = restaurants.getJSONObject(i);

                    //Storing each json  object in the variable.
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String location = c.getString(TAG_LOCATION);
                    String rating = c.getString(TAG_RATING);
                    String address = c.getString(TAG_ADDRESS);
                    String latitude = c.getString(TAG_LAT);
                    String longitude = c.getString(TAG_LONG);
                    String costfor2 = c.getString(TAG_COST_2);
                    String timing = c.getString(TAG_TIMING);
                    String type = c.getString(TAG_TYPE);
                    String perks = c.getString(TAG_PERKS);
                    String cuisine = c.getString(TAG_CUISINE);
                    String phone = c.getString(TAG_PHONE);


                    JSONArray menuArray = c.getJSONArray("menu");
                    JSONArray imagesArray = c.getJSONArray("image");


                    //Creating New Hashmap
                    HashMap<String, String>  map = new HashMap<String, String>();

                    //adding each child node to Hashmap key
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_LOCATION, location);
                    map.put(TAG_RATING, rating);
                    for(int m=0;m<menuArray.length();++m){
                        map.put("MENU_" + m,menuArray.getString(m));
                    }//menu for loop
                    map.put("TOTAL_MENU", menuArray.length());


              //      map.put(TAG_MENU, String.valueOf(menu));

                    //adding HashList to ArrayList
                    restaurant_list.add(map);
                }

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

        return null;
    }

    protected void onPostExecute(String file_url) {

        //dismiss the dialog
        pDialog.dismiss();


        //Updating UI from the Background Thread
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                ListAdapter adapter = new SimpleAdapter(
                        SearchAll.this, restaurant_list,
                        R.layout.listview_restaurants, new String[]{
                        TAG_ID, TAG_NAME, TAG_LOCATION, TAG_RATING}, new int[]{
                  R.id.login_id, R.id.restaurant_name, R.id.location,  R.id.rating});

                setListAdapter(adapter);


            }
        });


    }
}
这些是我“SeachAll”活动的一部分,我需要帮助。这是用于从JSON文件检索数据的for循环。我需要这里的帮助从“图像”和“菜单”检索数据,然后将其放入我的hashmap

 protected String doInBackground(String... arg) {
        //building parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        //Getting JSON from URL
        String json = jsonParser.makeHttpRequest(URL_RESTAURANT_LIST, "GET", params);

        //Log Cat Response Check
        Log.d("Areas JSON: ", "> " + json);

        try {
            restaurants = new JSONArray(json);

            if (restaurants != null) {
                //loop through all restaurants
                for (int i = 0; i < restaurants.length(); i++) {
                    JSONObject c = restaurants.getJSONObject(i);

                    //Storing each json  object in the variable.
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String location = c.getString(TAG_LOCATION);
                    String rating = c.getString(TAG_RATING);`  HashMap<String, String>  map = new HashMap<String, String>();

                    //adding each child node to Hashmap key
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_LOCATION, location);
                    map.put(TAG_RATING, rating);


                    //adding HashList to ArrayList
                    restaurant_list.add(map);
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }`
受保护字符串doInBackground(字符串…arg){
//建筑参数
List params=new ArrayList();
//从URL获取JSON
字符串json=jsonParser.makeHttpRequest(URL\u列表,“GET”,参数);
//日志Cat响应检查
Log.d(“区域JSON:”,“>”+JSON);
试一试{
餐厅=新JSONArray(json);
if(餐厅!=null){
//环游所有餐厅
对于(int i=0;i
这是我的onItemClick。需要帮助放置数组,我不知道是否可以像下面的json对象一样传递json数组

ListView lv = getListView();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(getApplicationContext(), RestaurantProfile.class);
            String loginId = ((TextView) view.
                    findViewById(R.id.login_id)).
                    getText().toString();

            String res_name = ((TextView) view.
                    findViewById(R.id.restaurant_name)).
                    getText().toString();


            intent.putExtra(TAG_ID, loginId);
            intent.putExtra(TAG_NAME, res_name);

            startActivity(intent);


        }
    });
ListView lv=getListView();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Intent Intent=newintent(getApplicationContext(),RestaurantProfile.class);
字符串loginId=((文本视图)视图。
findviewbyd(R.id.login\u id))。
getText().toString();
字符串res_name=((TextView)视图。
findViewById(R.id.餐厅名称))。
getText().toString();
intent.putExtra(标签号,登录号);
意向。额外(标签名称、资源名称);
星触觉(意向);
}
});
简而言之,我需要两件事的帮助

1.从JSON文件中检索JSON数组“image”和“menu”URL,并将其放入Hashmap中。

2.根据我的意愿将此数据传递给新活动。

这是“SearchAll”活动的完整代码

public class SearchAll extends ListActivity {

ConnectionDetector cd;
AlertDialogManager alert = new AlertDialogManager();

//Progress Dialog
private ProgressDialog pDialog;

//make json parser Object
JSONParser jsonParser = new JSONParser();

ArrayList<HashMap<String, String>> restaurant_list;

//Restaurant Json array
JSONArray restaurants = null;

private static final String URL_RESTAURANT_LIST 
  = "http://www.petuuk.com/android/allRestaurantList2.php";

//all JSON Node Names
private static final String TAG_ID = "login_id";
private static final String TAG_NAME = "name";
private static final String TAG_LOCATION = "location";
private static final String TAG_LAT = "lattitude";
private static final String TAG_LONG = "longitude";
private static final String TAG_ADDRESS = "address";
private static final String TAG_COST_2 = "costfortwopeople";
private static final String TAG_TYPE = "type";
private static final String TAG_PERKS = "perks";
private static final String TAG_CUISINE = "cuisne";
private static final String TAG_PHONE = "phone";
private static final String TAG_RATING = "rating";
private static final String TAG_IMAGE = "image";
private static final String TAG_MENU = "menu";
private static final String TAG_TIMING = "openingclosingtime";

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

    cd = new ConnectionDetector(getApplicationContext());

    //Check for Internet Connection
    if (!cd.isConnectingToInternet()) {
        //Internet connection not present
        alert.showAlertDialog(SearchAll.this, "Internet Connection Error",
                "Please Check Your Internet Connection", false);
        //stop executing code by return
        return;
    }

    restaurant_list = new ArrayList<HashMap<String, String>>();



    //get ListView
    ListView lv = getListView();
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

      Intent intent = new Intent(getApplicationContext(), RestaurantProfile.class);
            String loginId = ((TextView) view.
                    findViewById(R.id.login_id)).
                    getText().toString();

            String res_name = ((TextView) view.
                    findViewById(R.id.restaurant_name)).
                    getText().toString();


            intent.putExtra(TAG_ID, loginId);
            intent.putExtra(TAG_NAME, res_name);

            startActivity(intent);


        }
    });

    lv.setOnScrollListener(new EndlessScrollListener() {
        @Override
        public void onLoadMore(int page, int totalItemsCount) {

        }
    });

    new LoadRestaurants().execute();



}


class LoadRestaurants extends AsyncTask<String, String, String> {

    //Show Progress Dialog
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(SearchAll.this);
        pDialog.setMessage("Loading All Restaurants...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... arg) {
        //building parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        //Getting JSON from URL
        String json = jsonParser.makeHttpRequest(URL_RESTAURANT_LIST, "GET", params);

        //Log Cat Response Check
        Log.d("Areas JSON: ", "> " + json);

        try {
            restaurants = new JSONArray(json);

            if (restaurants != null) {
                //loop through all restaurants
                for (int i = 0; i < restaurants.length(); i++) {
                    JSONObject c = restaurants.getJSONObject(i);

                    //Storing each json  object in the variable.
                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_NAME);
                    String location = c.getString(TAG_LOCATION);
                    String rating = c.getString(TAG_RATING);
                    String address = c.getString(TAG_ADDRESS);
                    String latitude = c.getString(TAG_LAT);
                    String longitude = c.getString(TAG_LONG);
                    String costfor2 = c.getString(TAG_COST_2);
                    String timing = c.getString(TAG_TIMING);
                    String type = c.getString(TAG_TYPE);
                    String perks = c.getString(TAG_PERKS);
                    String cuisine = c.getString(TAG_CUISINE);
                    String phone = c.getString(TAG_PHONE);


                    JSONArray menuArray = c.getJSONArray("menu");
                    JSONArray imagesArray = c.getJSONArray("image");


                    //Creating New Hashmap
                    HashMap<String, String>  map = new HashMap<String, String>();

                    //adding each child node to Hashmap key
                    map.put(TAG_ID, id);
                    map.put(TAG_NAME, name);
                    map.put(TAG_LOCATION, location);
                    map.put(TAG_RATING, rating);
                    for(int m=0;m<menuArray.length();++m){
                        map.put("MENU_" + m,menuArray.getString(m));
                    }//menu for loop
                    map.put("TOTAL_MENU", menuArray.length());


              //      map.put(TAG_MENU, String.valueOf(menu));

                    //adding HashList to ArrayList
                    restaurant_list.add(map);
                }

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

        return null;
    }

    protected void onPostExecute(String file_url) {

        //dismiss the dialog
        pDialog.dismiss();


        //Updating UI from the Background Thread
        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                ListAdapter adapter = new SimpleAdapter(
                        SearchAll.this, restaurant_list,
                        R.layout.listview_restaurants, new String[]{
                        TAG_ID, TAG_NAME, TAG_LOCATION, TAG_RATING}, new int[]{
                  R.id.login_id, R.id.restaurant_name, R.id.location,  R.id.rating});

                setListAdapter(adapter);


            }
        });


    }
}
公共类SearchAll扩展了ListActivity{
连接检测器cd;
AlertDialogManager alert=新建AlertDialogManager();
//进度对话框
私人对话;
//生成json解析器对象
JSONParser JSONParser=新的JSONParser();
ArrayList餐厅列表;
//Json数组
JSONArray=null;
私有静态最终字符串URL\u餐厅\u列表
= "http://www.petuuk.com/android/allRestaurantList2.php";
//所有JSON节点名称
私有静态最终字符串标记\u ID=“login\u ID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串TAG_LOCATION=“LOCATION”;
私有静态最终字符串标记_LAT=“latitude”;
私有静态最终字符串标记_LONG=“经度”;
私有静态最终字符串标记_ADDRESS=“ADDRESS”;
私有静态最终字符串标记_COST_2=“costfortwopeople”;
私有静态最终字符串标记\u TYPE=“TYPE”;
私有静态最终字符串标记_PERKS=“PERKS”;
私有静态最终字符串标记_=“cuisne”;
专用静态最终字符串标记_PHONE=“PHONE”;
私有静态最终字符串标记_RATING=“RATING”;
私有静态最终字符串TAG_IMAGE=“IMAGE”;
私有静态最终字符串标记_MENU=“MENU”;
私有静态最终字符串标记_TIMING=“openingclosingtime”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u search\u all);
cd=新连接检测器(getApplicationContext());
//检查互联网连接
如果(!cd.isConnectingToInternet()){
//Internet连接不存在
alert.showAlertDialog(SearchAll.this,“Internet连接错误”,
“请检查您的互联网连接”,错误);
//通过返回停止执行代码
返回;
}
餐厅列表=新的ArrayList();
//获取列表视图
ListView lv=getListView();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Intent Intent=newintent(getApplicationContext(),RestaurantProfile.class);
字符串loginId=((文本视图)视图。
findviewbyd(R.id.login\u id))。
getText().toString();
字符串资源
imagesArray.getString(index);
for (int i = 0; i < restaurants.length(); i++) {
    :
    :
    map.put(TAG_ID, id);
    map.put(TAG_NAME, name);
    :
    :

    JsonArray menuArray = c.getJsonArray("menu");
    for(int m=0;m<menuArray.length();++m){
        menuMap.add(id,menuArray.getString(m));
    }//menu for loop

    //another for loop for imageArray...
}//end of restaurants loop
class restaurant{
private String name="", id =""....
//setters and getters ...

String menuItems[] = null;
String imageItems[] = null;

//setters getters for the arrays.
}
for(int m=0;m<menuArray.length();++m){
    map.add("MENU_" + m,menuArray.getString(m));
}//menu for loop
map.add("TOTAL_MENU", Integer.toString(menuArray.length()));