与android客户端以JSON格式进行数据通信
2012-11-27 09:48
447 查看
AndroidClient代码:
######################################
package android.test;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;
import yt.tool.Log;
public class AndroidClient {
@Test
public void HttpPostData() throws Exception {
for(int i = 0;i < 1;i++){
HttpClient httpclient = new DefaultHttpClient();
String uri ="http://192.168.1.55:8888/android/AndroidService";
HttpPost httppost = new HttpPost(uri);
//添加http头信息
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("User-Agent", "mozilla/5.0");
JSONObject obj = new JSONObject();
obj.put("age", 26);
obj.put("name", "nikerlover");
obj.put("password", 45667);
System.out.println("测试结果:"+obj.toString());
StringEntity se = new StringEntity(obj.toString());
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
//检验状态码,如果成功接收数据
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
System.out.println("######################success####################");
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while((line = reader.readLine()) !=null){
System.out.println(line);
}
}
}
}
public static String readJSONString(InputStream inputStream){
StringBuffer json = new StringBuffer();
int line;
try {
BufferedInputStream reader = new BufferedInputStream(inputStream);
System.out.println("测试返回结果内容1");
while((line = reader.read()) != 0) {
json.append(line);
}
Log.w("返回服务器端结果", "内容:" + json);
System.out.println("测试返回结果内容2");
reader.close();
}
catch(Exception e) {
System.out.println(e.toString());
}
return json.toString();
}
}
###############################
AndroidService代码
#############################
package android.test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import yt.tool.Log;
import net.sf.json.JSONObject;
public class AndroidService extends HttpServlet {
private String Json;
private int age;
private String name;
private String password;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
String jsonContent = readJSONString(req);
JSONObject obj= JSONObject.fromObject(jsonContent);
AndroidService ase =(AndroidService)JSONObject.toBean(obj, AndroidService.class);
Log.w("年龄数据", "内容:" + ase.getAge());
resp.setContentType("application/json;charset=UTF-8");
Writer out = resp.getWriter();
out.write(jsonContent);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getJson() {
return Json;
}
public void setJson(String json) {
Json = json;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) !=-1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();//网页的二进制数据
outStream.close();
inStream.close();
return data;
}
public static String readJSONString(HttpServletRequest request){
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while((line = reader.readLine()) != null) {
json.append(line);
}
Log.w("httpclient测试", "内容:" + json);
}
catch(Exception e) {
System.out.println(e.toString());
}
return json.toString();
}
}
###################################
web.xml中的配置
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>fetchInfo</servlet-name>
<servlet-class>android.test.AndroidService</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fetchInfo</servlet-name>
<url-pattern>/android/AndroidService</url-pattern>
</servlet-mapping>
</web-app>
######################################
package android.test;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;
import yt.tool.Log;
public class AndroidClient {
@Test
public void HttpPostData() throws Exception {
for(int i = 0;i < 1;i++){
HttpClient httpclient = new DefaultHttpClient();
String uri ="http://192.168.1.55:8888/android/AndroidService";
HttpPost httppost = new HttpPost(uri);
//添加http头信息
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("User-Agent", "mozilla/5.0");
JSONObject obj = new JSONObject();
obj.put("age", 26);
obj.put("name", "nikerlover");
obj.put("password", 45667);
System.out.println("测试结果:"+obj.toString());
StringEntity se = new StringEntity(obj.toString());
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost);
//检验状态码,如果成功接收数据
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
System.out.println("######################success####################");
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while((line = reader.readLine()) !=null){
System.out.println(line);
}
}
}
}
public static String readJSONString(InputStream inputStream){
StringBuffer json = new StringBuffer();
int line;
try {
BufferedInputStream reader = new BufferedInputStream(inputStream);
System.out.println("测试返回结果内容1");
while((line = reader.read()) != 0) {
json.append(line);
}
Log.w("返回服务器端结果", "内容:" + json);
System.out.println("测试返回结果内容2");
reader.close();
}
catch(Exception e) {
System.out.println(e.toString());
}
return json.toString();
}
}
###############################
AndroidService代码
#############################
package android.test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import yt.tool.Log;
import net.sf.json.JSONObject;
public class AndroidService extends HttpServlet {
private String Json;
private int age;
private String name;
private String password;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try {
String jsonContent = readJSONString(req);
JSONObject obj= JSONObject.fromObject(jsonContent);
AndroidService ase =(AndroidService)JSONObject.toBean(obj, AndroidService.class);
Log.w("年龄数据", "内容:" + ase.getAge());
resp.setContentType("application/json;charset=UTF-8");
Writer out = resp.getWriter();
out.write(jsonContent);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public String getJson() {
return Json;
}
public void setJson(String json) {
Json = json;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static byte[] readInputStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = inStream.read(buffer)) !=-1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();//网页的二进制数据
outStream.close();
inStream.close();
return data;
}
public static String readJSONString(HttpServletRequest request){
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while((line = reader.readLine()) != null) {
json.append(line);
}
Log.w("httpclient测试", "内容:" + json);
}
catch(Exception e) {
System.out.println(e.toString());
}
return json.toString();
}
}
###################################
web.xml中的配置
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>fetchInfo</servlet-name>
<servlet-class>android.test.AndroidService</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>fetchInfo</servlet-name>
<url-pattern>/android/AndroidService</url-pattern>
</servlet-mapping>
</web-app>
相关文章推荐
- android客户端与javaweb服务器端数据通信-Post-Json格式数据
- android 客户端与服务器端之间采用JSON进行数据通信之一
- Android客户端与服务器HTTP交互,数据以json格式传输
- Android网络通信中数据传输的JSON格式使用
- ArcGIS Android应用客户端加载JSON格式业务数据
- 转 winfrom如何通过http来进行通信,并且通过传递json格式的数据可接受json格式的数据
- Android访问网络:服务端返回XML或JSON格式数据,Android 进行解析并使用ListView显示
- android使用JSON进行网络数据交换(服务端、客户端)的实现
- android项目,从服务器端返回JSON数据,在android客户端进行显示。
- Android学习笔记:在Android平台上通过JSON格式与服务器端进行数据交互
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- Android网络开发中如何使用JSON进行网络通信---Android JSON数据通讯方法解析
- 如果希望给Android客户端提供JSON格式的数据,服务端应该用什么技术编写呢?
- Android之从网络中获取数据并返回客户端的两种方式:XML格式返回与Json格式返回
- ArcGIS Android应用客户端加载JSON格式业务数据
- 客户端传递json格式数据,spring mvc服务端接受并进行参数校验
- Android 中通过HTTPRequest 与服务端进行 JSON格式数据交互
- Android客户端处理服务器端返回的Json格式数据
- Android入门:通过JSON数据与服务器进行通信
- android端和pc端使用usb进行socket通信,其中android是服务器端,pc是客户端。如何实现安卓端输入的数据通过按钮发送到pc端?