本文主要根据代码实例来分析安卓客户端获取网络的图片。
1、把流转变为字节数组的工具类
}
2、获取网页代码的业务类
public class HtmlService {
/**
* get code from web service
* @throws IOException
*/
public static String getHtml(String path) throws IOException{
URL url = new URL(path) ;
HttpURLConnection conn = (HttpURLConnection) url.openConnection() ;
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
InputStream inStream = conn.getInputStream() ;
byte[] data = SteamTool.read(inStream) ;
return new String(data) ;
}else{
Toast.makeText(null, "Server is no response.", 1).show();
return null;
}
}
}
3、编写主activity,然后调用showHtml()方法
public void showHtml(TextView v, String path) {
//此处使用TextView 来显示网页的代码,可以根据需要进行修改
String html;
try {
html = HtmlService.getHtml(path);
v.setText(html);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(getApplicationContext(), "this is wrong.", 1).show();
}
}
调用:
String url = "http://172.27.1.260:8081/AndroidServlet/webCode.jsp" ;
//此处tv_code为TextView 实例对象
showHtml(tv_code, url);