xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zzw.request.MainActivity" > <EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" /> <EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/username"
android:hint="输入密码" /> <Button
android:id="@+id/get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/password"
android:onClick="getClient"
android:text="get请求" /> <Button
android:id="@+id/post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/get"
android:onClick="postClient"
android:text="post请求" /> </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 }

通过HttpClient方式连接网络的更多相关文章

  1. vmvare使用桥接和NAT方式连接网络

    一.背景:本着学以致用的心态,试着最小化安装Centos7.4.安装centos主要目的有两个:共享文件(samba).安装postgresql数据库 本打算使用内网(不联网)的方式安装samba和p ...

  2. Hyper-v 虚拟机使用NAT方式连接网络

    最近因为搞docker的原因,卸掉了vb和vm,用上了hyper. 其实一直挺喜欢hyper-v的,但是一直都受虚拟网络的影响--hyper-v创建不了vm和vb使用的那种NAT连接方式. 默认使用外 ...

  3. 通过URLHttpConnection方式连接网络步骤,获取位图为例

    要注意的是:访问网络不能直接放在主线程,要放在另外一个线程里面,如果放在主线程会报android.os.NetworkOnMainThreadException错误1 public Bitmap ge ...

  4. 转:在虚拟机中用NAT方式连接网络

    1.安装VMware Workstation .在安装过VMware Workstation软件后,会在本地连接中,多了两个虚拟网卡,一个是 VMware Network Adapter for VM ...

  5. VM12中CentOS7以NAT方式连接网络的方法

    解决问题:centos网络连不上,连不上主机,ifconfig等命令不能用(配完有网了,安装上就好了)等问题 前提:安装vm12,centos7(最小安装)  注意:以下以192开头的,你都要替换成自 ...

  6. Windows XPSP3通过网络级身份验证方式连接Windows Server 2008远程桌面

    远程桌面大大方便了大家的日常管理工作,Windows Server 2008同样秉承这一优秀特性,并引入网络级身份验证(NLA)作为远程桌面连接的默认身份验证方式. 网络级身份验证 (NLA) 是一种 ...

  7. 【Android-NetWork】 判断是否连接网络,判断网络连接方式

    如何判断Android是否连接网络? Java代码: ConnectivityManager conn = (ConnectivityManager) getSystemService(Activit ...

  8. android post 方式 访问网络 实例

    android post 方式 访问网络 实例 因为Android4.0之后对使用网络有特殊要求,已经无法再在主线程中访问网络了,必须使用多线程访问的模式 该实例需要在android配置文件中添加 网 ...

  9. httpUrlConnection连接网络的用法(用到了handle传递消息,在主线程中更新UI)

    由于httpclient在Android5.0以后已经过时,所以官方推荐使用httpUrlConnection来连接网络,现将该连接的基本方法展示,如下 注意:记得加入<uses-permiss ...

随机推荐

  1. Hibernate注解:一对多外键关联

    情形:两个表,cms_mode是主表,cms_model_field是子表,cms_model_field的model_id字段关联到cms_model的主键. # # Source for tabl ...

  2. oracle 数据恢复,只有oradata文件夹里的文件,没有备份文件的数据库恢复,重装系统后,oracle 10g数据库恢复

    格式化重装系统后,才想起来oracle 10g 数据库没有做备份,开始以为很麻烦,没想到数据库恢复的还挺顺利的 恢复方法: 1,把原来的数据库文件备份,(D:\oracle\product\10.2. ...

  3. (easy)LeetCode 203.Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  4. java的重载

    1.java的重载三种:参数类型不同,构成重载,参数类型不同:::::参数顺序不同构成重载::::::参数个数不同,构成重载

  5. dedecms不安全啊

    两个站都早被黑了,没心弄了.该注意的都注意了,除了没定期升级.不靠谱啊.开源软件的安全性是个大问题.

  6. .atomic vs volatile

    结论: atomic比volatile靠谱 java.util.concurrent.atomic.Atomic*原子类和volatile关键字是java中两种常见的处理多线程下数据共享读写的机制.二 ...

  7. 论java虚拟类和接口的区别

    如题:Abstract使数据成员虚拟化,而Interface则使方法成员虚拟化.

  8. VS2010 MSDN配置

    安装VS2010之后总是要装MSDN的,不然写起程序来还真不方便.前段时间换了电脑后,折腾了好久才把VS和MSDN装好,所以为了方便自己和别人特地把配置MSDN的详细步骤写出来: 1.         ...

  9. TortoiseSVN期望文件系统格式在“1”到“6”之间;发现格式“7”

    安装好Subversion和TortoiseSVN之后.检出和浏览版本库的时候一直报错 "期望文件系统格式在"1"到"6"之间;发现格式"7 ...

  10. 百度校招面试经历及总结(已发offer)

    听说发面经可以攒rp,希望早点给我确定的offer通知,也希望看到这个面经的小伙伴能顺利拿到心仪的offer~ 职位:机器学习-数据挖掘工程师 9.15 上午11点 一面 1.介绍项目 2.考研意向, ...