首页 > 程序开发 > 移动开发 > 其他 > 正文
Http请求解析Json与图片(AlertDialog)提示框
2017-08-11 10:35:00       个评论    来源:FH_94的博客  
收藏    我要投稿

Http请求解析Json与图片(AlertDialog)提示框。

public class HttpActivity extends AppCompatActivity {

    private ListView lv;
    List list;
    private MyHttpSqlite sqlite;
    private MyAdapter adapter;
    private Bitmap bit;

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

        lv = (ListView) findViewById(R.id.lv);
        list = new ArrayList();
        // 实例化工具类
        sqlite = new MyHttpSqlite(this);

        // 异步任务
        new AsyncTask() {

            @Override
            protected void onPostExecute(String s) {
                // 设置适配器
                adapter = new MyAdapter();
                lv.setAdapter(adapter);

                super.onPostExecute(s);
            }

            @Override
            protected String doInBackground(String... strings) {

                // 方法
                getData();

                return null;

            }
        }.execute();

        // 点击弹出AlertDialog删除
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, final int i, long l) {

                AlertDialog.Builder builder = new AlertDialog.Builder(HttpActivity.this);
                builder.setTitle("警告!");
                builder.setMessage("确定删除吗?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int j) {

                        delete(list.get(i).getTitle());
                        Toast.makeText(HttpActivity.this, list.get(i).getTitle() + "已删除", Toast.LENGTH_SHORT).show();
                        list.remove(i);
                        adapter.notifyDataSetChanged();

                    }
                });

                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        Toast.makeText(HttpActivity.this, "取消", Toast.LENGTH_SHORT).show();
                    }
                });

                builder.show();

            }
        });

    }

    // 删除的方法
    private void delete(String title) {

        SQLiteDatabase db = sqlite.getWritableDatabase();
        db.delete("hu", " title = ?", new String[]{title});
        db.close();

    }

    private void getData() {

        // 请求网络资源并解析

        try {
            URL url = new URL("http://169.254.19.1:8080/huhu/weixin.txt");

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestMethod("GET");

            int code = conn.getResponseCode();

            if (code == 200) {
                Gson gson = new Gson();
                Da da = gson.fromJson(new InputStreamReader(conn.getInputStream(), "UTF-8"), Da.class);
                list = da.getResult().getList();
                add(list);
            }

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

    }

    // 把解析的添加到数据库
    public void add(List list) {

        SQLiteDatabase db = sqlite.getWritableDatabase();

        for (int i = 0; i < list.size(); i++) {

            ContentValues valuses = new ContentValues();
            valuses.put("id", list.get(i).getId());
            valuses.put("title", list.get(i).getTitle());
            valuses.put("source", list.get(i).getSource());
            valuses.put("firstImg", list.get(i).getFirstImg());
            valuses.put("url", list.get(i).getUrl());
            db.insert("hu", null, valuses);
            db.close();
        }


    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {

            final ViewHolder holder;
 // 优化
            if (view == null) {
                holder = new ViewHolder();
                view = View.inflate(HttpActivity.this, R.layout.sw_item, null);
                holder.tv_id = view.findViewById(R.id.tv_id);
                holder.tv_title = view.findViewById(R.id.tv_title);
                holder.tv_source = view.findViewById(R.id.tv_source);
                holder.iv = view.findViewById(R.id.iv_firstImg);
                holder.tv_url = view.findViewById(R.id.tv_url);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }

            holder.tv_id.setText(list.get(i).getId());
            holder.tv_title.setText(list.get(i).getTitle());
            holder.tv_source.setText(list.get(i).getSource());
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final Bitmap image = getImage(list.get(i).getFirstImg());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            holder.iv.setImageBitmap(image);
                        }
                    });
                }
            }) {
            }.start();
            holder.tv_url.setText(list.get(i).getUrl());
            return view;
        }
    }

    // 获取网络图片
    private Bitmap getImage(String path) {

        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");

            int code = conn.getResponseCode();
            if (code == 200) {
                // 因为该地址是一个图片的地址,所以返回的数据流就是一个图片的数据流
                // 直接用该图片的数据流 转换成 一个Bitmap
                bit = BitmapFactory.decodeStream(conn.getInputStream());

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bit;
    }

    class ViewHolder {

        TextView tv_id, tv_title, tv_source, tv_firstImg, tv_url;
        ImageView iv;

    }
}
点击复制链接 与好友分享!回本站首页
上一篇:使用ImageLoader实例 进行加载图片
下一篇:第九章 删除表格行,定制Action按钮,MVC模式(二)
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训
版权所有: 红黑联盟--致力于做实用的IT技术学习网站