| 查看: 4259 | 回复: 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就连接不上 |
» 猜你喜欢
什么时候开奖?
已经有4人回复
咱们一起用铁证分析2026国家社科基金中标与否
已经有27人回复
为什么余额宝的年化利率越来越低?主要原因有哪些?
已经有3人回复
2027广东省杰青
已经有9人回复
朋友圈看到的
已经有5人回复
欢迎发来filecode的Mz6后的代码验证其规律
已经有110人回复
明天放榜?
已经有5人回复
时间戳变了,能看出什么问题?
已经有11人回复
重要消息,中午系统在维护
已经有10人回复
投稿咨询
已经有6人回复
找到一些相关的精华帖子,希望有用哦~
英文版原版经典教材 无线mesh网络架构与协议
已经有43人回复
招聘“信息检索、舆情分析、网络数据理解与挖掘”工作人员(中科院自动化所)
已经有4人回复
python urllib2 求助阿
已经有7人回复
可以用GA或者PSO优化算法把RBF神经网络的中心值,宽度和权值一起训练吗??
已经有8人回复
科研从小木虫开始,人人为我,我为人人










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