1. 发送请求到服务器有几种方式

(1)HttpURLConnection

(2)Httpclient 同步框架

(3)AsyncHttpClient 异步框架 (https://github.com/loopj/android-async-http 下载源码和lib包)

示例代码(3)示例 (最简便实用)

public void asyncGet(View v){
final String username = et_username.getText().toString().trim();
final String password = et_password.getText().toString().trim();
String uri = "http://192.168.1.100:8080/TestLogin/servlet/Login?"
+ "username=" + URLEncoder.encode(username)
+ "&password=" + URLEncoder.encode(password); AsyncHttpClient client = new AsyncHttpClient();
client.get(uri, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
} @Override
public void onFailure(Throwable error, String content) {
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
} });
} public void asyncPost(View v){
final String username = et_username.getText().toString().trim();
final String password = et_password.getText().toString().trim();
String uri = "http://192.168.1.100:8080/TestLogin/servlet/Login"; AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("username", username);
params.put("password", password); client.post(uri, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
Toast.makeText(MainActivity.this, response, Toast.LENGTH_SHORT).show();
} @Override
public void onFailure(Throwable error, String content) {
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
} });
}

示例代码 (1)(2)示例

public class MainActivity extends Activity {
private EditText et_password;
private EditText et_username; public void loginByGet(View paramView) {
String str1 = this.et_password.getText().toString().trim();
String str2 = this.et_username.getText().toString().trim();
if ((TextUtils.isEmpty(str1)) || (TextUtils.isEmpty(str2))) {
Toast.makeText(this, "用户名密码不能为空", 1).show();
return;
}
try {
String str3 = String
.valueOf("http://192.168.1.100:8080/web/LoginServlet?");
StringBuilder localStringBuilder1 = new StringBuilder(str3)
.append("name=");
String str4 = URLEncoder.encode(str2, "UTF-8");
StringBuilder localStringBuilder2 = localStringBuilder1
.append(str4).append("&password=");
String str5 = URLEncoder.encode(str1, "UTF-8");
String str6 = str5;
HttpURLConnection localHttpURLConnection = (HttpURLConnection) new URL(
str6).openConnection();
localHttpURLConnection.setRequestMethod("GET");
localHttpURLConnection.setConnectTimeout(5000);
localHttpURLConnection
.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727)");
if (localHttpURLConnection.getResponseCode() == 200) {
String str7 = StreamTools.readFromStream(localHttpURLConnection
.getInputStream());
Toast.makeText(this, str7, 1).show();
return;
}
} catch (Exception localException) {
localException.printStackTrace();
Toast.makeText(this, "登陆失败", 1).show();
return;
}
Toast.makeText(this, "登陆失败,http响应吗不正确.", 1).show();
} public void loginByPost(View paramView) {
String str1 = this.et_password.getText().toString().trim();
String str2 = this.et_username.getText().toString().trim();
if ((TextUtils.isEmpty(str1)) || (TextUtils.isEmpty(str2))) {
Toast.makeText(this, "用户名密码不能为空", 1).show();
return;
}
try {
HttpURLConnection localHttpURLConnection = (HttpURLConnection) new URL(
"http://192.168.1.100:8080/web/LoginServlet")
.openConnection();
localHttpURLConnection.setRequestMethod("POST");
localHttpURLConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
StringBuilder localStringBuilder1 = new StringBuilder("name=");
String str3 = URLEncoder.encode(str2, "UTF-8");
StringBuilder localStringBuilder2 = localStringBuilder1
.append(str3).append("&password=");
String str4 = URLEncoder.encode(str1, "UTF-8");
String str5 = str4;
String str6 = String.valueOf(str5.length());
String str7 = str6;
localHttpURLConnection.setRequestProperty("Content-Length", str7);
//允许对外写数据
localHttpURLConnection.setDoOutput(true);
OutputStream localOutputStream = localHttpURLConnection
.getOutputStream();
byte[] arrayOfByte = str5.getBytes();
localOutputStream.write(arrayOfByte);
localOutputStream.flush();
if (localHttpURLConnection.getResponseCode() == 200) {
String str8 = StreamTools.readFromStream(localHttpURLConnection
.getInputStream());
Toast.makeText(this, str8, 1).show();
localOutputStream.close();
return;
}
} catch (Exception localException) {
Toast.makeText(this, "post登陆失败", 1).show();
return;
}
Toast.makeText(this, "post登陆失败,http响应吗不正确.", 1).show();
} protected void onCreate(Bundle paramBundle) {
super.onCreate(paramBundle);
setContentView(R.layout.activity_main);
EditText localEditText1 = (EditText) findViewById(R.id.et_username);
this.et_username = localEditText1;
EditText localEditText2 = (EditText) findViewById(R.id.et_password);
this.et_password = localEditText2;
} /**
* 采用httpclient的方式 用get提交数据到服务器
*/ public void loginByClientGet(View view) {
String password = et_password.getText().toString().trim();
String name = et_username.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名密码不能为空", 1).show();
return;
}
// 1.打开浏览器
HttpClient client = new DefaultHttpClient();
// 2.输入浏览器的地址
String uri = "http://192.168.1.100:8080/web/LoginServlet?" + "name="
+ URLEncoder.encode(name) + "&password="
+ URLEncoder.encode(password);
HttpGet httpGet = new HttpGet(uri);
// 3.敲回车.
try {
HttpResponse response = client.execute(httpGet);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream is = response.getEntity().getContent();
String result = StreamTools.readFromStream(is);
Toast.makeText(this, result, 1).show();
} else {
Toast.makeText(this, "服务器异常", 1).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "访问网络异常", 1).show();
} } /**
* 采用httpclient post数据到服务器
*/
public void loginByClientPost(View view) {
String password = et_password.getText().toString().trim();
String name = et_username.getText().toString().trim();
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(password)) {
Toast.makeText(this, "用户名密码不能为空", 1).show();
return;
}
try {
// 1.创建一个浏览器
HttpClient client = new DefaultHttpClient();
// 2.准备一个连接
HttpPost post = new HttpPost(
"http://192.168.1.100:8080/web/LoginServlet");
// 要向服务器提交的数据实体
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("name", name));
parameters.add(new BasicNameValuePair("password", password));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
"utf-8");
post.setEntity(entity);
// 3.敲回车
HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
InputStream is = response.getEntity().getContent();
String result = StreamTools.readFromStream(is);
Toast.makeText(this, result, 1).show();
} else {
Toast.makeText(this, "服务器异常", 1).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "client post 失败", 1).show();
} }
}

Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交的更多相关文章

  1. Android 采用post方式提交数据到服务器

    接着上篇<Android 采用get方式提交数据到服务器>,本文来实现采用post方式提交数据到服务器 首先对比一下get方式和post方式: 修改布局: <LinearLayout ...

  2. Android 采用get方式提交数据到服务器

    首先搭建模拟web 服务器,新建动态web项目,servlet代码如下: package com.wuyudong.web; import java.io.IOException; import ja ...

  3. Android提交数据到服务器的两种方式四种方法

    本帖最后由 yanghe123 于 2012-6-7 09:58 编辑 Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方 ...

  4. Android 采用HttpClient提交数据到服务器

    在前几篇文章中<Android 采用get方式提交数据到服务器><Android 采用post方式提交数据到服务器>介绍了android的两种提交数据到服务器的方法 本文继续介 ...

  5. Android(java)学习笔记210:采用post请求提交数据到服务器(qq登录案例)

    1.POST请求:  数据是以流的方式写给服务器 优点:(1)比较安全 (2)长度不限制 缺点:编写代码比较麻烦   2.我们首先在电脑模拟下POST请求访问服务器的场景: 我们修改之前编写的logi ...

  6. Android(java)学习笔记153:采用post请求提交数据到服务器(qq登录案例)

    1.POST请求:  数据是以流的方式写给服务器 优点:(1)比较安全 (2)长度不限制 缺点:编写代码比较麻烦   2.我们首先在电脑模拟下POST请求访问服务器的场景: 我们修改之前编写的logi ...

  7. Android(java)学习笔记209:采用get请求提交数据到服务器(qq登录案例)

    1.GET请求:    组拼url的路径,把提交的数据拼装url的后面,提交给服务器. 缺点:(1)安全性(Android下提交数据组拼隐藏在代码中,不存在安全问题)  (2)长度有限不能超过4K(h ...

  8. Android(java)学习笔记152:采用get请求提交数据到服务器(qq登录案例)

    1.GET请求:    组拼url的路径,把提交的数据拼装url的后面,提交给服务器. 缺点:(1)安全性(Android下提交数据组拼隐藏在代码中,不存在安全问题)  (2)长度有限不能超过4K(h ...

  9. 采用httpclient提交数据到服务器

    1)Get提交数据 效果演示:

随机推荐

  1. 多进程使用matplotlib.pyplot绘heatmap(多线程不可以)

    数据格式如下: 8_15/l_eye/2732.png -20.5773 -5.17769 -3.34583 21.5859 9_13_1/l_eye/1211.png -10.1145 34.992 ...

  2. Mysql中字段类型之时间戳大坑

         一 .环境说明: 在目前项目中,有这样的一张表,用来记录会议的相关信息,例如:会议的内容.会议的参会人员.会议的地点.会议的状态(会议是否已结束.会议是否被撤销).会议的开始时间以及该条信息 ...

  3. mxGraph画图区域使用鼠标滚轮实现放大/缩小

    // 重写鼠标滚轮事件 mxEvent.addMouseWheelListener = function (funct) { } // 添加初次载入事件 window.onload = functio ...

  4. 使用paramiko的SFTP get或put整个目录

    在<使用paramiko执行远程linux主机命令>中举例说明了执行远程linux主机命令的方法,其实paramiko还支持SFTP传输文件. 由于get或put方法每次只能传输一个文件, ...

  5. 脱离JVM? Hadoop生态圈的挣扎与演化

    本文由知乎<大数据应用与实践>专栏 李呈祥授权发布,版权所有归作者,转载请联系作者! 新世纪以来,互联网及个人终端的普及,传统行业的信息化及物联网的发展等产业变化产生了大量的数据,远远超出 ...

  6. 理解tomcat之搭建简易http服务器

    做过java web的同学都对tomcat非常熟悉.我们在使用tomcat带来的便利的同时,是否想过tomcat是如何工作的呢?tomcat本质是一个http服务器,本篇文章将搭建一个简单的http服 ...

  7. Git的安装和设置

    1.客户端下载 首先可以在https://git-scm.com/downloads下载客户端,进行安装. 2.安装 安装比较简单,可以直接默认一步一步往下安装即可: 3.配置github的ssh秘钥 ...

  8. 测试:safenet提供的CheckKey函数 内存泄漏。具体来说是句柄.

    unsigned char vendor_code[] = "7XSQT4jxlSkDJhwqpxxfLwbuxgrYw93OMy+K5sc5pyfTa7HQo1ikLyg7FDuEpgUK ...

  9. jenkins SSH登录 Git配置(通过eclipse生成SSH 密钥)

    1.通过eclipse生成SSH 密钥 菜单栏的windows-->preferences-->General-->Network Connections-->SSH2--&g ...

  10. 解决 ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务

    速度太慢点击了一下优化,出现了 ORA-12514,搜索了解决方法记录一下,第一个基本服务就解决了. 转自:http://apps.hi.baidu.com/share/detail/19115407 ...