您有两个问题,第一个是如何解析sub json;第二个是如何解析sub
json。第二,如何从网上显示图像;我通过您的示例json网址成功进行了演示,如下所示:
首先,定义一个数据类:Data.java
package com.example.jsonparser;import java.util.ArrayList;public class Data { private String mState; private String mImageUrl; private String mTitle; private String mSummary; private ArrayList<String> mAddList; private String mUrl; public Data(String mState, String mImageUrl, String mTitle, String mSummary, ArrayList<String> addList, String mUrl) { super(); this.mState = mState; this.mImageUrl = mImageUrl; this.mTitle = mTitle; this.mSummary = mSummary; this.mAddList = addList; this.mUrl = mUrl; } public String getmState() { return mState; } public void setmState(String mState) { this.mState = mState; } public String getmImageUrl() { return mImageUrl; } public void setmImageUrl(String mImageUrl) { this.mImageUrl = mImageUrl; } public String getmTitle() { return mTitle; } public void setmTitle(String mTitle) { this.mTitle = mTitle; } public String getmSummary() { return mSummary; } public void setmSummary(String mSummary) { this.mSummary = mSummary; } public ArrayList<String> getmAddress1() { return mAddList; } public void setmAddress1(ArrayList<String> mAddress1) { this.mAddList = mAddress1; } public String getmUrl() { return mUrl; } public void setmUrl(String mUrl) { this.mUrl = mUrl; }}
其次,定义用于解析json字符串的解析器类:Parser.java
package com.example.jsonparser;import java.util.ArrayList;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.util.Log;public class Parser { private ArrayList<Data> mDataSource = new ArrayList<Data>(); public Parser(String json) { jsonParser(json); } private void jsonParser(String json) { try { JSonObject obj = new JSonObject(json); JSonObject var = obj.getJSonObject("data"); JSonArray list = var.getJSonArray("product"); int len = list.length(); for (int i = 0; i < len; i++) { try { JSonObject data = list.getJSonObject(i); String state = data.getString("state"); String image = data.getString("image"); String title = data.getString("title"); String summary = data.getString("summary"); String url = data.getString("url"); ArrayList<String> addList = new ArrayList<String>(); JSonArray addArray = data.getJSonArray("address"); int size = addArray.length(); for(int j = 0; j < size; j++) { addList.add(addArray.getJSonObject(j).getString("address")); } mDataSource.add(new Data(state, image, title, summary, addList, url)); } catch (JSonException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } catch (JSonException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public ArrayList<Data> getSource() { return mDataSource; }}
第三,通过URL定义任务下载图像:DownloadImageTask.java
package com.example.jsonparser;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.widget.ImageView;public class DownloadImageTask extends AsyncTask<Void, Void, Bitmap> { private String mUrl; private ImageView mImageView = null; public DownloadImageTask(String Url, ImageView imageView) { mUrl = Url; this.mImageView = imageView; } protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if (result != null) { mImageView.setImageBitmap(result); } } protected Bitmap doInBackground(Void... params) { Bitmap bitmap = getBitmap(mUrl); return bitmap; } public Bitmap getBitmap(String imageUrl) { Bitmap mBitmap = null; try { URL url = new URL(imageUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); InputStream is = conn.getInputStream(); mBitmap = BitmapFactory.depreStream(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return mBitmap; }}
第四,定义一个适配器扩展baseAdapter来显示数据和图像:Adapter.java
package com.example.jsonparser;import java.util.ArrayList;import android.app.Activity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AbsListView;import android.widget.AbsListView.RecyclerListener;import android.widget.baseAdapter;import android.widget.ImageView;import android.widget.TextView;public class Adapter extends baseAdapter { private static final String TAG = "Adapter"; private Activity mActivity; public ArrayList<Data> mObjects; static class ViewHolder { ImageView icon; TextView title; DownloadImageTask mTask; } public Adapter(Activity activity, ArrayList<Data> mObjects) { this.mActivity = (Activity) activity; this.mObjects = mObjects; } public void setObjects(ArrayList<Data> mObjects) { this.mObjects = mObjects; } @Override public View getView(int position, View convertView, ViewGroup parent) { Data item = mObjects.get(position); View rowView = convertView; if (rowView == null) { LayoutInflater inflater = mActivity.getLayoutInflater(); rowView = inflater.inflate(R.layout.item, parent, false); ViewHolder viewHolder = new ViewHolder(); viewHolder.icon = (ImageView) rowView.findViewById(R.id.image); viewHolder.title = (TextView) rowView.findViewById(R.id.title); rowView.setTag(viewHolder); } ViewHolder holder = (ViewHolder) rowView.getTag(); holder.title.setText(item.getmTitle()); holder.icon.setBackgroundResource(R.drawable.ic_launcher); holder.mTask = new DownloadImageTask(item.getmImageUrl(), holder.icon); if (!holder.mTask.isCancelled()) { holder.mTask.execute(); } return rowView; } @Override public int getCount() { return (this.mObjects.size()); } @Override public Object getItem(int position) { return (this.mObjects.get(position)); } @Override public long getItemId(int position) { return (position); } public AbsListView.RecyclerListener mRecyclerListener = new RecyclerListener( ){ public void onMovedToScrapHeap(View view) { ViewHolder viewHolder = (ViewHolder) view.getTag(); DownloadImageTask imagetask = viewHolder.mTask; if (imagetask != null) { imagetask.cancel(true); } } };}
最后,在活动中使用以下类:MainActivity.java
package com.example.jsonparser;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.widget.ListView;public class MainActivity extends Activity { private ListView mListView; private Adapter mAdapter; private ArrayList<Data> mSource = new ArrayList<Data>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(android.R.id.list); mAdapter = new Adapter(this, mSource); mListView.setAdapter(mAdapter); mListView.setRecyclerListener(mAdapter.mRecyclerListener); final String url = "http://metalgearrisingguide.com/samplejson.json"; new Thread(new Runnable() { @Override public void run() { Parser parser = new Parser(getJSonfromURL(url)); mSource = parser.getSource(); Message msg = mHandler.obtainMessage(); mHandler.sendMessage(msg); } }).start(); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { mAdapter.setObjects(mSource); mAdapter.notifyDataSetChanged(); } }; public String getJSonfromURL(String url) { InputStream is = null; String result = ""; // http post try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } // convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "n"); } is.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } return result; }}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="@android:color/transparent" /></LinearLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/image" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="left|top" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ellipsize="end" android:gravity="left|top" android:lineSpacingExtra="3.0dp" android:textColor="#ffffff" android:textSize="15sp" /></LinearLayout>
就这样。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)