get获取图片和post注册
效果图
布局文件
// An highlighted block
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="bw.com.day13.Main13Activity">
<Button
android:id="@+id/dowloadFile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载文件"
/>
<Button
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注册"
/>
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Java代码
// An highlighted block
package bw.com.day13;
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.FileReader;
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;
import java.net.URLConnection;
import bw.com.day1.R;
public class Main13Activity extends AppCompatActivity implements View.OnClickListener {
private HandlerThread handlerThread;
private Handler handler;
private final int COUNT = 1;
private final int COUNT1 = 2;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main13);
findViewById(R.id.dowloadFile).setOnClickListener(this);
findViewById(R.id.register).setOnClickListener(this);
imageView = findViewById(R.id.imageview);
handlerThread = new HandlerThread("http");
handlerThread.start();
handler = new HttpHandler(handlerThread.getLooper());//让handler消息运行子线程
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.dowloadFile:
handler.sendEmptyMessage(COUNT);
break;
case R.id.register:
handler.sendEmptyMessage(COUNT1);
break;
}
}
private class HttpHandler extends Handler{
public HttpHandler(Looper looper){
super(looper);
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case COUNT:
dowloadFile();
break;
case COUNT1:
registerUser();
break;
}
}
//post请求
private void registerUser() {
String registerUrl = "http://169.254.230.253:8080/register";
try {
URL url = new URL(registerUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");//post请求
urlConnection.setConnectTimeout(1000*5);
urlConnection.setReadTimeout(1000*5);
urlConnection.setDoInput(true);//允许从服务端获取数据
urlConnection.setDoOutput(true);//允许写入
urlConnection.setRequestProperty("Content-type","application/x-www-form-urlencoded");//以表单的形式传递参数
String postParms = "name=aBin&password=12345&verifyCode=8888";
OutputStream os = urlConnection.getOutputStream();
os.write(postParms.getBytes());//把参数发送过去
os.flush();
final StringBuffer buffer = new StringBuffer();
int code = urlConnection.getResponseCode();
if (code == 200){//成功
InputStream is = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;//一行一行读
while ((line = br.readLine())!=null){
buffer.append(line);//把一行数据拼接到buff里面
}
runOnUiThread(new Runnable() {
@Override
public void run() {
//把服务端的数据提示出来
Toast.makeText(Main13Activity.this, buffer.toString(), Toast.LENGTH_SHORT).show();
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
//需要放到子线程中,使用HandelerThread,
private void dowloadFile() {
String downLoadUrl = "http://pic41.nipic.com/20140508/18609517_112216473140_2.jpg";
String savePath = "/sdcard/flowergirl.jpg";
File file = new File(savePath);
if (file.exists()){
file.delete();//文件存在 删除掉
}
BufferedInputStream bis = null;
FileOutputStream os = null;
try {
URL url = new URL(downLoadUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(1000*5);
connection.setReadTimeout(1000*5);
connection.setDoInput(true);
connection.connect();
//代表请求成功
if (connection.getResponseCode() == 200){
InputStream is = connection.getInputStream();
bis = new BufferedInputStream(is);//文件输入流
os = new FileOutputStream(savePath);
byte[] bytes = new byte[10224];
int len = -1;
while ((len = bis.read(bytes))!=-1){
os.write(bytes,0,len);//写入文件
}
os.flush();//强制把数据写入磁盘
}
final Bitmap bitmap = BitmapFactory.decodeFile(savePath);
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (bis != null){
bis.close();
}
if (os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}