JsonActivity:
package com.fooddemo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class JsonActivityNew { public static Map<String, String> getJsonLogin(String str) throws JSONException { JSONObject obj = new JSONObject(str); String resultcode = obj.getString("resultcode"); String reason = obj.getString("reason"); Map<String, String> map = new HashMap<String, String>(); map.put("resultcode", resultcode); map.put("reason", reason); return map; } /** * 解析菜单 * */ public static List<MenuDemo> getMenuDemoh(String str) throws JSONException { List<MenuDemo> arr = new ArrayList<MenuDemo>(); MenuDemo mDemo = null; JSONObject obj = new JSONObject(str); String reason = obj.getString("reason"); String resultcode = obj.getString("resultcode"); int error_code = obj.getInt("error_code"); String result = obj.getString("result"); JSONArray ja = obj.getJSONArray("result"); for (int i = 0; i < ja.length(); i++) { JSONObject jb=ja.getJSONObject(i); String nearby_shops = obj.getString("nearby_shops"); String tags = obj.getString("tags"); String good_remarks = obj.getString("good_remarks"); String all_remarks = obj.getString("all_remarks"); String phone = obj.getString("phone"); String product_rating = obj.getString("product_rating"); String recommended_products = obj.getString("recommended_products"); String enviroment_rating = obj.getString("enviroment_rating"); String city = obj.getString("city"); String very_good_remarks = obj.getString("very_good_remarks"); String photos = obj.getString("photos"); String very_bad_remarks = obj.getString("very_bad_remarks"); String bad_remarks = obj.getString("bad_remarks"); String area = obj.getString("area"); String address = obj.getString("address"); String navigation = obj.getString("navigation"); String stars = obj.getString("stars"); String name = obj.getString("name"); String service_rating = obj.getString("service_rating"); String recommended_dishes = obj.getString("recommended_dishes"); String reslongitudeult = obj.getString("longitude"); String longitude = obj.getString("longitude"); String latitute = obj.getString("latitute"); String common_remarks = obj.getString("common_remarks"); String avg_price = obj.getString("avg_price"); mDemo = new MenuDemo(resultcode, reason, result, nearby_shops, tags, good_remarks, all_remarks, phone, product_rating, recommended_products, enviroment_rating, city, very_good_remarks, photos, very_bad_remarks, bad_remarks, area, address, navigation, stars, name, service_rating, recommended_dishes, longitude, latitute, common_remarks, avg_price, error_code); } // mDemo.setReason(reason); // mDemo.setResultcode(resultcode); // mDemo.setResult(result); // mDemo.setError_code(error_code); arr.add(mDemo); System.out.println("arr---" + arr); // System.out.println("mDemo---"+mDemo); return arr; } }
MenuDemoLogic:
package com.fooddemo.Logic; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; import com.fooddemo.HttpUtilNew; import com.fooddemo.JsonActivityNew; import com.fooddemo.MenuDemo; public class MenuDemoLogic { /** * 获取网上菜单表内容 */ public List<MenuDemo> MenuDemo(String radius) throws ClientProtocolException, IOException, JSONException { Map<String, String> map = new HashMap<String, String>(); map.put("key", "f5a1f5d1efd83cf0026ec5912c5f9208"); map.put("lng", "121.538123"); map.put("lat", "31.677132"); map.put("radius",radius); String str = HttpUtilNew.getRequest("query", map); System.out.println("str---"+str); List<MenuDemo> requst = JsonActivityNew.getMenuDemoh(str); System.out.println("requst---"+requst); return requst; } }
ManActivity:
package com.fooddemo; import java.io.IOException; import java.util.List; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; import com.fooddemo.Logic.MenuDemoLogic; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { MenuDemoLogic mLogic; Button bt; public static EditText ed,ed1; // Map<String, List<MenuDemo>> menumap; List<MenuDemo> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.button1); ed=(EditText) findViewById(R.id.editText1); ed1=(EditText) findViewById(R.id.editText2); mLogic = new MenuDemoLogic(); // 获取菜类 // for (Entry<String, List<MenuDemo>> map : menumap.entrySet()) { // list.add(map.getKey()); // } bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { try { list=mLogic.MenuDemo(ed.getText().toString()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } // ed1.setText(list.toString()); // System.out.println(list.toString()); // System.out.println("-----------------------------------------"); // System.out.println(list.get(0)); // String str = HttpUtilNew.getRequest("key", null); // System.out.println("点了我!!!"+"str---"+str); } }); } }