| 查看: 4229 | 回复: 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就连接不上 |
» 猜你喜欢
体制内长辈说体制内绝大部分一辈子在底层,如同你们一样大部分普通教师忙且收入低
已经有11人回复
过年走亲戚时感受到了所开私家车的鄙视链
已经有9人回复
今年春晚有几个节目很不错,点赞!
已经有10人回复
情人节自我反思:在爱情中有过遗憾吗?
已经有10人回复
基金正文30页指的是报告正文还是整个申请书
已经有5人回复
找到一些相关的精华帖子,希望有用哦~
英文版原版经典教材 无线mesh网络架构与协议
已经有43人回复
招聘“信息检索、舆情分析、网络数据理解与挖掘”工作人员(中科院自动化所)
已经有4人回复
python urllib2 求助阿
已经有7人回复
可以用GA或者PSO优化算法把RBF神经网络的中心值,宽度和权值一起训练吗??
已经有8人回复
科研从小木虫开始,人人为我,我为人人













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