访问网络数据
访问网络要在线程中执行,写一个NetworkRunnable类
package com.example.hasee.handler;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by hasee on 2016/8/30.
*/
public class NetworkRunnable implements Runnable,Handler.Callback{
private String url;
private MyCallback callback;
private Handler handler;
public NetworkRunnable(String url, MyCallback callback) {
this.url = url;
this.callback = callback;
handler = new Handler(Looper.getMainLooper(),this);
}
@Override
public void run() {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
int code = connection.getResponseCode();
if (code == 200) {
InputStream is = connection.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int length;
byte[] buffer = new byte[102400];
while ((length = is.read(buffer)) != -1) {
bos.write(buffer, 0, length);
}
String text = bos.toString("UTF-8");
handler.obtainMessage(0, text).sendToTarget();
} else {
RuntimeException exception = new RuntimeException("ResponseCode" + code);
String s = "服务器异常";
Bundle bundle = new Bundle();
bundle.putString("s", s);
bundle.putSerializable("exception", exception);
Message message = handler.obtainMessage(1);
message.setData(bundle);
handler.sendMessage(message);
}
} catch (Exception e) {
String s = "服务器异常";
Bundle bundle = new Bundle();
bundle.putString("s", s);
bundle.putSerializable("exception", e);
Message message = handler.obtainMessage(1);
message.setData(bundle);
handler.sendMessage(message);
}
}
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case 0:
callback.onSuccess((String)msg.obj);
break;
case 1:
Bundle data = msg.getData();
Exception exception = (Exception) data.getSerializable("exception");
String s = data.getString("s");
callback.onFail(exception,s);
break;
}
return true;
}
public interface MyCallback {
void onSuccess(String text);
void onFail(Exception e,String s);
}
}
在Activity中实现NetworkRunnable.MyCallback接口 执行
textView = (TextView) findViewById(R.id.main2);
NetworkRunnable runnable = new NetworkRunnable("http://www.baidu.com", this);
new Thread(runnable).start();
@Override
public void onSuccess(String text) {
textView.setText(text);
}
@Override
public void onFail(Exception e, String s) {
e.printStackTrace();
Toast.makeText(Main2Activity.this, s, Toast.LENGTH_SHORT).show();
}
获取网络图片
关键代码如下,其余代码参考上面
不获取百分比
InputStream is = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);//此方法不能获取百分比
handler.obtainMessage(0, bitmap).sendToTarget();
获取百分比
InputStream is = connection.getInputStream();
int contentLength = connection.getContentLength();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int length;
byte[] buffer = new byte[102400];
while ((length = is.read(buffer)) != -1) {
bos.write(buffer, 0, length);
int size = bos.size();
handler.obtainMessage(2, size, contentLength).sendToTarget();
}
byte[] bytes = bos.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
handlerMessage方法里执行
case 2:
float v = msg.arg1 * 100.0f / msg.arg2;
callback.onProgress(v);
break;
接口里方法
public interface MyCallback {
void onSuccess(Bitmap bitmap);
void onFail(Exception e, String s);
void onProgress(float percent);//获取百分比
}
主Activity里
@Override
public void onSuccess(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
@Override
public void onFail(Exception e, String s) {
e.printStackTrace();
Toast.makeText(Main2Activity.this, s, Toast.LENGTH_SHORT).show();
}
@Override
public void onProgress(float percent) {
textView.setText(String.format(Locale.CHINA,"%.2f%%",percent));
}