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. html 列表 ol 、ul 、dl

    html的列表分為無序列表(ul).有序列表(ol).自定義列表(dl). 無序列表: 以<ul>開始,列表項以<li>開始: 列表項可以是段落.圖像.連接.換行符.列表等: ...

  2. codeforces742B

    Arpa’s obvious problem and Mehrdad’s terrible solution CodeForces - 742B There are some beautiful gi ...

  3. mysql5.7 rpm安装教程

    注意版本和此次更新时间 2017-12-03  版本:mysql-5.7.20-1.el6.x86_64  环境:linux6.x ​官方下载地址: wget https://dev.mysql.co ...

  4. BZOJ3510 首都(LCT)

    即动态维护树的重心.考虑合并后的新重心一定在两棵树的重心的连线上.于是对每个点维护其子树大小,合并时在这条链的splay上二分即可.至于如何维护子树大小,见https://blog.csdn.net/ ...

  5. Educational Codeforces Round 4 B. HDD is Outdated Technology

    题目链接:http://codeforces.com/problemset/problem/612/B 解题思路: 一开始看错了题意,他要求的是从1-n所耗费的时间,n表示的是数值而不是下标, 实现代 ...

  6. QT 5 安装 vs2017 后,出现找不到 rc.exe 问题

    QT 5 安装 vs2017 后,出现找不到 rc.exe 问题 qt 5 cannot run 'rc.exe' 出现这种错误,是因为,rc.exe  未找到,也就是 SKD 路径不对. 找到相应的 ...

  7. UOJ#7. 【NOI2014】购票 | 线段树 凸包优化DP

    题目链接 UOJ #7 题解 首先这一定是DP!可以写出: \[f[i] = \min_{ancestor\ j} \{f[j] + (d[j] - d[i]) * p[i] + q[i]\}\] 其 ...

  8. emwin之窗口关闭按钮用法

    @2018-07-27 [小记] 使用函数 FRAMEWIN_AddCloseButton() 实现关闭当前窗口的功能时,调用其窗口的父窗口必须处于打开状态,否则将导致假死(当前窗口死了,系统还在工作 ...

  9. 迅雷thunder://协议解密

    echo -n 'thunder://''Cg==' | sed 's?thunder://??' | base64 -d | sed 's/^AA//; s/ZZ$//' 将thunder://替换 ...

  10. Luogu 1351 NOIP 2014 联合权值(贪心,计数原理)

    Luogu 1351 NOIP 2014 联合权值(贪心,计数原理) Description 无向连通图 G 有 n 个点,n-1 条边.点从 1 到 n 依次编号,编号为 i 的点的权值为 Wi, ...