HTTP操作 获取网页源码
来源:互联网 发布:淘宝有什么好吃的水果 编辑:程序博客网 时间:2024/06/05 10:17
package com.example.internet;import java.io.*;import org.apache.http.*;import org.apache.http.client.*;import org.apache.http.client.methods.*;import org.apache.http.impl.client.*;import org.apache.http.util.EntityUtils;import android.os.Bundle;import android.app.Activity;import android.view.Menu;import android.view.View;import android.widget.*;import android.os.*;public class inetrnet extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_inetrnet); StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() .detectDiskWrites() .detectDiskReads() .detectNetwork() .penaltyLog() .build()); StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .penaltyLog() .penaltyDeath() .build()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_inetrnet, menu); return true; } public String HTTPGET(String url) { String result = ""; try{ HttpGet httpget=new HttpGet(url); HttpClient httpclient = new DefaultHttpClient(); HttpResponse hResponse; hResponse = httpclient.execute(httpget); if(hResponse.getStatusLine().getStatusCode()==200) { result = EntityUtils.toString(hResponse.getEntity()); } }catch (ClientProtocolException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } return(result); } public void get(View v) { EditText editText1 =(EditText)findViewById(R.id.editText1); EditText editText2 =(EditText)findViewById(R.id.editText2); String url = editText1.getText().toString(); editText2.setText(HTTPGET(url)); }}
<uses-permission android:name="android.permission.INTERNET"/>权限在AndroidManifest.xml里加
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="URL:" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/textView1" android:ems="10" android:inputType="textNoSuggestions" android:text="http://baidu.com" > </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/editText1" android:layout_below="@+id/editText1" android:onClick="get" android:text="GET" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/button1" android:ems="10" android:inputType="textMultiLine" > <requestFocus /> </EditText></RelativeLayout>
0 0
- HTTP操作 获取网页源码
- j2me http 获取网页源码
- 发送Http请求获取网页源码
- c# 下载网页源码 获取http状态码
- 获取网页图片链接(http:/)
- xmlhttp获取网页源码
- js获取网页源码
- 高效获取网页源码
- CHttpFile获取网页源码
- c# 获取网页源码
- 高效获取网页源码
- C#获取网页源码
- C#获取网页源码
- c# 获取网页源码
- js 获取 网页源码
- c# 获取网页源码
- java 获取网页源码
- java获取网页源码
- Linux中修改文件所属用户组
- poj 3624 (动态规划)
- Python在信号与系统中的应用(1)——Hilbert变换,Hilbert在单边带包络检波的应用,FIR_LPF滤波器设计,还有逼格高高的FM(PM)调制
- db2 查询表、模式、用户和权限
- Linux维护-日常基本维护操作1
- HTTP操作 获取网页源码
- VS2003在vista/win7下搜索会出现僵死
- he Struts dispatcher cannot be found(struts标签要经过拦截器)
- 用Java进行GUI设计,出现对必需的库 rt.jar 具有一定限制,因此无法访问类型 JButton等问题
- Java 一些书写格式
- 手把手教你构建自己的Linux系统 —— 软件包下载地址
- unity2D 忽略物体之间的碰撞
- Spiral Matrix II
- TortoiseSVN使用