package com.example.day56;import java.io.BufferedReader;import java.io.InputStreamReader;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import android.util.Log;public class tools { HttpClient httpclient=new DefaultHttpClient(); public String http(String path){ HttpGet get=new HttpGet(path+"?"+"json"); HttpResponse httpResponse; try { httpResponse = httpclient.execute(get); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { // 读取服务器响应 BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); String line = null; StringBuilder code=new StringBuilder(); while((line = br.readLine())!=null){ code=code.append(line); } String condes=new String(code); Log.e("黄柳淞", condes); Log.v("黄柳淞", condes); Log.i("黄柳淞", condes); return condes; } } catch (Exception e) { e.printStackTrace(); } return null; }}
该类为获取网页源代码的工具类,为了方便日后减少代码的编写量,写了这个工具类可以随时调用
该类返回的是一个网站的全部源码String类型,可以直接数据,也可以直接打印。 ———————————————————————————————