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
原创粉丝点击