Afinal的使用(二):使用FinalHttp抓取网页内容

直接上代码,使用FinalHttp抓取百度网页内容:

官方的示例有错误的地方,官方onSuccess的参数是String类型,可是这个0.5版的改成了Object类型,其实本来就是一个字符串,不知道为什么要用Object类型

package com.example.app1;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FinalActivity  {

    @ViewInject(id = R.id.button1)
    private Button button1;
    
    @ViewInject(id = R.id.textView1)
    private TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        FinalHttp fh = new FinalHttp();
        fh.get("http://www.baidu.com/", new AjaxCallBack(){

            @Override
            public void onSuccess(Object t) {
                textView1.setText(t.toString());
            }
            
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                textView1.setText(strMsg);
            }

        });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

使用post请求的例子:

该例子需要传递参数到要抓取的页面,抓取指定日期余额宝的万份收益

package com.example.app1;

import java.io.ByteArrayInputStream;
import java.io.File;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends FinalActivity {

    @ViewInject(id = R.id.button1)
    private Button button1;

    @ViewInject(id = R.id.textView1)
    private TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AjaxParams params = new AjaxParams();
        params.put("method", "find");
        params.put("date", "2014-01-25");
        params.put("fundcode", "000198");

        FinalHttp fh = new FinalHttp();
        fh.post("http://www.thfund.com.cn/calculator.do", params,
                new AjaxCallBack() {

                    @Override
                    public void onSuccess(Object t) {
                        textView1.setText(t.toString());
                    }

                    @Override
                    public void onFailure(Throwable t, int errorNo,
                            String strMsg) {
                        textView1.setText(strMsg);
                    }

                });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

 

posted @ 2014-01-27 15:21  魔豆  阅读(6483)  评论(1编辑  收藏  举报