在网址中读取照片和字符串

写一个工具类

package com.lbp.demo3.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * author:Created by WangZhiQiang on 17/11/2.
 */

public class GetJsonUtil {
    public static  String getjson(String tvuri){
        try {
            URL url = new URL(tvuri);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream stream = connection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
            StringBuilder stringBuilder = new StringBuilder();
            String line="";
            while((line=bufferedReader.readLine())!=null){
                stringBuilder.append(line);
            }
            String s = stringBuilder.toString();
            return s;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static Bitmap getbitmap(String inauri){
        try {
            URL url = new URL(inauri);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(8000);
            int code = urlConnection.getResponseCode();
            if(code==200){
                InputStream stream = urlConnection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(stream);
                return bitmap;
            }else{

            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}

在Activity中写出来
 
package com.lbp.demo3;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lbp.demo3.bean.MyBean;
import com.lbp.demo3.util.GetJsonUtil;

public class MainActivity extends AppCompatActivity {

    private ImageView ima;
    private TextView tv1;
    public String strurl="http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
    public String imgrl="https://img-my.csdn.net/uploads/201407/26/1406383265_8550.jpg";
    private  Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int i = msg.what;
            switch(i){
                case 1:
                    String str = (String)msg.obj;
                    tv1.setText(str);
                    break;
                case 2:
                    Bitmap bitmap = (Bitmap) msg.obj;
                    ima.setImageBitmap(bitmap);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.tv1);
        ima = (ImageView) findViewById(R.id.ima);
        setclickListnener();
    }

    private void setclickListnener() {
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        String s = GetJsonUtil.getjson(strurl);
                        Gson gson  = new Gson();
                        MyBean myBean = gson.fromJson(s, MyBean.class);
                        String info = myBean.getInfo();
                        Message msg = new Message();
                        msg.obj=info;
                        msg.what=1;
                        handler.sendMessage(msg);
                    }
                }.start();
            }
        });

        ima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        Bitmap bitmap = GetJsonUtil.getbitmap(imgrl);
                        Message message = handler.obtainMessage();
                        message.obj=bitmap;
                        message.what=2;
                        handler.sendMessage(message);
                    }
                }.start();
            }
        });
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值