24小时热门版块排行榜    

查看: 4267  |  回复: 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 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 s3fFTmArrBt6 2026-09-14 3/150 2026-09-14 18:01 by Aj1rhIDL5ixY
[考博] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 s3fFTmArrBt6 2026-09-14 4/200 2026-09-14 17:49 by Aj1rhIDL5ixY
[考博] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +4 s3fFTmArrBt6 2026-09-14 4/200 2026-09-14 17:25 by Aj1rhIDL5ixY
[公派出国] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +3 LwdutQ8HoqWP 2026-09-13 5/250 2026-09-14 16:25 by Aj1rhIDL5ixY
[教师之家] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +3 LwdutQ8HoqWP 2026-09-13 4/200 2026-09-14 16:13 by Aj1rhIDL5ixY
[找工作] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +4 3n8v2C8RimXI 2026-09-13 4/200 2026-09-14 16:00 by Aj1rhIDL5ixY
[博后之家] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +4 3n8v2C8RimXI 2026-09-13 4/200 2026-09-14 15:57 by Aj1rhIDL5ixY
[教师之家] 售SCI一区T0P文章,我:8.O.55.1.O.54,科目齐全,可+急 +4 3n8v2C8RimXI 2026-09-13 4/200 2026-09-14 15:48 by Aj1rhIDL5ixY
[考研] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 3n8v2C8RimXI 2026-09-13 4/200 2026-09-14 15:45 by Aj1rhIDL5ixY
[考博] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +4 3n8v2C8RimXI 2026-09-13 4/200 2026-09-14 15:37 by Aj1rhIDL5ixY
[硕博家园] 售SCI一区T0P文章,我:8.O.55.1.O54,科目全,可伽急 +3 QUjhNVAcOSff 2026-09-13 3/150 2026-09-14 15:09 by Aj1rhIDL5ixY
[博后之家] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +3 QUjhNVAcOSff 2026-09-13 4/200 2026-09-14 15:01 by Aj1rhIDL5ixY
[公派出国] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +4 QUjhNVAcOSff 2026-09-13 6/300 2026-09-14 15:00 by Aj1rhIDL5ixY
[考研] 售SCI一区文章,我:8.O.551.O.5.4,科目全,可伽急 +3 QUjhNVAcOSff 2026-09-13 3/150 2026-09-14 14:57 by Aj1rhIDL5ixY
[基金申请] 求教各位大神:2026教育部人文社科青年基金项目何时公示呀? +5 云宴山人 2026-09-10 7/350 2026-09-14 12:11 by ecnu2013
[找工作] 浙江师范大学是怎么坑我的 +6 recruit123 2026-09-09 6/300 2026-09-14 12:00 by 萨仁其其格
[博后之家] 售SCI一区T0P文章,我:8.O.55.1.O.5.4,科目全,可+急 +3 QUjhNVAcOSff 2026-09-13 3/150 2026-09-14 10:11 by 23jxep3nCNZb
[考博] 27秋季材料博士申请求助 10+3 Cahal1 2026-09-11 4/200 2026-09-14 08:50 by 北京莱茵编辑
[教师之家] 浙江师范大学是怎么坑我的 +10 recruit123 2026-09-09 15/750 2026-09-13 10:45 by seaskyy
[基金申请] 面上有专家说实验设备不是我们单位的 +6 zj_muchong 2026-09-08 8/400 2026-09-11 06:56 by suifengdao
信息提示
请填处理意见