如何在android中从数组中获取Json数据?

例如,我有JSON格式的数组。

[
  [17,"Tracker-9210671","+918010804433",true,84,"869867039210671"],
  [18,"Gtrack_Ciaz","+918010804433",true,85,"869867039215308"], 
  [20,"Gtrack_Zeep_Trunk","+918010804433",true,86,"869867039210333"], 
  [19,"Gtrack_Zeep_Dashboard","+918010804433",true,87,"869867039209095"]
]

And I want to fetch this Json data in my android , How to get this.I am not able to Fetch this JsonArray Can anyone tell what should be the code for this.

我所做的是(使用volley)。

”’

 public class MainActivity extends AppCompatActivity {

 private TextView mTextViewResult;
 private RequestQueue mQueue;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  mTextViewResult = findViewById(R.id.text_view_result);
  Button buttonParse = findViewById(R.id.button_parse);

  mQueue = Volley.newRequestQueue(this);

  buttonParse.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    jsonParse();
   }
  });
 }
 private void jsonParse() {
  String url = "url";
  JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
   new Response.Listener < JSONArray > () {
    @Override
    public void onResponse(JSONArray response) { // Called when request successfull.


     try {
      for (int i = 0; i < response.length(); i++) {

       JSONArray jsonArray = response.getJSONArray(i);
       for (int j = 0; j < jsonArray.length(); j++) {
        JSONArray value = jsonArray.getJSONArray(j);

        int DB_ID = value.getInt(0);
        String Devie_Name = value.getString(1);
        String Mobile_Number = value.getString(2);
        Boolean Status = value.getBoolean(3);
        int Stock_Id = value.getInt(4);
        String Device_Id = value.getString(5);
        mTextViewResult.append(String.valueOf(DB_ID) + " , " + Devie_Name + " , " + Mobile_Number + " , " + String.valueOf(Status) + " , " + String.valueOf(Stock_Id) + " , " + Device_Id + "\n\n");

       }
      }

     } catch (JSONException e) {
      e.printStackTrace();
     }

    }


   }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) { // called when request not success.
     error.printStackTrace();
    }
   });

  mQueue.add(request);
 }
}

”’

我得到的错误。

org.json.JSONException: 类型为java.lang.Integer的值17在0处不能被转换为JSONArray。

解决方案:

只需将此添加到您的 Android Manifest 文件,因为从Android 9操作系统开始,我们需要添加这个属性。因为从Android 9操作系统开始,我们需要添加这个属性。android:usesCleartextTraffic="true".

因为从 Android 9 (API level 28)默认情况下,清文本支持被禁用。

 <application
        android:usesCleartextTraffic="true">

给TA打赏
共{{data.count}}人
人已打赏
未分类

从肥皂反应中提取价值

2022-9-24 22:00:36

未分类

EventGrid订阅权限

2022-9-24 22:00:38

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索