Android中进行HTTP操作
16lz
2021-12-04
要使自己的Android程序进行http操作,就必须现在AndroidManifest.xml定义访问Internet的权限:
下面是布局文件,layout/main.xml
下面是Activity的代码:
<?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上调用google map api v2
- 【转】Launcher研究之AndroidManifest.xml分析
- android之app widget(一)
- [置顶] Android4.2.2自增物理按键(frameworks)
- Android(安卓)Timer 更好方法
- Android中的AnimationDrawable的使用
- Android中attrs.xml文件的使用详解
- android intent 传递各种结构数据
- Android(安卓)App 权限一点知识