读书人

Android中开展HTTP操作

发布时间: 2012-06-28 15:20:04 作者: rapoo

Android中进行HTTP操作
要使自己的Android程序进行http操作,就必须现在AndroidManifest.xml定义访问Internet的权限:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.ncs.hj"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" /><uses-permission android:name="android.permission.INTERNET"/>    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".HttpTestActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

下面是布局文件,layout/main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_height="wrap_content" android:layout_width="match_parent"><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /><Buttonandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="TEST"android:id="@+id/btn"/></LinearLayout>

下面是Activity的代码:
package com.ncs.hj;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;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 android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class HttpTestActivity extends Activity {    /** Called when the activity is first created. */Button btn =null;HttpResponse httpResponse = null;HttpEntity httpEntity = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                btn = (Button)findViewById(R.id.btn);        btn.setOnClickListener(new HttpTest());    }        class HttpTest implements OnClickListener {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubHttpGet httpGet = new HttpGet("http://www.google.com");HttpClient httpClient = new DefaultHttpClient();InputStream inputStream = null;try {httpResponse = httpClient.execute(httpGet);httpEntity = httpResponse.getEntity();inputStream = httpEntity.getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));String result = "";String line ="";while((line = reader.readLine()) != null) {result = result +line;}System.out.println(result);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}    }}

读书人网 >Android

热点推荐