1>從網絡上獲取數據(圖片,網頁,xml,json等)
A.從網絡上獲取一張圖片,然後顯示到手機上
這是在java中
public class ImageRequest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
URL url = new URL("/wp-content/images1/20181012/20120419023209724712.jpg");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();//通過輸入流獲取圖片數據
byte[] data = readInputStream(inStream);//得到圖片的二進制數據
File imageFile = new File("itcast.jpg");//保存在項目下
FileOutputStream outStream = new FileOutputStream(imageFile);
outStream.write(data);
outStream.close();
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];//定義一個1k的緩沖區
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){//返回的是實際的字節數
outStream.write(buffer, 0, len);//將緩沖區的數據寫入到內存中
}
inStream.close();
return outStream.toByteArray();
}
}
在手機上
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String path = pathText.getText().toString();
try {
byte[] data = ImageService.getImage(path);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);//生成位圖
imageView.setImageBitmap(bitmap);//顯示圖片
} catch (Exception e) {
Toast.makeText(ImageShowActivity.this, R.string.error, 1).show();
Log.e(TAG, e.toString());
}
}});
<!– 訪問網絡的權限 –>
<uses-permission android:name="android.permission.INTERNET"/>
public class StreamTool {
/**
* 從輸入流中獲取數據
* @param inStream 輸入流
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
—————————————————————————————————–
public class ImageService {
public static byte[] getImage(String path) throws Exception {
URL url = new URL("/wp-content/images1/20181012/20120419023209724712.jpg");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream();//通過輸入流獲取圖片數據
return StreamTool.readInputStream(inStream);//得到圖片的二進制數據
}
}
B.從網絡上獲取網頁(用的比較少)
byte[] data = readInputStream(inStream);//得到網頁的二進制數據
String html = new String(data,"gb2312");
C.從網絡上獲取xml數據,然後顯示在手機上
android手機模擬器本身綁定在模擬器上,所以訪問web service的時候不能用local host
或者用127.0.0.1。而應該使用局域網上的ip地址。
public class VideoService {
/**
* 獲取最新的視頻資訊
* @return
* @throws Exception
*/
public static List<Video> getLastVideos() throws Exception{
String path = "http://192.168.1.100:8080/videoweb/video/list.do";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(5*1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
return parseXML(inStream);
}
/**
* 解析服務器返回的協議,得到視頻資訊
* @param inStream
* @return
* @throws Exception
*/
private static List<Video> parseXML(InputStream inStream) throws Exception{
List<Video> videos = null;
Video video = null;
XmlPullParser parser = Xml.newPullParser();
parser.setInput(inStream, "UTF-8");
int eventType = parser.getEventType();//產生第一個事件
while(eventType!=XmlPullParser.END_DOCUMENT){//隻要不是文檔結束事件
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
videos = new ArrayList<Video>();
break;
case XmlPullParser.START_TAG:
String name = parser.getName();//獲取解析器當前指向的元素的名稱
if("video".equals(name)){
video = new Video();
video.setId(new Integer(parser.getAttributeValue(0)));
}
if(video!=null){
if("title".equals(name)){
video.setTitle(parser.nextText());//獲取解析器當前指向元素的下一個文本節點的值
}
if("timelength".equals(name)){
video.setTime(new Integer(parser.nextText()));
}
}
break;
case XmlPullParser.END_TAG:
if("video".equals(parser.getName())){
videos.add(video);
video = null;
}
break;
}
eventType = parser.next();
}
return videos;
}
}
D.從網絡上獲取json數據,然後顯示在手機上
public class VideoService {
/**
* 獲取最新的視頻資訊
* @return
* @throws Exception
*/
public static List<Video> getJSONLastVideos() throws Exception{
List<Video> videos = new ArrayList<Video>();
String path = "http://192.168.1.100:8080/videoweb/video/list.do?format=json";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(5*1000);
conn.setRequestMethod("GET");
InputStream inStream = conn.getInputStream();
byte[] data = StreamTool.readInputStream(inStream);
String json = new String(data);
JSONArray array = new JSONArray(json);
for(int i=0 ; i < array.length() ; i++){
JSONObject item = array.getJSONObject(i);
int id = item.getInt("id");
String title = item.getString("title");
int timelength = item.getInt("timelength");
videos.add(new Video(id, title, timelength));
}
return videos;
}
}
摘自 虛懷若谷