24小时热门版块排行榜    

查看: 4231  |  回复: 0

suzikid

新虫 (初入文坛)

[求助] httpget设置网络超时时间

package com.example.httpgetpost;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;


import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HttpGetPost extends Activity {
    private Button httpGetButton;
    private URL url=null;

   
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
                try {
                        url = new URL("http://www.baidu.com/";
                } catch (MalformedURLException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                }  
        httpGetButton = (Button)findViewById(R.id.httpget);
        //httpPostButton = (Button)findViewById(R.id.httppost);
        httpGetButton.setOnClickListener(new OnClickListener(){
                public void onClick(View arg0) {
                                HttpGet httpget = new HttpGet( url.toString() );  
                                InputStream in=null;
                        HttpClient client = new DefaultHttpClient();  
                        HttpParams params = client.getParams();
                        HttpConnectionParams.setConnectionTimeout(params, 30000);
                                  
                            try {
                                        HttpResponse response = client.execute( httpget );
                                        HttpEntity httpentity = response.getEntity();
                                        in=httpentity.getContent();
                                        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                                        String line="";
                                        String result = "";
                                        while((line=reader.readLine())!=null)
                                        {
                                                result = result+line;
                                        }
                                        System.out.println("===========";
                                        System.out.println(result);
                                } catch (ClientProtocolException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                } finally{
                                        if(in!=null)
                                                try {
                                                        in.close();
                                                } catch (IOException e) {
                                                        // TODO Auto-generated catch block
                                                        e.printStackTrace();
                                                }
                                }
                        }
        });
    }
}
XML语言:
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    androidrientation="vertical" >
              android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mTextView"
        android:text="@string/hello_world"
        />
              android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/httpget"
        android:text="@string/httpget"/>
                android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/httppost"
        android:text="@string/httppost"/>





为何在2.3.1上可以运行,但是在4.0.3上却不可以,而且httpget可以用网络url吗?为什么好像只可以用本地url,换成网络url就连接不上
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

相关版块跳转 我要订阅楼主 suzikid 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 271材料工程求调剂 +5 .6lL 2026-03-18 5/250 2026-03-19 03:07 by 无懈可击111
[考研] 085700资源与环境308求调剂 +3 墨墨漠 2026-03-18 3/150 2026-03-18 22:35 by bingxueer79
[考研] 求材料调剂 +8 隔壁陈先生 2026-03-12 8/400 2026-03-18 22:19 by li123456789.
[考研] 330求调剂 +3 小材化本科 2026-03-18 3/150 2026-03-18 21:55 by 无懈可击111
[考研] 274求调剂 +5 S.H1 2026-03-18 5/250 2026-03-18 21:27 by guosr9609
[考研] 328求调剂,英语六级551,有科研经历 +3 生物工程调剂 2026-03-17 7/350 2026-03-18 20:41 by Wangjingyue
[考研] 0703化学调剂,求各位老师收留 +9 秋有木北 2026-03-14 9/450 2026-03-18 19:49 by macy2011
[考研] 【同济软件】软件(085405)考研求调剂 +3 2026eternal 2026-03-18 3/150 2026-03-18 19:09 by 搏击518
[考研] 085600材料与化工 +5 安全上岸! 2026-03-16 5/250 2026-03-18 15:33 by cmz0325
[考研] 0854可跨调剂,一作一项核心论文五项专利,省、国级证书40+数一英一287 +8 小李0854 2026-03-16 8/400 2026-03-18 14:35 by 搏击518
[考研] 环境工程调剂 +8 大可digkids 2026-03-16 8/400 2026-03-18 09:36 by zhukairuo
[硕博家园] 湖北工业大学 生命科学与健康学院-课题组招收2026级食品/生物方向硕士 +3 1喜春8 2026-03-17 5/250 2026-03-17 17:18 by ber川cool子
[考研] 有没有道铁/土木的想调剂南林,给自己招师弟中~ +3 TqlXswl 2026-03-16 7/350 2026-03-17 15:23 by TqlXswl
[考博] 26申博 +4 八旬速览 2026-03-16 4/200 2026-03-17 13:00 by 轻松不少随
[考研] 211本,11408一志愿中科院277分,曾在中科院自动化所实习 +6 Losir 2026-03-12 7/350 2026-03-17 12:09 by danranxie
[考研] 材料080500调剂求收留 +3 一颗meteor 2026-03-13 3/150 2026-03-14 10:54 by peike
[考研] [0860]321分求调剂,ab区皆可 +4 宝贵热 2026-03-13 4/200 2026-03-13 22:01 by 星空星月
[考研] 26调剂/材料科学与工程/总分295/求收留 +9 2026调剂侠 2026-03-12 9/450 2026-03-13 20:46 by 18595523086
[考研] 工科材料085601 279求调剂 +8 困于星晨 2026-03-12 10/500 2026-03-13 15:42 by ms629
[考研] 321求调剂(食品/专硕) +3 xc321 2026-03-12 6/300 2026-03-13 08:45 by xc321
信息提示
请填处理意见