项目目录

一、编写MainActivity.java

package com.hyzhou.getdemo;

import com.hyzhou.getdemo.service.LoginServer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText et_username, et_password; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
} public void click(View view) {
final String username = et_username.getText().toString().trim();
final String password = et_password.getText().toString().trim();
new Thread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
final String result = LoginServer
.loginByPost(username, password);
if (result != null) {
runOnUiThread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, result, 0).show();
}
});
} else {
runOnUiThread(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "请求失败", 0).show();
}
});
}
}
}).start();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
} }

二、编写LoginServer.java

/**
*
*/
package com.hyzhou.getdemo.service; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL; import com.hyzhou.getdemo.utiils.StreamTools; /**
* @author hyzhou
*
* 2013-11-6
*/
public class LoginServer {
public static String loginByPost(String username,String password)
{ try {
String path="http://192.168.1.54:8080/web/LoginServlet";
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.setDoOutput(true);
OutputStream os=conn.getOutputStream();
os.write(data.getBytes()); int code=conn.getResponseCode();
if (code==200) {
InputStream is=conn.getInputStream();
String text=StreamTools.readInputStream(is);
return text;
}else {
return null;
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

三、编写StreamTools.java

/**
*
*/
package com.hyzhou.getdemo.utiils; import java.io.ByteArrayOutputStream;
import java.io.InputStream; /**
* @author hyzhou
*
* 2013-11-6
*/
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);
/**
*
*/
package com.hyzhou.getdemo.utiils; import java.io.ByteArrayOutputStream;
import java.io.InputStream; /**
* @author hyzhou
*
* 2013-11-6
*/
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();
//试着解析result中的字符串
String temp=new String(result);
return temp;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "获取失败";
} }
}
/**
*
*/
package com.hyzhou.getdemo.utiils; import java.io.ByteArrayOutputStream;
import java.io.InputStream; /**
* @author hyzhou
*
* 2013-11-6
*/
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();
//试着解析result中的字符串
String temp=new String(result);
return temp;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return "获取失败";
} }
}

PS:相对get请求,Post请求相对复杂,需要指定Content-Type、Content-Length等

采用Post方式提交数据实例的更多相关文章

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

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

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

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

  3. 采用get方式提交数据到服务器实例

    GetDemo项目目录 一.编写StreamTools.java /** * */ package com.hyzhou.getdemo.utiils; import java.io.ByteArra ...

  4. 苹果微信浏览器不能post方式提交数据问题

    form表单中采用post方式提交数据时,在苹果的微信浏览器中无法传递,安卓的可以 如图: 在controller中获取该数据为 null 将表单的提交方式修改为get就能够获取到 现在采用Ajax方 ...

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

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

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

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

  7. Android(java)学习笔记210:采用post请求提交数据到服务器(qq登录案例)

    1.POST请求:  数据是以流的方式写给服务器 优点:(1)比较安全 (2)长度不限制 缺点:编写代码比较麻烦   2.我们首先在电脑模拟下POST请求访问服务器的场景: 我们修改之前编写的logi ...

  8. Android(java)学习笔记213:开源框架post和get方式提交数据(qq登录案例)

    1.前面提到Http的get/post方式  . HttpClient方式,实际工作的时候不常用到,因为这些方式编写代码是很麻烦的 2.Android应用会经常使用http协议进行传输,网上会有很完善 ...

  9. Android(java)学习笔记153:采用post请求提交数据到服务器(qq登录案例)

    1.POST请求:  数据是以流的方式写给服务器 优点:(1)比较安全 (2)长度不限制 缺点:编写代码比较麻烦   2.我们首先在电脑模拟下POST请求访问服务器的场景: 我们修改之前编写的logi ...

随机推荐

  1. Spring Cloud Config 配置中心 自动加解密功能 jasypt方式

    使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用  jasypt-spring-boot- ...

  2. 给data设置数据

    console.log(JSON.stringify(that.data.navigator[0].content) + "--____+" + JSON.stringify(th ...

  3. C++标准转换运算符static_cast

    该运算符把expression转换为type-id类型,但没有运行时类型检查来保证转换的安全性.   中文名 暂无 外文名 static_cast 分    类 强制类型转换 类    型 C++ s ...

  4. find & grep 命令 in linux(转)

    Linux下面工作,有些命令能够大大提高效率.本文就向大家介绍find.grep命令,他哥俩可以算是必会的linux命令,我几乎每天都要用到他们.本文结构如下: -exec,find命令对匹配的文件执 ...

  5. savReaderWriter 模块的使用

    作用: 由于python可以辅助数据分析和数据挖掘,读取文件, 而savReaderWriter模块就是为此而设计. 官网 :http://pythonhosted.org/savReaderWrit ...

  6. OpenGL中的二维编程——从简单的矩形开始

    一.OpenGL的组成 图元函数(primitive function)指定要生成屏幕图像的图元.包括两种类型:可以在二维.三维或者四维空间进行定义的几何图元,如多边形:离散实体:位图. 属性函数(a ...

  7. 关于session_cache_expire 的理解

    session_cache_limiter,它是session在客户端的缓存方式,有nocache,private,private_no_expire,publice主这几种. cache是属于浏览器 ...

  8. 图形界面至少要有一个顶级Swing容器

    图形界面至少要有一个顶级Swing容器 顶级Swing容器为其它Swing组件在屏幕上的绘制和处理事件提供支持 常用的顶级容器: JFrame(框架):表示主程序窗口 JDialog(对话框):每个J ...

  9. django学习2 视图和模板

    1 编写更多的视图 polls/views.py def detail(request, question_id): return HttpResponse("You're looking ...

  10. Build opencv libraries for android arm, x86 ubuntu

    废话不多说. 准备工作: 1. 下载源代码: http://opencv.org/ 编译平台:ubuntu14.04 opencv 2.4.6.1 本人用这样的办法编译了opecv 2.4.9 的没有 ...