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 { //使用URL的Get 方式请求服务器
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;
} //使用URL的Post 方式请求服务器
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;
} //使用HttpClient的Get 方式请求服务器 (HttpClient 过时)
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;
} //使用HttpClient的Post 方式请求服务器 (HttpClient 过时)
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;
} }

//将InputStream 转为 String

package mydemo.mycom.demo2.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream; public class StreamTools { //InputStream 转为 String
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 app与服务器交互的更多相关文章

  1. Android和FTP服务器交互,上传下载文件(实例demo)

    今天同学说他备份了联系人的数据放在一个文件里,想把它存到服务器上,以便之后可以进行下载恢复..于是帮他写了个上传,下载文件的demo 主要是 跟FTP服务器打交道-因为这个东东有免费的可以身亲哈 1. ...

  2. Android与PHP服务器交互

    转自:http://blog.csdn.net/ab_ba/article/details/7912424 服务器端:server.php 1 <?php 2         include(' ...

  3. java攻城狮之路(Android篇)--与服务器交互

    一.图片查看器和网页源码查看器 在输入地址的是不能输入127.0.0.1 或者是 localhost.ScrollView :可以看成一个滚轴 可以去包裹很多的控件在里面 练习1(图片查看器): pa ...

  4. Android客户端与服务器交互中的token

    学习Token Token是什么? Token是服务端生成的一串字符串,以作客户端进行请求的一个令牌,当第一次登录后,服务器生成一个Token便将此Token返回给客户端,以后客户端只需带上这个Tok ...

  5. Android 客户端与服务器交互

    在android中有时候我们不需要用到本机的SQLite数据库提供数据,更多的时候是从网络上获取数据,那么Android怎么从服务器端获取数据呢?有很多种,归纳起来有 一:基于Http协议获取数据方法 ...

  6. Android数据与服务器交互的GET,POST,HTTPGET,HTTPPOST的使用

    Android有这几种方式,可以提交数据到服务器,他们是怎么使用的呢,这里我们来探讨一下. 这里的例子用的都是提交客户端的用户名及密码,同时本节用到的StreamTools.readInputStre ...

  7. android笔记--与服务器交互更改简历状态

    private AsyncHttpClient asyncHttpClient; private Dialog dialog; /** * 改变简历状态 */ private void postcha ...

  8. Android客户端与服务器

    就是普通的服务器端编程,还不用写界面,其实还比服务器编程简单一些.跟J2EE一样的服务器,你android这一方面只要用json或者gson直接拿数据,后台的话用tomcat接受请求操作数据,功能不复 ...

  9. Android客户端与服务器交互方式-小结

    最近的Android项目开发过程中一个问题困扰自己很长时间,Android客户端与服务器交互有几种方式,最常见的就是webservices和json.要在Android手机客户端与pc服务器交互,需要 ...

随机推荐

  1. Python 零基础 快速入门 趣味教程 (咪博士 海龟绘图 turtle) 6. 条件

    前面的教程中,我们已经可以让小海龟绘制出各种图形了.但是,所有绘图的代码都是预先编好的,程序一旦运行起来,运行结果(绘制的图形)就是固定不变的.这一节中,咪博士将教大家如何让海龟响应用户的输入. im ...

  2. pinv

    moore-penrose pseudoinverse of matrix伪逆

  3. codeforces471B

    MUH and Important Things CodeForces - 471B It's time polar bears Menshykov and Uslada from the zoo o ...

  4. BZOJ3129 SDOI2013方程(容斥原理+扩展lucas)

    没有限制的话算一个组合数就好了.对于不小于某个数的限制可以直接减掉,而不大于某个数的限制很容易想到容斥,枚举哪些超过限制即可. 一般情况下n.m.p都是1e9级别的组合数没办法算.不过可以发现模数已经 ...

  5. MT【21】任意基底下的距离公式

    解析: 评:$\theta=90^0$时就是正交基底下(即直角坐标系下)的距离公式.

  6. CF528D Fuzzy Search 【NTT】

    题目链接 CF528D 题解 可以预处理出\(S\)每个位置能匹配哪些字符 对每种字符 构造两个序列 如果\(S[i]\)可以匹配该字符,则该位置为\(0\),否则为\(1\) 如果\(T[i]\)可 ...

  7. A1056. Mice and Rice

    Mice and Rice is the name of a programming contest in which each programmer must write a piece of co ...

  8. __slots__,__doc__,__del__,__call__,__iter__,__next__迭代器协议(三十六)

    1.__slots__是什么:是一个类变量,变量值可以是列表,元祖,或者可迭代对象,也可以是一个字符串(意味着所有实例只有一个数据属性) 2.引子:使用点来访问属性本质就是在访问类或者对象的__dic ...

  9. 集合类(常见的集合类:Collection、List、Set、ArrayList、linkedList、Vector、HashSet、TreeSet)

    一.集合类 定义:一种为了对多个对象进行操作而进行存储的方式. 1.与数组的区别: 数组:可以存储对象,也可以存储基本数据类型,但是一次只能存储一种类型,数组长度固定. 集合:只能存储对象,长度可变, ...

  10. NOIP 普及组 2016 回文日期

    传送门 https://www.cnblogs.com/violet-acmer/p/9859003.html 题解: 思路1: 相关变量解释: year1,month1,day1 : date1对应 ...