主类代码
package com.example.unit13;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
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.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
ImageView imageView;
HandlerThread thread;
private HttpHandler handler;
private static final int DOWNLOAD=1;//GET Handler发送码
private static final int REGISTER=2;//POST Handler发送码
InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=findViewById(R.id.image1);
findViewById(R.id.load).setOnClickListener(this);
findViewById(R.id.post).setOnClickListener(this);
thread=new HandlerThread("thread");
thread.start();
handler=new HttpHandler(thread.getLooper());//让hanlder消息运行子线程
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.load:
downLoadFile();
handler.sendEmptyMessage(DOWNLOAD);
break;
case R.id.post:
handler.sendEmptyMessage(REGISTER);
break;
default:break;
}
}
private void registerUser(){
String registerUrl="http://169.254.230.253:8080/register";
try {
URL url = new URL(registerUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setDoInput(true);//允许读取
connection.setDoOutput(true);//允许写入
connection.setRequestProperty("content-type","application/x-www-form-urlencoded");//以表单形式传递参数
String postParms="name=1702c&password=12345&verifyCode=8888";
OutputStream oos=connection.getOutputStream();
oos.write(postParms.getBytes());//把参数发送过去
oos.flush();
final StringBuffer sb=new StringBuffer();
int code=connection.getResponseCode();
if(code==200){//成功
InputStream is = connection.getInputStream();
BufferedReader bis=new BufferedReader(new InputStreamReader(is));
String line=null;//一行一行读
while((line=bis.readLine())!=null){
sb.append(line);//拼接
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//把获取的数据提示出来
Toast.makeText(MainActivity.this,sb.toString(),Toast.LENGTH_SHORT).show();
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
private class HttpHandler extends Handler{
public HttpHandler(Looper loopers){
super(loopers);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case DOWNLOAD:
downLoadFile();
break;
case REGISTER:
registerUser();
break;
}
}
}
//需要放到子线程中去 使用HandlerTjread
private void downLoadFile() {
String downloadURL="http://pic34.nipic.com/20131020/6704106_203943375000_2.jpg";
// String downloadURL="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561115440688&di=ac408b4a82c7962ce3894450b0299ba8&imgtype=0&src=http%3A%2F%2Fp1.qhmsg.com%2Fdmsmty%2F898_709_%2Ft01a57725210bedecaf.jpg";
String savePah="/sdcard/bigbig.jpg";
BufferedInputStream bis=null;
FileOutputStream fos=null;
File file=new File(savePah);
if(file.exists()){
file.delete();//如果文件存在,删掉
}
try {
URL url = new URL(downloadURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setDoInput(true);
connection.connect();
//代表请求成功
if(connection.getResponseCode()==200){
is = connection.getInputStream();
//文件输入流
bis=new BufferedInputStream(is);
//文件输出流
fos=new FileOutputStream(savePah);
byte[] bytes=new byte[1024];
int len=0;
while ((len=bis.read(bytes))!=-1){
fos.write(bytes,0,len);//写入文件
}
fos.flush();//强制把数据写入磁盘
}
final Bitmap bitmap= BitmapFactory.decodeFile(savePah);
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(bis!=null){
bis.close();
}
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
效果图