public class XXImpl implementsXX { @Override public ArrayList<Person> httpGetMainMenuList() throws Exception { Log.i("select===", "httpGetMainMenuList"); String str1 = HttpJsonClient.httpGet(http_maingrideurl); Log.i("select===", str1); ArrayList<Person> localArrayList = new ArrayList<Person>(); JSONArray localJSONArray = getJSONObject(str1).optJSONArray("arg0"); for (int i = 0; ; i++) { if (i >= localJSONArray.length()) return localArrayList; JSONObject localJSONObject = localJSONArray.getJSONObject(i); String a= localJSONObject.optString("lastUptOrg"); String b= localJSONObject.optString("menuCode"); String v= localJSONObject.optString("upMenuCode"); String b= localJSONObject.optString("menuType"); String n= localJSONObject.optString("menuName"); String m= localJSONObject.optString("menuDisp"); String r= localJSONObject.optString("menuLogo"); ...........................................................
Person c= new Person(); mm.setA(a); .............................. .................................... ...................................... .........................
localArrayList.add(mPerson ); } }
public Map<String, String> getParams() { HashMap<String,String> localHashMap = new HashMap<String,String>(); return localHashMap; } public JSONObject getJSONObject(String paramString) throws Exception { return (JSONObject)new JSONTokener(paramString).nextValue(); } } 这个更方便
按照你的这个json格式先定义两个个数据modle,M_A,M_B,M_A中有一个list<M_B>,请求数据,返回的jstr,通过jstr,jsonObject = new JSONObject(jstr);然后if (list<M_B> b_list; jsonObject.has("rows")) { JSONArray jsonArray = jsonObject.getJSONArray("rows");for (int i = 0; i < jsonArray.length(); i++) {M_B m_b; JSONObject listObject; listObject = jsonArray.getJSONObject(i);String s = listObject.getString(“paramVersion”); m_b.paramVersion= s, b_list.add(m_b);} M_A m_a; m_a.setM_BList(b_list);}上面的是解析,显示的话应该就明白了。大概思路是这样的,中间的代码是我随手写的,只是逻辑。
你是不是不太清楚json的格式啊
JSONTokener jsontokener = new JSONTokener(strjson);
JSONObject object = (JSONObject) jsontokener.nextValue();
String success = object.getString("success");
success 就可以直接设到画面上了。
String rows= object.getString("rows");
JSONArray new JSONArray(rows)
int size = jsonarray.length();
for (int i = 0; i < size; i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String paramVersion=jsonobject.getString("paramVersion");
...
}
当然有多条,不可能用String来保存,自己定义list之类的就可以搞定了!
只能说到这样了!
我还是去 在 试试
public class HttpUtils { /**
* 从指定的URL中获取数组
*
* @param urlPath
* @return
* @throws Exception
*/
public static String readParse(String urlPath) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
URL url = new URL(urlPath);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStream inStream = conn.getInputStream();
while ((len = inStream.read(data)) != -1) {
outStream.write(data, 0, len);
}
inStream.close();
return new String(outStream.toByteArray());// 通过out.Stream.toByteArray获取到写的数据
} /**
* 访问数据库并返回JSON数据字符串
*
* @param params
* 向服务器端传的参数
* @param url
* @return
* @throws Exception
*/
public static String doPost(List<NameValuePair> params, String url)
throws Exception {
String result = null;
// 获取HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 新建HttpPost对象
HttpPost httpPost = new HttpPost(url);
if (params != null) {
// 设置字符集
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 设置参数实体
httpPost.setEntity(entity);
} HttpResponse httpResp = httpClient.execute(httpPost);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() == 200) {
// 获取返回的数据
result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
} else {
Log.i("HttpPost", "HttpPost方式请求失败");
} return result; }
/**
* 解析
*
* @throws JSONException
*/
public static List<JsonBean> getListBean(String key,String jsonStr){
List<JsonBean> list =new ArrayList<JsonBean>();
try {
JSONObject jsonObject =new JSONObject(jsonStr);
JSONArray jsonArray =jsonObject.getJSONArray(key);
for(int i=0;i<jsonArray.length();i++){
JSONObject jsonObject2 =jsonArray.getJSONObject(i);
Map< String, Object> map =new HashMap<String, Object>();
Iterator<String> iterator= jsonObject2.keys();
while(iterator.hasNext()){
String json_key=iterator.next();
String json_value= jsonObject2.getString(json_key);
if(json_value == null){
json_value="";
}
map.put(json_key, json_value);
}
list.add((JsonBean) map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
package com.zhouweiyousha.json;/**
* Json实体类
*
* @author admin
*
*/
public class JsonBean {
/**
* json数据的实体类
*
*/ private String keyWord;
private String menuLogo;
private int menuSort; public JsonBean(String keyword, String menuLoge, int menuSort) {
super();
this.keyWord = keyword;
this.menuLogo = menuLoge;
this.menuSort = menuSort;
}
public JsonBean() {
// TODO Auto-generated constructor stub
}
// 字符输出
public String toString() {
return "JsonBean [keyWord=" + keyWord + ", menuLogo=" + menuLogo
+ ", menuSort=" + menuSort + "]";
} public String getKeyWord() {
return keyWord;
} public void setKeyWord(String keyWord) {
this.keyWord = keyWord;
} public String getMenuLogo() {
return menuLogo;
} public void setMenuLogo(String menuLogo) {
this.menuLogo = menuLogo;
} public int getMenuSort() {
return menuSort;
} public void setMenuSort(int menuSort) {
this.menuSort = menuSort;
}
}
这个jar包自己搜下下载
怎么把他显示出来 额 ,还要怎么把 文字和图片显示到
public class JsonBeanAdapter extends BaseAdapter { private Context mContext;
private LayoutInflater mInflater;
private List<JsonBean> data;
private AsyncImageLoader imageLoader; // 实现内部类的方法
public JsonBeanAdapter(Context context, List<JsonBean> data) {
super();
this.mContext = context;
this.data = data;
imageLoader =new AsyncImageLoader();
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() {
return data.size();
} public Object getItem(int position) {
return data.get(position);
} public long getItemId(int position) {
return position;
} class ViewHolder {
private TextView title;
private ImageView imageView;
} // getView方法的实现
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
JsonBean jsonBean=data.get(position);
if(convertView ==null){
holder =new ViewHolder();
convertView=mInflater.inflate(R.layout.griditem, null);
holder.imageView=(ImageView)convertView.findViewById(R.id.imageView);
holder.title=(TextView)convertView.findViewById(R.id.title);
convertView.setTag(holder);;
}else{
holder=(ViewHolder)convertView.getTag();
}
return convertView;
}
}这个自定义的适配器中
/**
* 测试
*
* @param jsonString
* @return
*/
public static void getData(String jsonString) {
try {
System.out.println("----->>> jsonString = " + jsonString);
// 获取所有数据
JSONObject jsonObject = new JSONObject(jsonString);
System.out.println("----->>> jsonString = " + jsonObject.toString());
boolean success = jsonObject.getBoolean("success");
String respinfo = jsonObject.getString("respinfo");
int total = jsonObject.getInt("total");
JSONArray rows = jsonObject.getJSONArray("rows");
System.out.println("----->>> success = " + success);
System.out.println("----->>> respinfo = " + respinfo);
System.out.println("----->>> total = " + total);
System.out.println("----->>> rows = " + rows.toString()); } catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.json_test);
}
public void onClick(View v){
String jsonString = "{'success':true,'respinfo':null,'total':15,'rows':[{'test':'1'},{'test':'2'},{'test':'3'}]}";
switch (v.getId()) {
case R.id.button1:
//测试数据
JsonTools.getData(jsonString);
break;
default:
break;
}
}
}
/**
* 测试
*
* @param jsonString
* @return
*/
public static void getData(String jsonString) {
try {
System.out.println("----->>> jsonString = " + jsonString);
// 获取所有数据
JSONObject jsonObject = new JSONObject(jsonString);
System.out.println("----->>> jsonString = " + jsonObject.toString());
boolean success = jsonObject.getBoolean("success");
String respinfo = jsonObject.getString("respinfo");
int total = jsonObject.getInt("total");
//解析JSONArray数组
JSONArray rows = jsonObject.getJSONArray("rows");
for (int i = 0; i < rows.length(); i++) {
JSONObject temp = (JSONObject) rows.get(i);
String paramVersion = temp.getString("paramVersion");
String menuCode = temp.getString("menuCode");
String upMenuCode = temp.getString("upMenuCode");
String menuType = temp.getString("menuType");
String menuName = temp.getString("menuName");
String menuDisp = temp.getString("menuDisp");
String menuLogo = temp.getString("menuLogo");
String menuSort = temp.getString("menuSort");
String menuPath = temp.getString("menuPath");
String keyWord = temp.getString("keyWord");
String range = temp.getString("range");
String lastUptTime = temp.getString("lastUptTime");
String lastUptAcc = temp.getString("lastUptAcc");
String lastUptOrg = temp.getString("lastUptOrg");
}
System.out.println("----->>> success = " + success);
System.out.println("----->>> respinfo = " + respinfo);
System.out.println("----->>> total = " + total);
System.out.println("----->>> rows = " + rows.toString()); } catch (Exception e) {
e.printStackTrace();
}
}
哦,原来是操作UI啊,就是说加载图片,加载文字,到页面。用gridview形式表现出来呗?快下班了,不给你写代码了。
估计文字显示的问题,你应该会做,就是图片不会处理吧。至于UI更新问题也应该没问题(例如:Handler来更新),那么就是图片的处理:
我看见你有属性menuLogo是图片路径吧(图片在服务器的路径,对吧)那么你就用这个来获取图片吧。
下载此包:
导入名称:import net.tsz.afinal.FinalBitmap;
实例化:FinalBitmap fb = FinalBitmap.create(MainActivity.this);参数就是个context
用法:
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
String imgUrl = "http://192.168.1.1:8080/Test/images/store.png";
fb.display(imageView, imgUrl);
public ArrayList<Person> httpGetMainMenuList() throws Exception {
Log.i("select===", "httpGetMainMenuList");
String str1 = HttpJsonClient.httpGet(http_maingrideurl);
Log.i("select===", str1);
ArrayList<Person> localArrayList = new ArrayList<Person>();
JSONArray localJSONArray = getJSONObject(str1).optJSONArray("arg0");
for (int i = 0; ; i++)
{
if (i >= localJSONArray.length())
return localArrayList;
JSONObject localJSONObject = localJSONArray.getJSONObject(i);
String a= localJSONObject.optString("lastUptOrg");
String b= localJSONObject.optString("menuCode");
String v= localJSONObject.optString("upMenuCode");
String b= localJSONObject.optString("menuType");
String n= localJSONObject.optString("menuName");
String m= localJSONObject.optString("menuDisp");
String r= localJSONObject.optString("menuLogo");
...........................................................
Person c= new Person();
mm.setA(a);
..............................
....................................
......................................
.........................
localArrayList.add(mPerson );
}
}
public Map<String, String> getParams()
{
HashMap<String,String> localHashMap = new HashMap<String,String>();
return localHashMap;
} public JSONObject getJSONObject(String paramString)
throws Exception
{
return (JSONObject)new JSONTokener(paramString).nextValue();
}
}
这个更方便