Android获取服务器Json字符串并显示在ListView上面
已经好久没有更新博客,今天终于有新的东西可以记录了。
通过这次的任务学习到了以前没有注意到的知识点,真的有种书读百遍,其义自见的感觉。这次又重新认识了《Handler消息机制原理》。这次的任务中有更新UI。但是忘记了在Android4.0以后不能在UI线程访问网络,子线程也不能更新UI界面。下面我来展示一下这次的效果图。



这次的任务是:获取服务器端的json字符串,并解析显示在Android界面上。
当我接到这个任务的时候,首先想到的是利用Fragment布局加上ListView布局。但是因为没有完全掌握,所以失败了。这是就想到了现在的方案。利用GridView加上ListView来实现。布局是没有问题。但是写代码的时候却出现了问题。当我获取到数据并顺利解析的时候。却怎么也传不到ListView。总是显示它的size为0.但是通过打印确实获取到了。这个时候老师让我利用Handler,便能顺利解决问题了。原因就是一开始,我写到的不能在子线程更新UI。
下面我来粘出我的代码以供大家参考:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/set_bj">
<GridView
android:id="@+id/gv_icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="4"
android:layout_alignParentBottom="true"> </GridView>
</RelativeLayout>
item_gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"> <TextView
android:id="@+id/tv_icons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="功能"
android:layout_centerInParent="true"
android:textSize="13sp"
android:textColor="#000000"/> </RelativeLayout>
activity_select.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_id"
android:layout_width="0dp"
android:layout_weight="2"
android:textColor="@android:color/black"
android:layout_height="wrap_content" android:textSize="15sp"
android:text="题号"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_weight="1" android:layout_height="wrap_content"
android:textSize="15sp"
android:text="题目"/>
<TextView
android:id="@+id/tv_tech"
android:layout_width="0dp"
android:layout_weight="7"
android:gravity="center"
android:layout_height="wrap_content"
android:textSize="15sp"
android:text="要求"/> </LinearLayout> <ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp">
</ListView> </LinearLayout>
item_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_id"
android:layout_width="0dp"
android:layout_weight="1"
android:textColor="@android:color/black"
android:layout_height="wrap_content"
android:gravity="center"
android:text="题号"/>
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center"
android:layout_height="wrap_content"
android:text="题目"/>
<TextView
android:id="@+id/tv_tech"
android:layout_width="0dp"
android:layout_weight="6"
android:gravity="center"
android:layout_height="wrap_content"
android:text="要求"/> </LinearLayout>
MainActivity.java
package com.rjxy.student; import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView; public class MainActivity extends Activity {
private GridView gv_Home;
private String[] settingText = {"全部题目","我的选题","个人信息","我的密码"}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv_Home = (GridView) findViewById(R.id.gv_icons);
gv_Home.setAdapter(new MyAdapter());
gv_Home.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
System.out.println(position);
switch (position) {
case 0:
//查看全部题目 Intent intent = new Intent(MainActivity.this,SetlectActivity.class);
startActivity(intent);
break;
case 1:
//我的选题
break;
case 2:
//个人信息
break;
case 3:
//修改密码
break;
default: break;
} } });
} class MyAdapter extends BaseAdapter{ @Override
public int getCount() { return settingText.length;
} @Override
public Object getItem(int position) { return settingText[position];
} @Override
public long getItemId(int position) { return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(getApplicationContext(), R.layout.item_gridview, null);
TextView tv_Home = (TextView) view.findViewById(R.id.tv_icons);
tv_Home.setText(settingText[position]);
return view;
} } }
SetlectActivity.java
package com.rjxy.student; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView; public class SetlectActivity extends Activity {
private static final int CHANGE_UI = 1;
private static final int ERROR = 2;
private ListView lv;
private List<Data> datas = new ArrayList<Data>();
//主线程创建消息处理器
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
if (msg.what == CHANGE_UI) {
try {
JSONArray arr = new JSONArray((String)msg.obj);
for (int i = 0; i < arr.length(); i++) {
JSONObject temp = (JSONObject) arr.get(i);
// Log.d("json", temp.getInt("id")+temp.getString("exp_name")+temp.getString("exp_tech"));
Data data = new Data();
data.setId(temp.getInt("id"));
data.setExp_name(temp.getString("exp_name"));
data.setExp_tech(temp.getString("exp_tech"));
//这个地方可以获取到值但是适配器那位0
datas.add(data); }
lv.setAdapter(new MyAdapter());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
lv = (ListView) findViewById(R.id.lv);
select(); } private void select(){
//子线程更新UI
new Thread(){
public void run(){
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
String path = "http://10.6.78.254:2016/xampp/graduate/index.php/home/Student/test_android";
HttpGet httpGet = new HttpGet(path);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { BufferedReader reader = new BufferedReader
(new InputStreamReader(response.getEntity().getContent(),"UTF-8"));
for(String s=reader.readLine();s!=null;s=reader.readLine())
{
builder.append(s);
}
String content = builder.toString();
//通知主线程更新UI
Message message = new Message();
message.what = CHANGE_UI;
message.obj = content;
handler.sendMessage(message);
}else{
Log.e(MainActivity.class.toString(), "Failed");
}
} catch (ClientProtocolException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace();
} };
}.start();
}
class MyAdapter extends BaseAdapter{ @Override
public int getCount() {
Log.d("AAA", ""+datas.size());
return datas.size(); } @Override
public Object getItem(int position) { return datas.get(position);
} @Override
public long getItemId(int position) { return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = View.inflate(SetlectActivity.this, R.layout.item_listview, null);
TextView id = (TextView) view.findViewById(R.id.tv_id);
TextView exp_name = (TextView) view.findViewById(R.id.tv_name);
TextView exp_tech = (TextView) view.findViewById(R.id.tv_tech);
Data data = datas.get(position);
Log.d("aaaaa",datas.get(position).getExp_name() ); id.setText(String.valueOf(datas.get(position).getId()));
exp_name.setText(datas.get(position).getExp_name());
//Log.i("exp_name", datas.get(position).getExp_name());
exp_tech.setText(datas.get(position).getExp_tech());
return view;
} }
}
Data.java
package com.rjxy.student;
public class Data {
private int id;
private String exp_name;
private String exp_tech;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getExp_name() {
return exp_name;
}
public void setExp_name(String exp_name) {
this.exp_name = exp_name;
}
public String getExp_tech() {
return exp_tech;
}
public void setExp_tech(String exp_tech) {
this.exp_tech = exp_tech;
}
@Override
public String toString() {
return "Data [id=" + id + ", exp_name=" + exp_name + ", exp_tech="
+ exp_tech + "]";
}
}
添加权限:
<uses-permission android:name="android.permission.INTERNET"/>
服务器端的代码:ThinkPHP代码为:
public function test_android(){
$myModel = new \Think\Model();
$result=$myModel->query("select id,exp_name,exp_tech from g_experiment ;");
echo json_encode($result);
}
Android获取服务器Json字符串并显示在ListView上面的更多相关文章
- 识别Json字符串并分隔成Map集合
识别Json字符串并分隔成Map集合 前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新 ...
- wemall app商城源码Android 获取XML网络数据并绑定到ListView
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享Android 获取XML网络数据并绑定到Li ...
- android 读取本地json文件 解决显示乱码显示
1.读取本地JSON ,但是显示汉字乱码 public static String readLocalJson(Context context, String fileName){ ...
- 菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析
一.概述. SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到. 二.代码演示. 需要添加的jar包. 2.1 we ...
- Java版本:识别Json字符串并分隔成Map集合
前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...
- java对象转化为json字符串并传到前台
package cc.util; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import ...
- Android获取屏幕实际高度跟显示高度,判断Android设备是否拥有虚拟功能键
//获取屏幕尺寸,不包括虚拟功能高度 getWindowManager().getDefaultDisplay().getHeight(); 获取屏幕原始尺寸高度,包括虚拟功能键高度, private ...
- Android解析服务器Json数据实例
Json数据信息如下: { "movies": [ { "movie": "Avengers", "year": 201 ...
- asp.net用sql数据库生成json字符串并显示出来
use Shop ,) )) insert into DictBase select '包装' UNION ALL select '价格' UNION ALL select '品牌' 工厂方法模式 I ...
随机推荐
- ASP.NET MVC5 网站开发实践(二) Member区域 - 添加文章
上次把架构做好了,这次做添加文章.添加文章涉及附件的上传管理及富文本编辑器的使用,早添加文章时一并实现. 要点: 富文本编辑器采用KindEditor.功能很强大,国人开发,LGPL开源,自己人的好东 ...
- EntityFramework 7 更名为EntityFramework Core(预发布状态)
前言 最近很少去学习和探索新的东西,尤其是之前一直比较关注的EF领域,本身不太懒,但是苦于环境比较影响自身的心情,所以迟迟没有下笔,但是不去学习感觉在精神层面缺少点什么,同时也有园友说EF又更新了,要 ...
- Oracle Database 11g Express Editon介绍及安装
一.Oracle Database 11g Express版本介绍 公司项目开发中,使用的数据库是Oracle 10g和MySQL 5.5,最新因为开发需要,需要从后台读取一些数据.使用的客户端是PL ...
- 应用程序框架实战十一:创建VS解决方案与程序集
上一篇,介绍了开发环境需要的工具和版本,本篇将动手创建VS解决方案. 对于本系列文章提供的示例,我想通过两种途径来演示,一种是单元测试,另外为了能更直观的看到效果,还会提供一个用户界面来展示.为了不分 ...
- 从零开始编写自己的C#框架(9)——数据库设计与创建
对于千万级与百万级数据库设计是有所区别的,由于本项目是基于中小型软件开发框架来设计,记录量相对会比较少,所以数据库设计时考虑的角度是:与开发相结合:空间换性能:空间换开发效率:减少null异常.... ...
- Puppet简易入门
一.查看官方提供的下载源 https://docs.puppet.com/guides/puppetlabs_package_repositories.html 二. 选择对应系统的下载源 因为本机是 ...
- JavaScript 框架设计(二)
JavaScript 高级框架设计 (二) 上一篇,JavaScript高级框架设计(一)我们 实现了对tag标签的选择 下来我们实现对id的选择,即id选择器. 我们将上一篇的get命名为getTa ...
- 使用Spire.Barcode程序库生成二维码
使用Spire.Barcode程序库生成二维码 某天浏览网页发现了一个二维码的程序库.它的描述说他可以扫描二维码图像.我很感兴趣,想试试他是不是会有用.所以我就用了些方法扫描二维码图像来测试一下.结果 ...
- 对来自于Azure的远程连接文件(.rdp)的另一种更便捷的自定义方法
在上一篇日志中(很抱歉那张比较黑的截图)介绍了如何获得Azure中的Windows虚拟机的远程连接文件,以及一种基于文本编辑方式进行自定义的方法. 实际上对于在Windows下的用户来说,我们可以使用 ...
- 原生JS实现jquery的链式编程。
这是我根据之前遇到的一个面试题,题目:用原生JS实现$("#ct").on("click",fn).attr("id"). 然后看了篇jqu ...