| 查看: 4220 | 回复: 0 | |||
[求助]
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语言: android:layout_width="match_parent" android:layout_height="match_parent" android rientation="vertical" >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就连接不上 |
» 猜你喜欢
拟解决的关键科学问题还要不要写
已经有8人回复
26申博
已经有3人回复
存款400万可以在学校里躺平吗
已经有22人回复
最失望的一年
已经有4人回复
国自然申请面上模板最新2026版出了吗?
已经有19人回复
请教限项目规定
已经有3人回复
基金委咋了?2026年的指南还没有出来?
已经有10人回复
基金申报
已经有6人回复
推荐一本书
已经有13人回复
疑惑?
已经有5人回复
找到一些相关的精华帖子,希望有用哦~
英文版原版经典教材 无线mesh网络架构与协议
已经有43人回复
招聘“信息检索、舆情分析、网络数据理解与挖掘”工作人员(中科院自动化所)
已经有4人回复
python urllib2 求助阿
已经有7人回复
可以用GA或者PSO优化算法把RBF神经网络的中心值,宽度和权值一起训练吗??
已经有8人回复
科研从小木虫开始,人人为我,我为人人













;
rientation="vertical" >
回复此楼
点击这里搜索更多相关资源