android - 使用 Volley 获取 JSON 文件的问题
问题描述
再会,
我在JSON
使用 Android Studio 获取文件时遇到问题。如何JSONObject
在下面的代码中调用特定的示例我想知道输入的电话号码是否等于该JSONObject
号码。我正在尝试JSONArray
在不同的字符串上单独读取和存储。
每次我试图obj.getString("phonenum")
在循环内调用时,我得到的唯一值就是最后一个值。
我的代码如下。
JSON 文件。
获取信息.php
[
{
"id": 1,
"name": "Angelo Dayao",
"email": "angelo@gmail.com",
"phonenum": "639201234567"
},
{
"id": 2,
"name": "Angelo Dayao",
"email": "angelogg@g.com",
"phonenum": "639161234567"
},
{
"id": 3,
"name": "Anna Chelzia",
"email": "anna@g.com",
"phonenum": "639771234567"
}
]
JAVA文件代码
public void GetData(){
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for(int i = 0; i < jsonArray.length(); i++){
try {
JSONObject obj = null;
obj = jsonArray.getJSONObject(i);
if(obj.getString("phonenum") == phonenumber){
Toast.makeText(getApplicationContext(), obj.getString("phonenum"), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//displaying the error in toast if occurrs
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
解决方案
推荐阅读
- javascript - Google Maps GeoJSON 功能触发点击事件
- azure - 如何使用 Powershell 创建具有 HDD 操作系统磁盘的新 VM
- typescript - 导入的ES6模块在包中处理后不执行
- mongodb - Mongoose findOneAndUpdate 文档内数组中的数组
- flutter - 即使在插入值后 TextFormField 仍显示错误消息(使用 Form Flutter)
- postgresql - GROUP BY 和 DISTINCT ON 对表和视图的工作方式不同
- android - 未使用的消息计数在活动通道中返回 null
- typescript - 使用 * glob 理解 TypeScript 中的“baseUrl”和“路径”
- python - Tkinter 和 CherryPy 并发
- list - 对列表的多个子列表进行排序以创建新列表,Dart