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与服务器数据交互的更多相关文章

  1. UDP协议实现客户服务器数据交互

    UDP协议实现客户服务器数据交互 按照往常一样将今天自己写的题目答案写在了博客上习题:客户端循环发送消息给服务端,服务端循环接收,并打印出来,直到收到Bye就退出程序. package network ...

  2. (转载)Android项目实战(二十七):数据交互(信息编辑)填写总结

    Android项目实战(二十七):数据交互(信息编辑)填写总结   前言: 项目中必定用到的数据填写需求.比如修改用户名的文字编辑对话框,修改生日的日期选择对话框等等.现总结一下,方便以后使用. 注: ...

  3. TCP移动端跟服务器数据交互

    同一台笔记本下的客户端和服务端 TCPClient 客户端: //  RootViewController.h#import <UIKit/UIKit.h>#import "As ...

  4. Appcn 移动开发 前台与服务器数据交互

    第一次写.嘿嘿. 言归正传,这几天开始学习移动开发,使用的是Appcan平台.Appcan平台采用HTML5+CSS3做开发 实现跨平台,正好可以满足我们的业务需求. Appacn和数据库进行交互的方 ...

  5. Android进阶之Fragment与Activity之间的数据交互

    1 为什么 因为Fragment和Activity一样是具有生命周期,不是一般的bean通过构造函数传值,会造成异常. 2 Activity把值传递给Fragment 2.1 第一种方式,也是最常用的 ...

  6. unity用json和服务器数据交互

    第一种类型:服务器json数据是个对象 /// <summary> /// 获取用户信息初始化信息 /// </summary> void InitUserMessage() ...

  7. android通过HttpClient与服务器JSON交互

    通过昨天对HttpClient的学习,今天封装了HttpClient类 代码如下: package com.tp.soft.util; import java.io.BufferedReader; i ...

  8. Android项目实战(二十七):数据交互(信息编辑)填写总结

    前言: 项目中必定用到的数据填写需求.比如修改用户名的文字编辑对话框,修改生日的日期选择对话框等等.现总结一下,方便以后使用. 注: 先写实现过程,想要学习的同学可以看看,不需要的同学可以直接拉到最下 ...

  9. mui.ajax()和asp.net sql服务器数据交互【2】json数组和封装

    今天没有做循环创建显示:可以参考张鑫旭的文章:<基于HTML模板和JSON数据的JavaScript交互> 1.ashx页面代码 //下面的封装一般框架底层都是写好的:连接 数据库和获取D ...

随机推荐

  1. sql server2005查询分析器显示行号方法

    工具栏:工具--选项--文本编辑器---所有语言--右边复选框 行号 打上勾就ok了

  2. 删除文件,copy文件bat

    由于IntelliJ IDEA Build Artifacts速度实在是无法忍受,特整理了一个bat脚本方便maven install后更新jar文件到部署包 del /F /S /Q D:\Idea ...

  3. PythonProject(1)vim的Hustoj插件

    打算写一个vim的插件,或者emacs的插件.可以在编辑器里打比赛,看rank,交代码.总之相当于一个桌面版的hustoj 这是上学期就有的一个脑洞产物,昨天学了Python的爬虫,发现这个东西很有实 ...

  4. jenkins和sonar的几个问题

    错误1:有个pom文件内容错了,但是在jenkins上面编译的时候,控制台将这个错误信息给打出来了,maven的编译也打印了failed with error,但是jenkins的job并没有因此而停 ...

  5. Problem B. Harvest of Apples HDU - 6333(莫队)

    Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...

  6. MT【66]寻找对称中心

    设函数$f(x)=2x-cosx,{a_n}$是公差为$\frac{\pi}{8}$的等差数列,$f(a_1)+f(a_2)+f(a_3)+f(a_4)+f(a_5)=5\pi$,则 $[f(a_3) ...

  7. 04 Zabbix核心概念回顾

    04 Zabbix核心概念回顾 1. 监控四大核心功能: 数据采集----数据储存----数据展示和数据分析-----告警    1.1. 数据采集方式: SNMP:被监控设备上面必须启用SNMP a ...

  8. 自学Python1.8-python input/print用法 格式化输出

    自学Python之路 自学Python1.8-python input/print用法 格式化输出 1.input函数 Python3.x 中 input() 函数接受一个标准输入数据,返回为 str ...

  9. 洛谷 P3258 [JLOI2014]松鼠的新家 解题报告

    P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...

  10. Jenkins中使用Azure Powershell连接Service Fabric报错not recognized的原因与解决办法

    一.使用背景 在涉及Azure service Fabric的自动化应用场景中,依赖于Service Fabric的Azure Powershell cmdlets,我们可以使用Jenkins能实现c ...