android get或post及HttpClient与服务器数据交互
1、Service
package mydemo.mycom.demo2.service; import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair; import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List; import mydemo.mycom.demo2.utils.StreamTools; /**
* Created by Administrator on 2015/5/20.
*/
public class NetService { public static String loginByGet(String username, String password) {
try {
String path = "http://192.168.1.110:1010/UserInfo/Login?username=" + username + "&password=" + password;
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code == 200) {
//请求成功
InputStream is = conn.getInputStream();
String result = StreamTools.readInputStream(is);
return result;
} else {
//请求失败
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static String loginByPost(String username, String password) {
try {
String path = "http://192.168.1.110:1010/UserInfo/Login";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
String data = "username=" + username + "&password=" + password; conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length() + ""); //post 的方式 实际上是浏览器把数据 写给了服务器
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
int code = conn.getResponseCode();
if (code == 200) {
//请求成功
InputStream is = conn.getInputStream();
String result = StreamTools.readInputStream(is);
return result;
} else {
//请求失败
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static String loginByClientGet(String username, String password) {
try {
HttpClient client = new DefaultHttpClient();
String path = "http://192.168.1.110:1010/UserInfo/Login?username=" + username + "&password=" + password;
HttpGet httpget = new HttpGet(path);
HttpResponse response = client.execute(httpget);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
//请求成功
InputStream is = response.getEntity().getContent();
String result = StreamTools.readInputStream(is);
return result;
} else {
//请求失败
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static String loginByClientPost(String username, String password) {
try {
HttpClient client = new DefaultHttpClient();
String path = "http://192.168.1.110:1010/UserInfo/Login";
HttpPost httppost = new HttpPost(path);
//指定要提交的数据实体
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("username", username));
list.add(new BasicNameValuePair("password", password));
httppost.setEntity(new UrlEncodedFormEntity(list, "UTF_8"));
HttpResponse response = client.execute(httppost);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
//请求成功
InputStream is = response.getEntity().getContent();
String result = StreamTools.readInputStream(is);
return result;
} else {
//请求失败
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} }
2.StreamTools
package mydemo.mycom.demo2.utils; import java.io.ByteArrayOutputStream;
import java.io.InputStream; public class StreamTools {
public static String readInputStream(InputStream is)
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len =0;
byte[] buffer = new byte[1024];
while((len=is.read(buffer))!=-1)
{
baos.write(buffer,0,len);
}
is.close();
baos.close(); byte[] result = baos.toByteArray();
String temp = new String(result);
return temp;
}catch (Exception e)
{
e.printStackTrace();
return null;
} } }
android get或post及HttpClient与服务器数据交互的更多相关文章
- UDP协议实现客户服务器数据交互
UDP协议实现客户服务器数据交互 按照往常一样将今天自己写的题目答案写在了博客上习题:客户端循环发送消息给服务端,服务端循环接收,并打印出来,直到收到Bye就退出程序. package network ...
- (转载)Android项目实战(二十七):数据交互(信息编辑)填写总结
Android项目实战(二十七):数据交互(信息编辑)填写总结 前言: 项目中必定用到的数据填写需求.比如修改用户名的文字编辑对话框,修改生日的日期选择对话框等等.现总结一下,方便以后使用. 注: ...
- TCP移动端跟服务器数据交互
同一台笔记本下的客户端和服务端 TCPClient 客户端: // RootViewController.h#import <UIKit/UIKit.h>#import "As ...
- Appcn 移动开发 前台与服务器数据交互
第一次写.嘿嘿. 言归正传,这几天开始学习移动开发,使用的是Appcan平台.Appcan平台采用HTML5+CSS3做开发 实现跨平台,正好可以满足我们的业务需求. Appacn和数据库进行交互的方 ...
- Android进阶之Fragment与Activity之间的数据交互
1 为什么 因为Fragment和Activity一样是具有生命周期,不是一般的bean通过构造函数传值,会造成异常. 2 Activity把值传递给Fragment 2.1 第一种方式,也是最常用的 ...
- unity用json和服务器数据交互
第一种类型:服务器json数据是个对象 /// <summary> /// 获取用户信息初始化信息 /// </summary> void InitUserMessage() ...
- android通过HttpClient与服务器JSON交互
通过昨天对HttpClient的学习,今天封装了HttpClient类 代码如下: package com.tp.soft.util; import java.io.BufferedReader; i ...
- Android项目实战(二十七):数据交互(信息编辑)填写总结
前言: 项目中必定用到的数据填写需求.比如修改用户名的文字编辑对话框,修改生日的日期选择对话框等等.现总结一下,方便以后使用. 注: 先写实现过程,想要学习的同学可以看看,不需要的同学可以直接拉到最下 ...
- mui.ajax()和asp.net sql服务器数据交互【2】json数组和封装
今天没有做循环创建显示:可以参考张鑫旭的文章:<基于HTML模板和JSON数据的JavaScript交互> 1.ashx页面代码 //下面的封装一般框架底层都是写好的:连接 数据库和获取D ...
随机推荐
- @classmethod 与 @staticmethod 区别
- mybatis 缓存(cache)的使用
许多应用程序,为了提高性能而增加缓存, 特别是从数据库中获取的数据. 在默认情况下,mybatis 的一级缓存是默认开启的.类似于hibernate, 所谓一级缓存,也就是基于同一个sqlsessio ...
- BZOJ1251序列终结者——非旋转treap
题目描述 网上有许多题,就是给定一个序列,要你支持几种操作:A.B.C.D.一看另一道题,又是一个序列 要支持几种操作:D.C.B.A.尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技 ...
- BZOJ3129 SDOI2013方程(容斥原理+扩展lucas)
没有限制的话算一个组合数就好了.对于不小于某个数的限制可以直接减掉,而不大于某个数的限制很容易想到容斥,枚举哪些超过限制即可. 一般情况下n.m.p都是1e9级别的组合数没办法算.不过可以发现模数已经 ...
- 从TensorFlow到PyTorch:九大深度学习框架哪款最适合你?
开源的深度学习神经网络正步入成熟,而现在有许多框架具备为个性化方案提供先进的机器学习和人工智能的能力.那么如何决定哪个开源框架最适合你呢?本文试图通过对比深度学习各大框架的优缺点,从而为各位读者提供一 ...
- 教你如何开启/关闭ubuntu防火墙
目录 [隐藏] 1 安装方法 2 使用方法 3 推荐设置 4 详细使用说明 安装方法 sudo apt-get install ufw 当然,这是有图形界面的(比较简陋),在新立得里搜索gufw试 ...
- poj1038 Bugs Integrated,Inc. (状压dp)
题意:N*M的矩阵,矩阵中有一些坏格子,要在好格子里铺2*3或3*2的地砖,问最多能铺多少个. 我的方法好像和网上流传的方法不太一样...不管了.... 由数据范围很容易想到状压dp 我们设某个状态的 ...
- Nginx入门篇
Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 ...
- Coablt strike官方教程中文译版本
安装和设置 系统要求 Cobalt Strike的最低系统要求 2 GHz +以上的cpu 2 GB RAM 500MB +可用空间 在Amazon的EC2上,至少使用较高核数的CPU(c1.medi ...
- codeblocks 支持多个exe同时执行
如果看总时间,没什么用,因为总资源是一样的. 但是可以做到:吃饭前,执行多个程序,吃完饭,所有程序执行完.