1、6.Error:Execution failed for task ':app:buildInfoDebugLoader'.
> Exception while doing past iteration backup : Source G:\project\***\app\build\intermediates\builds\debug\37215488613481\classes.dex and destination G:\project\***\app\build\intermediates\builds\debug\37215488613481\classes.dex must be different

这种问题不知道怎么出现的,clean一下即可。

2、Imageview加载网络图片,要用Handler。

3、ImageView设置图片的比例问题。http://blog.csdn.net/hhbgk/article/details/8101676

4、取消标题栏,只要设置一下Theme即可。

第一步是爬取相关的文章标题,图片,以及地址。

用的是普通的java写的,还有用了htmlunit的库比较方便,为什么要这样,还得学下css

源网址:http://daily.zhihu.com

出来的效果大概是这样

仿知乎日报App-LMLPHP

代码:

import java.io.IOException;
import java.net.MalformedURLException; import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage; public class main { public static void main(String args[]) throws FailingHttpStatusCodeException, MalformedURLException, IOException{ String url = "http://daily.zhihu.com"; WebClient webClient = new WebClient(BrowserVersion.CHROME); //把警告弄掉的
webClient.getOptions().setJavaScriptEnabled(false); webClient.getOptions().setCssEnabled(false); HtmlPage page = webClient.getPage(url); //System.out.println(page.asXml()); //DomNode content = page.querySelector(".main-wrap.content-wrap");
//System.out.println(content.asXml()); //找到所有wrap的类
DomNodeList<DomNode> iList = page.querySelectorAll(".wrap"); for(DomNode i: iList){ HtmlAnchor a = (HtmlAnchor)i.querySelector("a");
HtmlImage img = (HtmlImage)i.querySelector("img"); // 获取 a 标签的属性 href ,就是帖子详情的地址啦!!
String href = a.getAttribute("href");
String src = img.getAttribute("src");
String title = i.asText(); System.out.println(title);
System.out.println(href);
System.out.println(src); }//end for } }

第二步:怎么处理获得的数据?

就是UI的问题,这个暂时没有设计好- -

就是网上的图片获取之后弄成bitmap,然后在handleMessage里面设置到ImageView里面

还有就是涉及网络的任务必须新开一个线程,不能在主线程执行

代码:

public class MainActivity extends AppCompatActivity {

    WebView webView;
ImageView imageView;
Bitmap bitmap; Handler handler; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); handler = new Handler(){ @Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Log.d("TAG","getMessage"); imageView.setImageBitmap(bitmap); }
}; /*
webView = (WebView)findViewById(R.id.webView); webView.loadUrl("http://daily.zhihu.com/story/8746558");
*/ imageView = (ImageView)findViewById(R.id.imageview1); new Thread(new Runnable() {
@Override
public void run() { try {
getImageViewInputStream(); } catch (IOException e) {
e.printStackTrace();
} Message message = new Message();
handler.sendMessage(message); }
}).start(); } public void getImageViewInputStream() throws IOException {
InputStream inputStream = null;
URL url = new URL("http://pic1.zhimg.com/96b31814276e24e991064920567ed9e4.jpg"); //服务器地址
if (url != null) {
//打开连接
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
//httpURLConnection.setConnectTimeout(3000);//设置网络连接超时的时间为3秒
httpURLConnection.setRequestMethod("GET"); //设置请求方法为GET
httpURLConnection.setDoInput(true); //打开输入流
httpURLConnection.connect();
int responseCode = httpURLConnection.getResponseCode(); // 获取服务器响应值
if (responseCode == HttpURLConnection.HTTP_OK) { //正常连接
inputStream = httpURLConnection.getInputStream(); //获取输入流
bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
Log.d("TAG","getOK");
} } } }

第三步:怎么处理文章?

当时还在纠结一堆html格式的怎么解析,后来想到可以用WebView偷懒。直接把文章的网址放在WebView里面就好了

05-11 19:31