android http 连接通信

一共有三个文件,JAVA,MAIN.XML,AndroidManifest.xml
JAVA文件:
package Android_https.com;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 Android_httpsActivity extends Activity {
   
    privateButton sendbutton = null;   
    privateHttpResponse httpResponse = null;
    privateHttpEntity httpEntity = null;
    
   @Override
    public voidonCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       
       sendbutton = (Button)findViewById(R.id.sendbutton);
       sendbutton.setOnClickListener(new OnClickListener() {
         
         @Override
         public void onClick(View v) {
            HttpGet httpget = newHttpGet("http://localhost/");
            HttpClient httpClient = newDefaultHttpClient();
            InputStream inputStream = null;
            try {
               httpResponse = httpClient.execute(httpget);
               httpEntity = httpResponse.getEntity();
               inputStream = httpEntity.getContent();
               BufferedReader reader = new BufferedReader(newInputStreamReader(inputStream));               
               String result = "" ;
               String line = "";
               while ((line = reader.readLine()) != null){
                  result = result + line;
               }
               System.out.println(result);
            } catch (Exception e) {
               // TODO Auto-generated catch block
               System.out.println("有错误!!!");
               e.printStackTrace();
            }
            
         }
      });
       
    }
}





<?xml version="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello" />

   
   <Button
       android:id="@+id/sendbutton"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="简单发送请求,并回得报文"
       
       />

</LinearLayout>



<?xml version="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
   package="Android_https.com"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk android:minSdkVersion="3"/>
   <uses-permissionandroid:name="android.permission.INTERNET"/>
   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name" >
       <activity
           android:label="@string/app_name"
           android:name=".Android_httpsActivity" >
           <intent-filter >
               <action android:name="android.intent.action.MAIN"/>

               <categoryandroid:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
       </activity>
   </application>

</manifest>




上一篇:RDS的磁盘空间说明


下一篇:Linux下目录快速切换小工具bd