第二阶段冲刺(1)
第二阶段的主要工作在于主界面数据的呈现,尤其是图片的呈现,因为爬到的数据是网页连接,但安卓不想HTML直接可以用超链接把网上的图片显示出来,
安卓需要通过HttpClient来对网址进行解析,
package com.hui.newproject01.ApacheHttpClient;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
public class ApacheHttpClient {
private static final String TAG = "Error";
public InputStream httpGet(String url) {
InputStream result = null;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
int httpStatus = httpResponse.getStatusLine().getStatusCode();
if (httpStatus == HttpStatus.SC_OK) {
InputStream in = httpResponse.getEntity().getContent();
try {
result = in;
} catch (Exception e) {
Log.i(TAG, "Exception");
e.printStackTrace();
}
} else {
result = null;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.i(TAG, "ClientProtocolException");
} catch (IOException e) {
e.printStackTrace();
Log.i(TAG, "ClientProtocolException");
}
return result;
}
public Bitmap getHttpBmp(String url) {
Bitmap bm = null;
InputStream is = httpGet(url);
bm = BitmapFactory.decodeStream(is);
return bm;
}
}
通过这个类的方法可将网址进行解析,并拿到图片数据,但注意要申请网络权限。
浙公网安备 33010602011771号