24小时热门版块排行榜    

查看: 4219  |  回复: 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 的主题更新
信息提示
请填处理意见