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. matplotlib绘图

    fig = plt.figure() ax=plt.gca() timeList = np.array(timeList) timeList=timeList*100 timeList1 = np.a ...

  2. shell 学习笔记二

    一.break命令 break命令允许跳出所有循环(终止执行后面的所有循环). 下面的例子中,脚本进入死循环直至用户输入数字大于5.要跳出这个循环,返回到shell提示符下,就要使用break命令. ...

  3. Macbook系统环境安装wget的2个方法 - 传统包及Homebrew安装

    文章目录 这里有2个方法可以安装wget命令工具: 考虑到自身项目的拓展需要,朋友建议学习Python爬虫这样对于做大数据采集有较大的帮助,老蒋虽然每天也都接触一些脚本和程序的修改,但是并没有专业和系 ...

  4. 常见的HTTP报头(头参数)

    本内容摘抄自<RESTful WebServices> 中文译本附录C '常见的HTTP报头'. 原文作者:Leonard Ricbardson & Sam Ruby 翻译:徐涵. ...

  5. Lodop窗口的按钮、权限,隐藏或设置功能不可用

    Lodop隐藏某个按钮或部分,具体参考Lodop技术手册 SET_SHOW_MODE篇.以下是几个例子,(对应下图图片): 第一种:LODOP.SET_SHOW_MODE ("HIDE_PB ...

  6. 腾讯云 Ubuntu16.04 搭建Git 服务

    一.安装Git服务器所需软件 1.安装git-core, openssh-server, openssh-client三个软件.git-core是git的核心软件: openssh-server.op ...

  7. docker --Dockerfile--一些语法

    环境更换 环境变量(与声明的ENV声明),也可以在特定指令作为变量用来被解释 Dockerfile.转义也被处理,从字面上包含类似于变量的语法. 环境变量Dockerfile用 $variable_n ...

  8. BZOJ1014 JSOI2008火星人(splay+哈希)

    splay维护哈希值即可. #include<iostream> #include<cstdio> #include<cmath> #include<cstd ...

  9. MT【232】展开式中的系数

    $(1+x+x^2+\cdots+x^{100})^3$展开式中$x^{150}$前的系数为_____ 解答:$(1+x+x^2+\cdots+x^{100})^3=(1-x^{101})^3\sum ...

  10. MT【211】保序同构

    设$S,T$是$R$的两个非空子集,如果存在一个从$S$到$T$的函数$y=f(x)$满足:$1)T=\{f(x)|x\in S\};$2)对任意$x_1,x_2\in S$,当$x_1<x_2 ...