zoukankan      html  css  js  c++  java
  • 通过HttpClient方式连接网络

    xml:

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context="com.zzw.request.MainActivity" >
    10 
    11     <EditText
    12         android:id="@+id/username"
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:hint="请输入用户名" />
    16 
    17     <EditText
    18         android:id="@+id/password"
    19         android:layout_width="match_parent"
    20         android:layout_height="wrap_content"
    21         android:layout_below="@id/username"
    22         android:hint="输入密码" />
    23 
    24     <Button
    25         android:id="@+id/get"
    26         android:layout_width="match_parent"
    27         android:layout_height="wrap_content"
    28         android:layout_below="@id/password"
    29         android:onClick="getClient"
    30         android:text="get请求" />
    31 
    32     <Button
    33         android:id="@+id/post"
    34         android:layout_width="match_parent"
    35         android:layout_height="wrap_content"
    36         android:layout_below="@id/get"
    37         android:onClick="postClient"
    38         android:text="post请求" />
    39 
    40 </RelativeLayout>

     java代码:

      1 package com.zzw.httpClient;
      2 
      3 import java.io.ByteArrayOutputStream;
      4 import java.io.IOException;
      5 import java.io.InputStream;
      6 import java.util.ArrayList;
      7 import java.util.List;
      8 
      9 import org.apache.http.HttpEntity;
     10 import org.apache.http.HttpResponse;
     11 import org.apache.http.NameValuePair;
     12 import org.apache.http.client.ClientProtocolException;
     13 import org.apache.http.client.HttpClient;
     14 import org.apache.http.client.entity.UrlEncodedFormEntity;
     15 import org.apache.http.client.methods.HttpGet;
     16 import org.apache.http.client.methods.HttpPost;
     17 import org.apache.http.impl.client.DefaultHttpClient;
     18 import org.apache.http.impl.conn.DefaultClientConnection;
     19 import org.apache.http.message.BasicNameValuePair;
     20 
     21 import android.app.Activity;
     22 import android.os.Bundle;
     23 import android.view.Menu;
     24 import android.view.MenuItem;
     25 import android.view.View;
     26 import android.widget.EditText;
     27 import android.widget.Toast;
     28 
     29 public class MainActivity extends Activity {
     30     EditText username_et = null;
     31     EditText passWord_et = null;
     32 
     33     @Override
     34     protected void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.activity_main);
     37         username_et = (EditText) findViewById(R.id.username);
     38         passWord_et = (EditText) findViewById(R.id.password);
     39     }
     40 
     41     public void getClient(View view) {
     42         new Thread(new Runnable() {
     43             @Override
     44             public void run() {
     45                 try {
     46                     // 1、获得一个client,相当于浏览器
     47                     HttpClient client = new DefaultHttpClient();
     48                     // 2、定义一个get请求,并且封装参数
     49                     String data = "username=" + username_et.getText().toString() + "&password="
     50                             + passWord_et.getText().toString();
     51                     HttpGet get = new HttpGet("http://10.0.2.2:8080/baidu/LoginServelt?" + data);
     52                     // 3、用定义好的浏览器来请求一个地址
     53                     HttpResponse response = client.execute(get);
     54                     int status = response.getStatusLine().getStatusCode();
     55                     if (status == 200) {
     56                         final String content = getStringFromStream(response.getEntity().getContent());
     57                         runOnUiThread(new Runnable() {
     58                             @Override
     59                             public void run() {
     60                                 Toast.makeText(MainActivity.this, "返回信息:" + content, 0).show();
     61                             }
     62                         });
     63 
     64                     }
     65                 } catch (ClientProtocolException e) {
     66                     // TODO Auto-generated catch block
     67                     e.printStackTrace();
     68                 } catch (IOException e) {
     69                     // TODO Auto-generated catch block
     70                     e.printStackTrace();
     71                 }
     72 
     73             }
     74         }).start();
     75     }
     76 
     77     public void postClient(View view) {
     78         new Thread(new Runnable() {
     79             @Override
     80             public void run() {
     81                 try {
     82                     // 1、获得client
     83                     HttpClient client = new DefaultHttpClient();
     84                     // 2、获得POST请求及设置参数
     85                     HttpPost post = new HttpPost("http://10.0.2.2:8080/baidu/LoginServelt");
     86                     List<NameValuePair> parameters = new ArrayList<NameValuePair>();
     87                     NameValuePair nameuser = new BasicNameValuePair("username", username_et.getText().toString());
     88                     NameValuePair passWord = new BasicNameValuePair("passWord", passWord_et.getText().toString());
     89                     parameters.add(nameuser);
     90                     parameters.add(passWord);
     91 
     92                     HttpEntity entity = new UrlEncodedFormEntity(parameters, "UTF-8");
     93                     post.setEntity(entity);
     94                     // 3、执行post请求
     95                     HttpResponse response = client.execute(post);
     96                     int status = response.getStatusLine().getStatusCode();
     97                     if (status == 200) {
     98                         final String content = getStringFromStream(response.getEntity().getContent());
     99                         runOnUiThread(new Runnable() {
    100                             
    101                             @Override
    102                             public void run() {
    103                                 Toast.makeText(MainActivity.this,"POST:"+content,0).show();
    104                             }
    105                         });
    106                         
    107                     
    108                     }
    109                 } catch (ClientProtocolException e) {
    110                     // TODO Auto-generated catch block
    111                     e.printStackTrace();
    112                 } catch (IOException e) {
    113                     // TODO Auto-generated catch block
    114                     e.printStackTrace();
    115                 }
    116             }
    117         }).start();
    118     }
    119 
    120     public String getStringFromStream(InputStream in) throws IOException {
    121         byte[] buffer = new byte[1024];
    122         ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    123         int leng = 0;
    124         while ((leng = in.read(buffer, 0, 1024)) != -1) {
    125             byteArray.write(buffer);
    126         }
    127         String content = byteArray.toString();
    128         byteArray.close();
    129         return content;
    130     }
    131 }
  • 相关阅读:
    Entity Framework 6 Recipes 2nd Edition(13-5)译 -> 使POCO的修改追踪更高
    Entity Framework 6 Recipes 2nd Edition(13-6)译 -> 自动编译的LINQ查询
    Entity Framework 6 Recipes 2nd Edition(13-4)译 -> 有效地创建一个搜索查询
    Entity Framework 6 Recipes 2nd Edition(13-3)译 -> 为一个只读的访问获取实体
    Entity Framework 6 Recipes 2nd Edition(13-2)译 -> 用实体键获取一个单独的实体
    Entity Framework 6 Recipes 2nd Edition(13-1)译 -> 优化TPT继承模型的查询
    Asp.net MVC5 路由Html后缀的问题
    Entity Framework 6 Recipes 2nd Edition(12-8)译 -> 重新获取一个属性的原始值
    Entity Framework 6 Recipes 2nd Edition(12-7)译 -> 设定默认值
    Entity Framework 6 Recipes 2nd Edition(12-5)译 -> 自动删除相关联实体
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4920040.html
Copyright © 2011-2022 走看看