本帖最后由 u010814841 于 2014-01-25 12:42:23 编辑

解决方案 »

  1.   


    package com.example.http_demo;import android.util.Log;
    import org.apache.http.HttpResponse;
    import org.apache.http.StatusLine;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.params.BasicHttpParams;
    import org.apache.http.params.HttpConnectionParams;
    import org.apache.http.params.HttpParams;
    import org.apache.http.util.EntityUtils;
    import java.io.IOException;/**
     * Created by gaofeng on 14-1-14.
     */
    public class HttpCaller {
        public String httpGet(String url) {
            Log.d("httpGet", "url:" + url);
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet _httpGet = new HttpGet(url);
            try {
                HttpParams params = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(params,20000);
                _httpGet.setParams(params);            HttpResponse httpResponse = httpClient.execute(_httpGet);
                StatusLine _status = httpResponse.getStatusLine();            if (_status.getStatusCode() == 200) {
                    Log.d("httpGet", "req is 200.");
                    return EntityUtils.toString(httpResponse.getEntity());
                }
            } catch (IOException e) {
                e.printStackTrace();
                return e.getMessage();
            }
            return null;
        }
    }
    package com.example.http_demo;import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;/**
     * gaofeng
     */
    public class MyActivity extends Activity {    Button btn;
        EditText editText;
        TextView textView;    /**
         * Called when the activity is first created.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);        btn = (Button) findViewById(R.id.btn1);
            editText = (EditText) findViewById(R.id.edit1);        btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    run();
                }
            });
            textView = (TextView) findViewById(R.id.content);    }
        void run() {
            final String url = editText.getText().toString();
            new Thread() {
                public void run() {
                    String content = new HttpCaller().httpGet(url);
                    showContent(content);
                }
            }.start();
        }
        void showContent(final String content) {
            this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    textView.setText(content);
                }
            });
        }
    }
      

  2.   

    我运行了您给的例子,可是它还是在Tomcat上模拟,并没有抓取实际网站的数据呀。而且,您也没说在哪段代码写要抓取的网站的域名或ID.本人菜鸟,勿怪。
      

  3.   

    二楼的那个DEMO只能在4.0以下能使用,代码没错,但是需要重启一个线程,才能正常使用,楼主,需要加我QQ 在家里代码没带!回公司给你!664=====51===070
      

  4.   

    而且他这个demo似乎没有联网啊,不是还得用到HttpURLConnection!
    如下:
    public void captureHtml(String ip) throws Exception {  
        String strURL = "http://ip.chinaz.com/?IP=" + ip;  
        URL url = new URL(strURL);  
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  
        InputStreamReader input = new InputStreamReader(httpConn  
                .getInputStream(), "utf-8");  
        BufferedReader bufReader = new BufferedReader(input);  
        String line = "";  
        StringBuilder contentBuf = new StringBuilder();  
        while ((line = bufReader.readLine()) != null) {  
            contentBuf.append(line);  
        }  
        String buf = contentBuf.toString();  
        int beginIx = buf.indexOf("查询结果[");  
        int endIx = buf.indexOf("上面四项依次显示的是");  
        String result = buf.substring(beginIx, endIx);  
        System.out.println("captureHtml()的结果:\n" + result);  
    }  
      

  5.   

    android-async-http 这个库http://loopj.com/android-async-http/
    还有诸如com.ning打头的一个网络库,当然它需要选择网络后台,netty就是常见选择
    https://github.com/AsyncHttpClient/async-http-client
    至于绑定控件之类的还是自己处理吧
    这些都是开源的,用不用看具体需要,没必要自己写,它们的用法都写的很明白
      

  6.   

    /**
         * 根据url获得页面源码, 调用web接口
         * @param url 访问地址, 必备参数
         * @param method 请求方式post还是get, 默认get
         * @param params 参数列表 post必备, 比如:"name=张三&age=18"
         * @param sessionInfo 可以保持session, 默认不保持
         * @param encoding 编码格式, 默认UTF_8
         * @param isLine 得到的源码是否换行, 默认false
         * @return
         */
    public String sound(String url, String method, String params, String sessionInfo, String encoding, boolean isLine) throws Exception {
    Log.e("AbstractActivity", "调用web接口:" + url);
    encoding = (isBlank(encoding) ? HTTP.UTF_8 : encoding);
    method = (isBlank(method) ? Domain.REQUEST_GET : method.toUpperCase());
    String mathStr = "mathRandom=" + Math.random();
    if (method.equals(Domain.REQUEST_GET)) {url += (url.indexOf("?") != -1 ? "&" : "?") + mathStr;}
    HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
    if (!isBlank(sessionInfo)) {conn.setRequestProperty("Cookie", sessionInfo);}
    conn.setRequestMethod(method);
    if (requestTime > 0) {
    conn.setConnectTimeout(requestTime);
    conn.setReadTimeout(requestTime);
    }
    if (method.equals(Domain.REQUEST_POST)) {
    conn.setDoOutput(true);
    OutputStream output = conn.getOutputStream(); 
    output.write((isBlank(params) ? mathStr : params + "&" + mathStr).getBytes(encoding)); 
    output.flush();output.close();
    }
    String response = FileUtil.get().readFile(conn.getInputStream(), encoding, 1, isLine);
    conn.disconnect();
    requestTime = Domain.LONG_REQUEST_TIME;
    return response;
    }