android使用JSON数据和服务器进行交互
//点击按钮发送反馈信息给服务端,成功则进入优惠券界面
Button upload = (Button) findViewById(R.id.upload);
final String finalLatitude = latitude;
final String finalLongitude = longitude;
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获得建筑物名称
building=(EditText) findViewById(R.id.buildingInput);
final String buildingInput = building.getText().toString();
//获得楼层号
floor = (EditText)findViewById(R.id.floorInput);
final String floorInput = floor.getText().toString();
//获得手机号
telephone=(EditText)findViewById(R.id.telInput);
final String telInput = telephone.getText().toString();
//获得描述
description=(EditText)findViewById(R.id.descripInput);
final String descripInput = description.getText().toString();
if (buildingInput.length() <= 0
|| floorInput.length() <= 0
|| telInput.length() <= 0
|| descripInput.length() <= 0)
{
Toast.makeText(Feedback.this, "请完成反馈信息的填写",
Toast.LENGTH_LONG).show();
}
else{
new Thread() {
public void run() {
Looper.prepare();
final String urlPath = "http://10.8.176.105:8080/QRCodeAdmin
/commAction.action";
URL url;
try {
url = new URL(urlPath);
JSONObject feedbackInfo = new JSONObject();
feedbackInfo.put("building", buildingInput);
feedbackInfo.put("floor", floorInput);
feedbackInfo.put("telephone", telInput);
feedbackInfo.put("description", descripInput);
feedbackInfo.put("latitude", finalLatitude);
feedbackInfo.put("longitude", finalLongitude);
/**
*
* 封装feedback数组
使用JsonObject封装
{"building":"","floor":"","telephone":"","description":"",
"latitude":"","longitude":""}
*/
//将JSON数组转换成String类型使用输出流向服务器写
String content = String.valueOf(feedbackInfo);
//输出
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setDoOutput(true);//允许输出
conn.setRequestMethod("POST");//post方式输出
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();
os.write(content.getBytes());
os.close();
if (conn.getResponseCode() == 200) {
//传回信息,信息应当为优惠券的信息
InputStream is = conn.getInputStream();
//下面的Json数据是{"id":"","path":""}的string形式
String json = NetUtils.readString(is);
awardBean award = new awardBean();
JSONObject jsonObject = new JSONObject(json);
//获得award的id和path
String id = jsonObject.getString("id");
String path = jsonObject.getString("path");
//将获得的优惠券信息发送给award界面
Intent intent1 = new Intent(Feedback.this, Award.class);
intent1.putExtra("id", id);
intent1.putExtra("path", path);
startActivity(intent1);
} else {
Toast.makeText(Feedback.this, "发送失败",
Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Toast.makeText(Feedback.this, "发送失败",
Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
Toast.makeText(Feedback.this, "发送失败",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(Feedback.this, "发送失败",
Toast.LENGTH_SHORT).show();
}
}
}.start();
}
}
});
下面是awardBean(我也不知道这个bean写的是不是必要):
package com.example.euphemiaxiao.feedback; import java.io.Serializable;
/**
* Created by Euphemia Xiao on 2017/7/13.
*/ public class awardBean implements Serializable{
public String path;
public String id; public awardBean()
{
this.path = path;
this.id=id;
}
public String getPath()
{
return path;
} public String getId()
{
return id;
}
}
NetUtils是一个工具类:
package com.example.euphemiaxiao.feedback; import java.io.ByteArrayOutputStream;
import java.io.InputStream; /**
* Created by Euphemia Xiao on 2017/7/4.
*/ public class NetUtils {
public static byte[] readBytes(InputStream is){
try {
byte[] buffer = new byte[1024];
int len = -1 ;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while((len = is.read(buffer)) != -1){
baos.write(buffer, 0, len);
}
baos.close();
return baos.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null ;
}
public static String readString(InputStream is){
return new String(readBytes(is));
}
}
P.S.另一种接收(上面的接收是接收一条信息{"award":{"id":"","path":""}},下面的接收是接收一组信息{{"student":{"studentID":"","name":""}}{"student":{"studentID":"","name":""}}{"student":{"studentID":"","name":""}}...})
JSONArray actArray = jsonObject.getJSONArray("student");
for (int i = 0; i < actArray.length(); i++) {
//获得JSON数组中的每一个JSONObject对象
JSONObject actObject = actArray.getJSONObject(i);
String stuID=actObject.getString("studentID");
studentID.add(stuID);
String stuName = actObject.getString("name");
//studentName.add(stuName);
studentInfo.add(stuID+" "+stuName);
}
再P.S.获得一个json数据,前端对其进行处理,获取key和value
var obj = {"abc":"123"};
for(var key in obj) {
console.log(key+"+"obj[key])}
输出结果为abc+123
android使用JSON数据和服务器进行交互的更多相关文章
- struts2 的验证框架validation如何返回json数据 以方便ajax交互
struts2 的验证框架validation简单,好用,但是input只能输出到jsp页面通过struts2的标签<s:fielderror />才能取出,(EL应该也可以). 如果使 ...
- android基础---->JSON数据的解析
上篇博客,我们谈到了XML两种常用的解析技术,详细可以参见我的博客(android基础---->XMl数据的解析).网络传输另外一种数据格式JSON就是我们今天要讲的,它是比XML体积更小的数据 ...
- iOS开发网络篇—发送json数据给服务器以及多值参数
iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...
- 【转】iOS开发网络篇—发送json数据给服务器以及多值参数
原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...
- IOS-网络(发送JSON数据给服务器和多值参数)
三步走: 1.使用POST请求 2.设置请求头 [request setValue:@"application/json" forHTTPHeaderField:@"Co ...
- Android系列---JSON数据解析
您可以通过点击 右下角 的按钮 来对文章内容作出评价, 也可以通过左下方的 关注按钮 来关注我的博客的最新动态. 如果文章内容对您有帮助, 不要忘记点击右下角的 推荐按钮 来支持一下哦 如果您对文章内 ...
- Android中Json数据读取与创建
一: Json的特性和在数据交互中的地位就不用说了,直接看案例. 首先在android studio中创建assets文件目录,用于存放Json数据文件,android studio 1.3 默认项 ...
- Android 实现Json数据解析,并进行应用!
从网站上获取数据然后再客户端进行解析是常见的数据交互.下面是常用的一些接口网址: webservice工厂接口 http://www.36wu.com 快递查询接口http://webservice. ...
- Android中Json数据读取与创建的方法
转自:http://www.jb51.net/article/70875.htm 首先介绍下JSON的定义,JSON是JavaScript Object Notation的缩写. 一种轻量级的数据交换 ...
随机推荐
- 使用JS代码实现点击按钮下载文件
有时候我们在网页上需要增加一个下载按钮,让用户能够点击后下载页面上的资料,那么怎样才能实现功能呢?这里有两种方法: 现在需要在页面上添加一个下载按钮,点击按钮下载文件. 题外话,这个下载图标是引用的 ...
- 获取对象属性类型、属性名称、属性值的研究:反射和JEXL解析引擎
同步发布:http://www.yuanrengu.com/index.php/20170511.html 先简单介绍下反射的概念:java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所 ...
- 移动端页面点击a标签会有半透明的阴影或红色边框的bug
好久没有更新了,今天来一发 ^_^ 最近在写移动端页面,测试时发现一个a标签的bug:无论是iOS端还是Android端都存在,当点击a标签,会有一个矩形的透明的阴影闪一下(不同的浏览器阴影的颜色还不 ...
- Sping Boot入门到实战之入门篇(二):第一个Spring Boot应用
该篇为Spring Boot入门到实战系列入门篇的第二篇.介绍创建Spring Boot应用的几种方法. Spring Boot应用可以通过如下三种方法创建: 通过 https://start.spr ...
- Gitlab_服务器安装配置
1:开放防火墙端口 1. sudo yum install curl openssh-server openssh-clients postfix cronie -y sudo service p ...
- Spring Boot 2.0(二):Spring Boot 2.0尝鲜-动态 Banner
Spring Boot 2.0 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜. 配置依赖 使用 Spring Boot 2.0 首先需要将项目依赖包替换为刚刚发 ...
- 高性能JavaScript读书笔记
零.组织结构 根据引言,作者将全书划分为四个部分: 一.页面加载js的最佳方式(开发前准备) 二.改善js代码的编程技巧(开发中) 三.构建与部署(发布) 四.发布后性能检测与问题追踪(线上问题优化) ...
- onCreate源码分析
原文地址Android面试题-onCreate源码都没看过,怎好意思说自己做android Activity扮演了一个界面展示的角色,堪称四大组件之首,onCreate是Activity的执行入口,都 ...
- APP性能测试(电量)
#encoding:utf-8 import csv import os import time #控制类 class Controller(object): def __init__(self, c ...
- MFC使用SQLite 学习系列 二:无法容忍的数据插入效率
上一篇随笔中,介绍了,基本的使用没什么问题了,那么开始数据的插入. 一 问题--无法容忍的插入效率 代码写入基本完成,然后开始测试.起初,插入数据的时候基本上是插入每次插入9组数据,看不出来数据插入的 ...