android HttpClient将数据提交到服务器
1、HttpClient 使用方式
public static String loginByClientGet(String username,String password)
{
try { //打开浏览器
HttpClient client = new DefaultHttpClient(); //输入地址(url)
String url = "http://192.168.1.100:8088/Login.ashx?username="+username+"&password="+password;
HttpGet httpGet = new HttpGet(url); //按回车(发请求http get请求)
HttpResponse response = client.execute(httpGet); //得到相应码
int code = response.getStatusLine().getStatusCode(); if(code==200)
{
//得到相应实体
HttpEntity entity = response.getEntity();
//得到相应内容
InputStream is = entity.getContent();
return StreamUtil.readInputStream(is);
}
else
{
return null;
} } catch (Exception e) {
e.printStackTrace();
return null;
}
} public static String loginByClientPost(String username,String password)
{ try
{
//打开浏览器
HttpClient client = new DefaultHttpClient(); //输入地址(输入url)
String url = "http://192.168.1.100:8088/Login.ashx";
//使用post请求
HttpPost httpPost = new HttpPost(url); //输入指定提交的数据实体
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(params)); //得到相应
HttpResponse response = client.execute(httpPost); //得到响应码
int code = response.getStatusLine().getStatusCode();
if(code==200)
{
//得到相应内容
InputStream is = response.getEntity().getContent();
return StreamUtil.readInputStream(is);
}
else
{
return null;
}
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
}
2、InputStream转为String方法
package com.example.getserverdata.utils; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream; public class StreamUtil { public static String readInputStream(InputStream is)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
try {
while((len = is.read(data))!=-1)
baos.write(data, 0, len);
is.close();
baos.close();
return new String(baos.toByteArray()); } catch (Exception e) { e.printStackTrace();
} return null;
}
}
android HttpClient将数据提交到服务器的更多相关文章
- android 使用get和post将数据提交到服务器
1.activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android& ...
- 利用asynchttpclient开源项目来把数据提交给服务器
可以通过github去查找asynchttpclient,并下载源代码,并加载到自己的工程中. 1.利用get方法提交 2.利用post方法来提交
- Android提交数据到JavaWeb服务器实现登录
之前学习Android提交数据到php服务器没有成功,在看了两三个星期的视频之后,现在终于实现了与服务器的交互.虽然完成的不是PHP端的,但是在这个过程还是学到了不少东西的.现在我先来展示一下我的成果 ...
- Android 采用post方式提交数据到服务器
接着上篇<Android 采用get方式提交数据到服务器>,本文来实现采用post方式提交数据到服务器 首先对比一下get方式和post方式: 修改布局: <LinearLayout ...
- Android发送数据到web服务器4种方式
1./** 2. * Android中向web服务器提交数据的两种方式四种方法 3. */ 4.public class SubmitDataByHttpClientAndOrdinaryWay { ...
- HttpClient将手机上的数据发送到服务器
到官网下载jar包,下载GA发布版本即可 在项目中将httpclient-4.5.5.jar.httpcore-4.4.9.jar.httpmime-4.5.5.jar.commons-logging ...
- RSA Android加密的数据服务器上无法解密?
一.android加密的数据服务器上无法解密? "算法/模式/填充" android的rsa加密方式是--------RSA/ECB/NoPadding或者RSA/None/NoP ...
- Android网络之数据解析----SAX方式解析XML数据
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android中实现java与PHP服务器(基于新浪云免费云平台)http通信详解
Android中实现java与PHP服务器(基于新浪云免费云平台)http通信详解 (本文转自: http://blog.csdn.net/yinhaide/article/details/44756 ...
随机推荐
- __slots__用法以及优化
其实也是无意之中又看到这个东西,这次索性再记一下,免得下次忘记又再看一遍,往复循环浪费了太多时间. __slots__其实我做项目这么久还没有主动使用过.下面reference有提到这么一句话 War ...
- React Native & Web APP
React Native Build native mobile apps using JavaScript and React https://facebook.github.io/react-na ...
- liunx上安装MySQL一个非常简单的方法
1.官网下载yum源 https://www.mysql.com/ 2.把yum源包上传到linux,安装. 执行命令安装 [root@bogon ~]# yum localinstall mysql ...
- Nginx ACCESS阶段 如何限制IP访问
192.168.1.0/24(最大32位的子网掩码) 每个ip是8位 那么 24/8 = 3 也就是前三个二进制 是 11111111 11111111 11111111 是指子网掩码的位数.写的是多 ...
- Windows 下vim的配置文件_vimrc
set nocompatible source $VIMRUNTIME/vimrc_example.vim source $VIMRUNTIME/mswin.vim behave mswin set ...
- windows下安装PyTorch0.4.0
PyTorch框架,据说2018.4.25刚刚上架windows,安个玩玩 我的环境: windows 10 anaconda虚拟环境python3.6 cuda9.1 cudnn7 pytorch ...
- android 面试准备基础题
1. 请描述下Activity的生命周期. 必调用的三个方法:onCreate() --> onStart() --> onResume(),用AAA表示 )父Activity启动子 ...
- SharePoint 2013 event id 8321 错误
SharePoint 2013里会报8321的错误: A certificate validation operation took 15011.1412 milliseconds and has e ...
- docker的网络模式
记性不好,回顾一下.按照惯例,直接看官文. Docker's networking subsystem is pluggable, using drivers. Several drivers exi ...
- Activity的跳转与传值
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://android.blog.51cto.com/268543/323982 Acti ...