gpt4 book ai didi

java - 如何使用 Volley (TextView) 解析数组数据

转载 作者:行者123 更新时间:2023-12-01 18:35:45 27 4
gpt4 key购买 nike

package com.sp.demoapiheader;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

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);

StringRequest request = new StringRequest(Request.Method.GET, "http://datamall2.mytransport.sg/ltaodataservice/BusServices", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (!response.equals(null)) {
Log.e("Your Array Response", response);

} else {
Log.e("Your Array Response", "Data Null");
}
}

}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("error is ", "" + error);
}
}) {

//This is for Headers If You Needed
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=UTF-8");
params.put("AccountKey", "XXXX==");
return params;
}

//Pass Your Parameters here
/*@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("User", UserName);
params.put("Pass", PassWord);
return params;
}*/
};
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request);

}
}

我想将数据解析为 TextView 并以字符串/整数格式显示每个变量。从代码中可以看出,有一个带有按钮和 TextView 的简单 XML 文件。我是否在代码中的某个时刻实现了 JSONArray 或 JSONObject?目前,该代码仅允许在运行选项卡中进行响应。

最佳答案

可以实现json数组解析器,两种方式在textview中显示

1) Gson 转换器依赖

dependencies {
implementation 'com.google.code.gson:gson:2.8.6'
}


@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d(TAG, "onResponse: " + response.body());
List<MenuResponseModel> listArr = new ArrayList<>();
Gson gson = new Gson();
StringBuffer stringBuffer = new StringBuffer();
try {
MenuResponseModel menuResponseModel = gson.fromJson(response.body().string(), MenuResponseModel.class);
if (menuResponseModel.getStaus().equals("1")) {
listArr.addAll(menuResponseModel.getData());
for (int i = 0; i < listArr.size(); i++) {

Integer price = Integer.parseInt(foodOrderList.get(i).getFood_quantity()) * Integer.parseInt(foodOrderList.get(i).getFood_price());
stringBuffer.append("\n\nFood Name: " + foodOrderList.get(i).getFood_name() + "\n Food Quantity : " + foodOrderList.get(i).getFood_quantity() + "\n Food Price :" + price.toString());
tv_order_details.setText(stringBuffer.toString());
}

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

2) JSONArray 类

try {
JSONObject jsonObject = new JSONObject(response.body().string());
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject js = jsonArray.getJSONObject(i);
stringBuffer.append("Name :"+js.getString("customer_name")+" - Table No: "+js.getString("table_id")+js.getString("seats"));
} mTextViewResult.setText(stringBuffer.toString());
} catch (Exception e) {
e.printStackTrace();
}

关于java - 如何使用 Volley (TextView) 解析数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60051070/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com