源码链接

package com.zhangbz.submitdata.Utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder; import android.util.Log; public class NetUtils { private static final String TAG = "NetUtils";
/**
* 使用post的方式登录
* @param userName
* @param password
* @return
*/
public static String loginOfPost(String userName, String password){
HttpURLConnection conn = null;
try {
URL url = new URL("http://10.0.2.2:8080/serverzhangbz/servlet/LoginServlet?"); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");
conn.setReadTimeout(10000); //连接的超时时间
conn.setReadTimeout(5000); //读数据的超时时间
conn.setDoOutput(true);//必须设置此方法,允许输出
//conn.setRequestProperty("content-Length", 234); //设置请求头消息,可以设置多个 //post请求的参数
String data = "username=" + userName + "&password=" + password; //获得一个输出流,用于向服务器写数据,默认情况下,系统不予许向服务器输出内容
OutputStream out = conn.getOutputStream();
out.write(data.getBytes());
out.flush();
out.close(); int responseCode = conn.getResponseCode();
if(responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getSringFromInputStream(is);
Log.i(TAG, state);
return state;
} else {
Log.i(TAG, "访问失败:" + responseCode);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(conn != null) {
conn.disconnect(); //关闭连接
}
} return null;
} /**
* 使用get的方式登录
* @param userName
* @param password
* @return
*/
public static String logOfPost(String userName, String password) {
HttpURLConnection conn = null; //局部变量在使用时必须进行初始化
try {
String data = "username=" + URLEncoder.encode(userName) + "&password=" + URLEncoder.encode(password);
URL url = new URL("http://10.0.2.2:8080/serverzhangbz/servlet/LoginServlet?" + data);
conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET");//get或者post必须得全大写
conn.setReadTimeout(10000);//连接的超时时间
conn.setReadTimeout(5000);//读数据的超时连接 int responseCode = conn.getResponseCode();
if(responseCode == 200) {
InputStream is = conn.getInputStream();
String state = getSringFromInputStream(is);
Log.i(TAG, state);
return state;
} else {
Log.i(TAG, "访问失败:" + responseCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(conn != null) {
conn.disconnect(); //关闭连接
}
} return null; } /**
* 根据流返回一个字符串信息
* @param is
* @return
* @throws IOException
*/
private static String getSringFromInputStream(InputStream is) throws IOException{ ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1; while((len = is.read(buffer)) != -1){
baos.write(buffer, 0, len);
}
is.close();
String html = baos.toString(); //把流中的数据转换成字符串,采用的编码是:utf-8 //String html = new String(baos.toByteArray(), "GBK"); baos.close();
return html;
}
}

android 使用HttpURLConnection方式提交get/post请求的更多相关文章

  1. Android 采用post方式提交数据到服务器

    接着上篇<Android 采用get方式提交数据到服务器>,本文来实现采用post方式提交数据到服务器 首先对比一下get方式和post方式: 修改布局: <LinearLayout ...

  2. Android 使用Post方式提交数据(登录)

    在Android中,提供了标准Java接口HttpURLConnection和Apache接口HttpClient,为客户端HTTP编程提供了丰富的支持. 在HTTP通信中使用最多的就是GET和POS ...

  3. Android 使用Post方式提交数据

    在Android中,提供了标准Java接口HttpURLConnection和Apache接口HttpClient,为客户端HTTP编程提供了丰富的支持. 在HTTP通信中使用最多的就是GET和POS ...

  4. Android 使用HttpClient方式提交POST请求

    final String username = usernameEditText.getText().toString().trim(); final String password = passwr ...

  5. Android 使用HttpClient方式提交GET请求

    public void httpClientGet(View view) { final String username = usernameEditText.getText().toString() ...

  6. Android 采用get方式提交数据到服务器

    首先搭建模拟web 服务器,新建动态web项目,servlet代码如下: package com.wuyudong.web; import java.io.IOException; import ja ...

  7. android 通过post方式提交数据的最简便有效的方法

    public boolean post(String username, String password) throws Exception { username = URLEncoder.encod ...

  8. IE11在使用get方式提交没有进行请求的bug问题

    在做iemsc项目的时候,测试提交了一个bug问题,在发布新闻成功后,自动刷新列表的时候,不进行刷新,但是在谷歌上面又不会出现这种问题, 原因: 发现请求的时候用的get请求,因为不同的浏览器的请求机 ...

  9. android 之httpclient方式提交数据

    HttpClient: 今天实战下httpclient请求网络json数据,解析json数据返回信息,显示在textview, 起因:学校查询饭卡余额,每次都要访问校园网(内网),才可以查询,然后才是 ...

随机推荐

  1. Watch out for these 10 common pitfalls of experienced Java developers & architects--转

    原文地址:http://zeroturnaround.com/rebellabs/watch-out-for-these-10-common-pitfalls-of-experienced-java- ...

  2. NoSuchMethodError: antlr.collections.AST.getLine()I

    错误完整表述: Filter execution threw an exception] with root cause java.lang.NoSuchMethodError: antlr.coll ...

  3. Scrum 3.2 多鱼点餐系统开发进度(页面优化&下单详细信息页面)

    Scrum 3.2 多鱼点餐系统开发进度(页面优化&下单详细信息页面)  1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选 ...

  4. Entity FrameWork 延迟加载本质(二)

    1.对于外键实体而言,EF会在用到这个外键属性的时候,才会去查对应的表.这就是按需加载了... 2.按需加载的缺点:每次调用外键实体的时候,都会去查询数据库(EF有小优化:相同的外键实体只查一次) I ...

  5. STL --- UVA 123 Searching Quickly

    UVA - 123 Searching Quickly Problem's Link:   http://acm.hust.edu.cn/vjudge/problem/viewProblem.acti ...

  6. css样式表和选择器的优先级以及position元素属性值的区别

    css样式表优先级 问题:当同一个HTML元素被不止一个样式定义时,会使用哪个样式呢? 答:一般而言,所有的样式会根据下面的规则层叠于一个新的虚拟样式表中,其中数字4拥有最高的优先权. 1.浏览器缺省 ...

  7. 野比的示波器案例(Winfrom用户控件)

    使用该用户控件做的效果图,如果数据正确,可实现 波形.直线.等等效果图...... 对于本程序的认识还是不够深彻.如果有其他方法或算法,欢迎讨论下.将我所能理解的代码都再次标识了一番. ------- ...

  8. csharp: Export DataSet into Excel and import all the Excel sheets to DataSet

    /// <summary> /// Export DataSet into Excel /// </summary> /// <param name="send ...

  9. sql2008 查询字段所属表

    select a.name as 表名, g.*from sysobjects as a left join syscolumns as b on a.id=b.id left JOIN sys.ex ...

  10. JMS学习(一)基本概念

    这两天面试了一两个公司,由于简历中的最近一个项目用到了JMS,然而面试官似乎对这个很感兴趣,所以都被问到了,但可惜的是,我除了说我们使用了JMS外,面对他们提出的一些关于JMS的问题,我回答得相当差, ...