在我的应用中,我在资源文件夹中存储了一个JSON文件。现在我正在尝试解析本地JSON文件以在recyclerview中显示一些信息。我的Json文件看起来像这样。
{
"contacts":[
{
"_id": "57f762f1",
"dn": "CNdreas ",
"mobile": "123456",
"mail": "and@yahoo.com",
"name": "Hallo",
"company": "xyz company",
"department": "software",
"title": "Junior Developer",
"__v": 0,
"updated_at": "2016-10-07T08:55:13.909Z"
},
{
"_id": "57f7",
"dn": "CNernard",
"mobile": "1239855",
"mail": "mail@yahoo.com",
"name": "nameis",
"company": "xyz Company",
"department": "Inhouse",
"__v": 0,
"updated_at": "2016"
},
..................lots of object
从这个文件我想获得手机,邮件,名称,公司,部门和职位。但在一些对象"标题"不包括在内。在我的例子中显示。我尝试在recyclerview和cardview中解析这些信息。但每次我的应用程序崩溃。我没有得到如何从这个JSON解析一些数据并在RecyclerView上显示。这是我的JSON数据模型类
public class MyColleageModel {
private String _id;
private String dn;
private String mobile;
private String mail;
private String name ;
private String company ;
private String department;
private String title ;
private int __v ;
private String updated_at;
public MyColleageModel() {
}
public MyColleageModel(String mobile, String mail,
String name, String company,
String department, String title) {
this.mobile = mobile;
this.mail = mail;
this.name = name;
this.company = company;
this.department = department;
this.title = title;
}
public String get_id() {
return _id;
}
public String getDn() {
return dn;
}
public String getMobile() {
return mobile;
}
public String getMail() {
return mail;
}
public String getName() {
return name;
}
public String getCompany() {
return company;
}
public String getDepartment() {
return department;
}
public String getTitle() {
return title;
}
public int get__v() {
return __v;
}
public String getUpdated_at() {
return updated_at;
}
}
我的主要活动
public class MyColleaguesPage extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<MyColleageModel> colleagueObject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
recyclerView=(RecyclerView)findViewById(R.id.colleagues_recycler);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
colleagueObject = new ArrayList<>();
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("contacts");
ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;
for (int i = 0; i < m_jArry.length(); i++) {
JSONObject jo_inside = m_jArry.getJSONObject(i);
String val1 = jo_inside.getString("mobile");
String val2 = jo_inside.getString("mail");
String val3 = jo_inside.getString("name");
String val4 = jo_inside.getString("company");
String val5 = jo_inside.getString("department");
String val6 = jo_inside.optString("title");
//Add your values in your `ArrayList` as below:
m_li = new HashMap<String, String>();
m_li.put("mobile", val1);
m_li.put("mail",val2);
m_li.put("name", val3);
m_li.put("company",val4);
m_li.put("department", val5);
m_li.put("title",val6);
formList.add(m_li);
}
} catch (JSONException e) {
e.printStackTrace();
}
recyclerViewstart();
loadJSONFromAsset();
adapter = new MyColleaguesAdapter(colleagueObject,this);
recyclerView.setAdapter(adapter);
}
private String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open("colleagues.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
}
我收到空指针异常错误这里是我的错误日志。我知道在某些情况下标题不包括在内。但是如何在Recyclerview中显示我的所有数据而没有任何错误?
07-15 10:51:05.927 2406-2406/? W/art: Before Android 4.1, method int
android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int,
boolean) would have incorrectly overridden the package-private method in
android.widget.ListView
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err:
org.json.JSONException: No value for title
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
org.json.JSONObject.get(JSONObject.java:389)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
org.json.JSONObject.getString(JSONObject.java:550)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
myColleagues.MyColleaguesPage.onCreate(MyColleaguesPage.java:55)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
android.app.Activity.performCreate(Activity.java:6662)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
android.app.ActivityThread.-wrap12(ActivityThread.java)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
android.os.Handler.dispatchMessage(Handler.java:102)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
android.os.Looper.loop(Looper.java:154)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
android.app.ActivityThread.main(ActivityThread.java:6077)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
java.lang.reflect.Method.invoke(Native Method)
07-15 10:51:08.049 2406-2406/com.testgrid W/System.err: at
答案 0 :(得分:0)
我认为你放错了recyclerview init
recyclerViewstart(); //first
然后
adapter = new MyColleaguesAdapter(colleagueObject,this);
recyclerView.setAdapter(adapter);
喜欢这个
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mycolleagues_layout);
colleagueObject = new ArrayList<>();
try {
JSONObject obj = new JSONObject(loadJSONFromAsset());
JSONArray m_jArry = obj.getJSONArray("contacts");
ArrayList<HashMap<String, String>> formList = new ArrayList<HashMap<String, String>>();
HashMap<String, String> m_li;
......
} catch (JSONException e) {
e.printStackTrace();
}
recyclerViewstart();
adapter = new MyColleaguesAdapter(colleagueObject,this);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
答案 1 :(得分:0)
你应该致电gdb
,用它来查找recyclerView,然后你可以recyclerViewstart()
答案 2 :(得分:0)
您将数据设置为尚未初始化的recyclerView。 移动这个
recyclerView.setAdapter(adapter);
之后立即
到班级的顶部
的setContentView(R.layout.mycolleagues_layout);
答案 3 :(得分:0)
你可以使用.optString(“title”);用于空标题响应.getString(“title”);像上面那样
String val6 = jo_inside.optString("title");
这意味着它会检查更多check this link
的可选字符串标题