zoukankan      html  css  js  c++  java
  • 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>




  • 相关阅读:
    Java的char是16位的unicode类型
    Leetcode_907. 子数组的最小值之和(单调栈)
    Leetcode_34. 在排序数组中查找元素的第一个和最后一个位置
    Leetcode_739. 每日温度(单调栈)
    Leetcode_1048. 最长字符串链
    Leetcode_877. 石子游戏(区间dp)
    Leetcode_面试题 17.24. 最大子矩阵
    Leetcode_面试题 17.08. 马戏团人塔(二维LIS)
    C#委托和事件的简单实例
    WPS快速下拉填充公式
  • 原文地址:https://www.cnblogs.com/xiaowangba/p/6314425.html
Copyright © 2011-2022 走看看