1. 程式人生 > >HttpURLConnection和H

HttpURLConnection和H

MainActivity

package com.example.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;

public class MainActivity extends AppCompatActivity {
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”;
String tag=“錯了”;
private ImageView img_view;
private TextView text_view;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what1){
String jsonstr = (String) msg.obj;
text_view.setText(jsonstr);
}else if (msg.what

2){
Bitmap bitmap= (Bitmap) msg.obj;
img_view.setImageBitmap(bitmap);
}
}
};
/*Handler handler =new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what==1){
String jsonstr = (String) msg.obj;
text_view.setText(jsonstr);
}

    }
};*/
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img_view = findViewById(R.id.img_view);
    text_view = findViewById(R.id.text_view);
    setOnlickListener();
}

private void setOnlickListener() {
    text_view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        //申請許可權
            new Thread(){
                @Override
                public void run() {
                    super.run();
                   String jsonstr= NetUtil.getjson(urlString);
                    Message message = handler.obtainMessage();
                    message.what=1;
                    message.obj=jsonstr;
                    handler.sendMessage(message);

                }
            }.start();
        }
    });
img_view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new Thread() {
            @Override
            public void run() {
                super.run();
                Bitmap mbitmap = NetUtil.getBitmap(urlBitmap);
                Message message = handler.obtainMessage();
                message.what = 2;
                message.obj = mbitmap;
                handler.sendMessage(message);
            }

        }.start();
    }
});

}

}

class NetUtil {

public static Bitmap getbitmap(String string) {
    try {
        URL url = new URL(string);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setConnectTimeout(8000);
        int responseCode = urlConnection.getResponseCode();
        if (responseCode==200){
            InputStream inputStream = urlConnection.getInputStream();
           /* BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder builder=new StringBuilder();
            String str="";*/
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

public static String getjson(String string) {

    try {
        URL url = null;
        url = new URL(string);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setConnectTimeout(8000);
        int responseCode = urlConnection.getResponseCode();
        if (responseCode==200){
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder builder=new StringBuilder();
            String str="";
            while ((str=reader.readLine())!=null){
                builder.append(str);
            }
            return builder.toString();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

}