关于Android中http请求Gosn解析的一些个人见解:
首先是XML中构建布局:
在布局里面建一个listview用来展示Gson解析的字符
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.maohuawei.work.MainActivity"> <ListView android:id="@+id/list_view" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
然后建一个listview内的布局item:
关于item我也就不多述了
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="150dp" android:layout_margin="10dp" > <ImageView android:layout_margin="10dp" android:padding="10dp" android:id="@+id/image_view_item" android:layout_width="60dp" android:layout_height="60dp"/> <TextView android:layout_margin="10dp" android:padding="10dp" android:layout_toRightOf="@id/image_view_item" android:id="@+id/tv_title_item" android:textSize="25sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:layout_toRightOf="@id/image_view_item" android:layout_margin="10dp" android:padding="10dp" android:layout_below="@id/tv_title_item" android:textSize="20sp" android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:textColor="#ff0526" android:id="@+id/tv_number" android:layout_margin="10dp" android:padding="10dp" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </RelativeLayout>
然后是主类:
import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import com.google.gson.Gson; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private static final String TAG = MainActivity.class.getSimpleName(); private ListView list_view; private String data; private List<RootBean.NewslistEntity.NewsEntity> list; private MyBaseAdapter adapter;
//handler耗时操作
Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); list_view.setAdapter(adapter); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list_view = (ListView) findViewById(R.id.list_view);
//Gson包导过来 final Gson gson = new Gson(); list = new ArrayList<>();
//new一个线程 new Thread(new Runnable() { @Override public void run() {
//调用getData(); data = getData(); Log.e(TAG, "run: " + data);
//做Gson解析 RootBean rootBean = gson.fromJson(data, RootBean.class); RootBean.NewslistEntity newslist = rootBean.getNewslist(); List<RootBean.NewslistEntity.NewsEntity> news = newslist.getNews(); //往list里添加数据
list.addAll(news); adapter = new MyBaseAdapter(MainActivity.this, list); handler.sendEmptyMessage(1); } }).start(); } private String getData() { try {
//做网络请求 URL url = new URL("http://huixinguiyu.cn/Assets/js/newsnew.js"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(1024 * 8); connection.setReadTimeout(1024 * 8); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == 200) { InputStream inputStream = connection.getInputStream(); int len = 0; //缓冲区 一次读取1024*8 byte[] buf = new byte[1024 * 8]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((len = inputStream.read(buf)) != -1) { baos.write(buf, 0, len); } baos.close();
//返回值 return baos.toString(); } else { Log.e(TAG, "onCreate: Error" + responseCode); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }
然后listview适配器类:
private Context context; private List<RootBean.NewslistEntity.NewsEntity> list; public MyBaseAdapter(Context context, List<RootBean.NewslistEntity.NewsEntity> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder = null; if (view == null) { holder = new ViewHolder(); view = view.inflate(context, R.layout.item, null); holder.image_view_item = view.findViewById(R.id.image_view_item); holder.tv_title_item = view.findViewById(R.id.tv_title_item); holder.tv_content = view.findViewById(R.id.tv_content) holder.tv_number = view.findViewById(R.id.tv_number); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.tv_title_item.setText(list.get(i).getTitle()); holder.tv_content.setText(list.get(i).getDetail()); holder.tv_number.setText(list.get(i).getComment()); ImageLoaderUtils.setImageView(list.get(i).getImage(), context, holder.image_view_item); return view; } static class ViewHolder { TextView tv_title_item, tv_content, tv_number; ImageView image_view_item; } }
再然后是json串工具类:
public class RootBean { private List<NewsEntity> news; public void setNews(List<NewsEntity> news) { this.news = news; } public List<NewsEntity> getNews() { return news; } public static class NewsEntity { private String title; private String detail; private String comment; private String image; public void setTitle(String title) { this.title = title; } public void setDetail(String detail) { this.detail = detail; } public void setComment(String comment) { this.comment = comment; } public void setImage(String image) { this.image = image; } public String getTitle() { return title; } public String getDetail() { return detail; } public String getComment() { return comment; } public String getImage() { return image; } } } }
最后是imageLoader:public class ImageLoaderUtils { public static void setImageView(String url, Context context, ImageView imageView) { //初始化 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context).build(); //创建Imageloader 对象 ImageLoader imageLoader = ImageLoader.getInstance(); //设置初始化 imageLoader.init(config); DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) //设置图片在下载期间显示的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.mipmap.ic_launcher) //设置图片加载/解码过程中错误时候显示的图片 .cacheInMemory(true)//设置下载的图片是否缓存在内存中 .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中 .imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型 .build();//构建完成 imageLoader.displayImage(url, imageView, options); } }
以上都是本人练习时的一些作品,如有错误之处请指教。