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();
}

最新文章

  1. 函数randint的使用
  2. [windows驱动]内核态驱动架构
  3. PHP 开发环境配置
  4. 子窗体显示在任务栏,且子窗体中又有弹窗(CreateParams修改三个风格参数)
  5. mysql数据库的安装以及常见优化设置
  6. Android源码编译jar包BUILD_JAVA_LIBRARY 与BUILD_STATIC_JAVA_LIBRARY的区别(二)
  7. offsetWidth相关js属性
  8. Python random模块(获取随机数)
  9. vuex 入坑篇
  10. Desktop Central —— Windows 管理工具
  11. why microsoft named their cloud service Azure?
  12. IDEA一定要懂的32条快捷键
  13. SpringBoot快速开始Hello World
  14. Bootstrap -- 模态框实现拖拽移动
  15. 文件系统--fs(读)--fs.read
  16. shell统计昨天的独立ip
  17. Redis之Python操作
  18. Android Intent 教程
  19. Laravel 5.x HTTPS反向代理的实现
  20. 29.Combination Sum(和为sum的组合)

热门文章

  1. fastcgi配置
  2. 插入排序---希尔插入排序算法(Javascript版)
  3. Android ADB 命令大全
  4. 分享15个HTML5工具
  5. Android使用SQLite数据库(4)
  6. 口袋微博android源码服务端和客户端
  7. MongoDB学习-在.NET中的简单操作
  8. 用Apache 里面的ab做一个简单的压力测试
  9. extern的用法
  10. 【LoadRunner】OSGI性能测试实例