1. 程式人生 > >圖片和文字網路介面聯絡

圖片和文字網路介面聯絡

MainActivity.class 聯網許可權

package com.example.kanghuwei.day01;

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

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    private ImageView image_name;
    private TextView text_name;
    public String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
    String urlBitmap = "http://img.my.csdn.net/uploads/201407/26/1406383265_8550.jpg";

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                Bitmap bitmap = (Bitmap) msg.obj;
                image_name.setImageBitmap(bitmap);
            } else if (msg.what == 2) {
                text_name.setText((String) msg.obj);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image_name = findViewById(R.id.image_name);
        text_name = findViewById(R.id.text_name);
        image_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        //呼叫工具類
                        Bitmap bitmap = NetWorkUtils.getBitmap(urlBitmap);
                        Message message = handler.obtainMessage();
                        message.obj = bitmap;
                        message.what = 1;
                        handler.sendMessage(message);
                    }
                }.start();
            }
        });
        text_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        //呼叫工具類
                        try {
                            String json = NetWorkUtils.getJson(urlString);
                            Message message = handler.obtainMessage();
                            message.obj = json;
                            message.what = 2;
                            handler.sendMessage(message);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }
}

NetWorkUtils工具類

package com.example.kanghuwei.day01;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

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;

public class NetWorkUtils {

    public static Bitmap getBitmap(String urlBitmap) {
        try {
            URL url = new URL(urlBitmap);
            try {
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                int responseCode = urlConnection.getResponseCode();
                if (responseCode == 200) {
                    InputStream inputStream = urlConnection.getInputStream();
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                } else {
                    Log.e("khw", "responseCode:" + responseCode);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getJson(String urlString) throws IOException {
        URL url = new URL(urlString);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        int responseCode = urlConnection.getResponseCode();
        if (responseCode == 200) {
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String temp = "";
            StringBuilder stringBuilder = new StringBuilder();
            while ((temp = bufferedReader.readLine()) != null) {
                stringBuilder.append(temp);
            }
            //錯
            return stringBuilder.toString();
        } else {
            Log.e("khw", "responseCode--json" + responseCode);
        }
        return "";
    }
}