public static String getHttpText(String url) {
if (MyApplication.FOR_DEBUG) {
Log.i(TAG, "[getHttpText1]" + url);
}
Log.i(TAG, "[getHttpText2]" + url);
if (url == null || url.equals(""))
return null;

StringBuilder builder = new StringBuilder();
InputStreamReader isReader = null;
HttpURLConnection conn = null;
try {
URL u = new URL(url);
conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(TIMEOUT);
if (conn == null || conn.getResponseCode() != HttpURLConnection.HTTP_OK)
return null;
conn.connect();
isReader = new InputStreamReader(conn.getInputStream());
BufferedReader reader = new BufferedReader(isReader);
String buffer;
while ((buffer = reader.readLine()) != null) {
builder.append(buffer);
}
reader.close();
return builder.toString();
} catch (Exception e) {
Log.e(TAG, "getHttpText error: " + e.getMessage());
} finally {
if (isReader != null) {
try {
isReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null) {
conn.disconnect();
}
}
return null;
}

上面是json的请求方式,参数只需要传一个对应的服务器地址就行,下面是解析格式

String json = HttpUtils.getHttpText(
Configs.getServerAddress(context)
+ "/api.php/Message/getMessage"
+ Configs.getRoomInfo(context));
if (json == null || json.equals(""))
continue;

try {
JSONObject root = new JSONObject(json);
if (root.getInt("status") != 0)
return;

boolean gotMessage = false;
JSONArray ja = root.getJSONArray("messageList");
int len = ja.length();
long now = System.currentTimeMillis();
for (int i = 0; i < len; i++) {
JSONObject jo = ja.getJSONObject(i);
long deadline = jo.getLong("finish_time") * 1000;
if (deadline <= now) continue;
MarqueeContent content = new MarqueeContent();
content.id = mMarqueeId++;
content.deadline = deadline;
content.text = jo.getString("content");
mMarqueeList.add(content);
gotMessage = true;
}
if (gotMessage) {
context.sendBroadcast(new Intent(MarqueeView.GOT_MARQUEE_ACTION));
}
return;
} catch (JSONException e) {
e.printStackTrace();
}

android 比较完善json请求格式的更多相关文章

  1. Android为TV端助力 比较完善json请求格式

    public static String getHttpText(String url) { if (MyApplication.FOR_DEBUG) { Log.i(TAG, "[getH ...

  2. 如何用 Postman 处理 json请求格式

    下边是其他博友写的 http://blog.163.com/huan12_8/blog/static/130519090201611711213719/

  3. Android 下使用 JSON 实现 HTTP 请求

    不得不说,JSON 格式的确是非常美妙的,速度快而且简化了很多操作在 Android 下,Android SDK 已经为我们封装好了整个与 JSON 有关的操作,使用非常方便 以下就是一个标准的 JS ...

  4. Android 下使用 JSON 实现 HTTP 请求,外加几个示例!

    不得不说,JSON 格式的确是非常美妙的,速度快而且简化了很多操作在 Android 下,Android SDK 已经为我们封装好了整个与 JSON 有关的操作,使用非常方便 以下就是一个标准的 JS ...

  5. 实现android上解析Json格式数据功能

    实现android上解析Json格式数据功能,该源码转载于安卓教程网的,http://android.662p.com ,个人感觉还不错的,大家可以看看一下吧. package com.practic ...

  6. 提交json串格式的POST请求

    提交json串格式的POST请求 Action() { web_reg_save_param("retCode", "LB=retCode\":\"& ...

  7. Android框架Volley使用:Json请求实现

    首先我们在项目中导入这个框架: implementation 'com.mcxiaoke.volley:library:1.0.19' 在AndroidManifest文件当中添加网络权限: < ...

  8. .net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml)

    .net core将URL请求格式化为XML或JSON(网站动态生成sitemap.xml) 首先设置 Startup.cs 文件 配置 ConfigureServices services .Add ...

  9. 4种解决json日期格式问题的办法

    4种解决json日期格式问题的办法   开发中有时候需要从服务器端返回json格式的数据,在后台代码中如果有DateTime类型的数据使用系统自带的工具类序列化后将得到一个很长的数字表示日期数据,如下 ...

随机推荐

  1. Struts2 源码分析——Hello world

    新建第一个应用程序 上一章我们讲到了关于struts2核心机制.对于程序员来讲比较概念的一章.而本章笔者将会亲手写一个Hello world的例子.所以如果对struts2使用比较了解的朋友,请跳过本 ...

  2. HT For Web 拓扑图背景设置

    HT For Web 的HTML5拓扑图组件graphView背景设置有多种途径可选择: divBackground:通过css设置graphView对应的div背景 Painter:通过graphV ...

  3. Wamp 简单使用方法

    1.在wamp的安装目录 \bin\apache\Apache2.4.4\conf 中找到  httpd.conf文件删除 Include conf/extra/httpd-vhosts.conf 这 ...

  4. c# 访问SQL Server 其他会话正在使用事务的上下文

    这个异常是我第二次遇到! 从网上查找资料后知道了是由于数据库链接字符串是远程链接造成的,去掉链接服务器地址就行了,换成普通的链接字符串就可以了

  5. Expression<Func<TObject, bool>>与Func<TObject, bool>的区别

    Func<TObject, bool>是委托(delegate) Expression<Func<TObject, bool>>是表达式 Expression编译后 ...

  6. Linux下的C编程实战

    Linux下的C编程实战(一) ――开发平台搭建 1.引言 Linux操作系统在服务器领域的应用和普及已经有较长的历史,这源于它的开源特点以及其超越Windows的安全性和稳定性.而近年来, Linu ...

  7. 背水一战 Windows 10 (8) - 控件 UI: StateTrigger

    [源码下载] 背水一战 Windows 10 (8) - 控件 UI: StateTrigger 作者:webabcd 介绍背水一战 Windows 10 之 控件 UI VisualState 之 ...

  8. 重写onStart()函数

    Android开发中有时会遇到这种情况,在数据列表的Activity中点击添加按钮,弹出另一个Activity添加数据,这样返回数据列表的Activity时就需要刷新数据列表(因为添加了一条数据).这 ...

  9. windows下用eclipse+goclipse插件+gdb搭建go语言开发调试环境

    windows下用eclipse+goclipse插件+gdb搭建go语言开发调试环境   http://rongmayisheng.com/post/windows%E4%B8%8B%E7%94%A ...

  10. 实现在Android 下log的使用总结

    一:在源码开发模式下 1:包含头文件: #include <cutils/log.h>   2:定义宏LOG_TAG #define LOG_TAG "MY LOG TAG&qu ...