我遇到了json的问题
[
{
"id": "1",
"userid": "1831",
"authorname": "WhatsApp",
"message": "There are only two times that I want to be with you: Now and Forever.",
"likes": "2",
"type": "romantic",
"yourlikes": "0"
},
{
"id": "2",
"userid": "1831",
"authorname": "WhatsApp",
"message": "My six word love story: “I can’t imagine life without you.”",
"likes": "1",
"type": "romantic",
"yourlikes": "0"
},
{
"id": "3",
"userid": "1831",
"authorname": "WhatsApp",
"message": "You have no idea how much my heart races when I see you.",
"likes": "3",
"type": "romantic",
"yourlikes": "1"
},
{
"id": "4",
"userid": "1831",
"authorname": "WhatsApp",
"message": "Since the time I’ve met you, I cry a little less, laugh a little harder and smile all the more, just because I have you, my life is a better place.",
"likes": "0",
"type": "romantic",
"yourlikes": "0"
}
]
您可以看到以下内容:
"yourlikes": "1"
当我按消息显示时,它是0
CODE
public class ShowMessages extends AppCompatActivity {
String type;
ArrayList<Item_Messages> item_messages = new ArrayList<>();
RecyclerView rv_message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_messages);
Intent intent = getIntent();
type = intent.getStringExtra("type");
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, Information.URL_Messages + Information.userid + "&type=" + type, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
for(int x = 0 ; x < response.length() ; x++)
{
item_messages.add(new Item_Messages(response.getJSONObject(x).getString("id"),response.getJSONObject(x).getString("userid"),response.getJSONObject(x).getString("authorname"),response.getJSONObject(x).getString("message"),response.getJSONObject(x).getString("likes"),response.getJSONObject(x).getString("type"),response.getJSONObject(x).getString("yourlikes")));
}
Toast.makeText(ShowMessages.this, "" + response.getJSONObject(2).getString("yourlikes"), Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
rv_message = (RecyclerView)findViewById(R.id.rv_message);
rv_message.setLayoutManager(new LinearLayoutManager(ShowMessages.this,LinearLayoutManager.VERTICAL,false));
Adapter_Messages adapterview = new Adapter_Messages(ShowMessages.this, item_messages);
rv_message.setAdapter(adapterview);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(jsonArrayRequest);
}
}
自定义数组列表
import java.util.ArrayList;
public class Item_Messages {
String id,userid,authorname,message,likes,type,yourlikes;
public Item_Messages(String id, String userid, String authorname, String message, String likes, String type, String yourlikes) {
this.id = id;
this.userid = userid;
this.authorname = authorname;
this.message = message;
this.likes = likes;
this.type = type;
this.yourlikes = yourlikes;
}
}
为什么在结果中显示0
而非1
的任何想法?
我搜索了很多,没有找到任何与此问题相关的问题问题是否在php
文件中?除了以下所有数据都正确显示:
"yourlikes": "1"
or
"yourlikes": "0"
请你试着帮我,谢谢你。