Java 将JSON文件中的字符串数组解析为全局变量,但使用它时,数组内容为null
我有一个带有不同字符串的JSON文件,我想创建一个altert对话框,但我的字符串数组不起作用 我创建了一个私有字符串类别。在getJobArray方法中,我使用for循环将字符串值解析为类别字符串。在那里,LogCat输出告诉我数组被字符串值填充。但是,当我在onCreateDialog中使用category字符串数组时,它告诉我这些值都是null。我甚至无法将长度打印到LogCatJava 将JSON文件中的字符串数组解析为全局变量,但使用它时,数组内容为null,java,android,json,checkbox,android-alertdialog,Java,Android,Json,Checkbox,Android Alertdialog,我有一个带有不同字符串的JSON文件,我想创建一个altert对话框,但我的字符串数组不起作用 我创建了一个私有字符串类别。在getJobArray方法中,我使用for循环将字符串值解析为类别字符串。在那里,LogCat输出告诉我数组被字符串值填充。但是,当我在onCreateDialog中使用category字符串数组时,它告诉我这些值都是null。我甚至无法将长度打印到LogCat public class MultipleChoiceDialogFragment extends Dialo
public class MultipleChoiceDialogFragment extends DialogFragment {
private RequestQueue mRQ;
private Context mContext;
private RequestQueue mRequestQ;
private static String TAG = "xd";
private String[] category;
public interface onMultiChoiceListener{
void onPositiveClicked(String[] list,ArrayList<String> selectedItemList);
void onNegativeButtonClicked();
}
onMultiChoiceListener mListener;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mRequestQ = Volley.newRequestQueue(context);
getJobArray();
try {
mListener = (onMultiChoiceListener) context;
}catch (Exception e){
throw new ClassCastException(getActivity() + "onMultiChoice not working error");
}
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ArrayList<String> selectedItemList = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//not working don't understand why... here category is null
final String[] list = category;
builder.setTitle("Select one")
.setMultiChoiceItems(list, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
if(b){
selectedItemList.add(list[i]);
}else{
selectedItemList.remove(list[i]);
}
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onPositiveClicked(list,selectedItemList);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onNegativeButtonClicked();
}
});
return builder.create();
}
private void getJobArray(){
//todo here I can get array from json array
String url = "http://api_staging.jab.poweredby.cnddts.at/mobile/metadata/categories?portal=1";
JsonObjectRequest request = new JsonObjectRequest(com.android.volley.Request.Method.GET, url, null,
new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("categories");
category = new String[jsonArray.length()];
Log.d(TAG, "onResponse: is getting array ready right now");
Log.d(TAG, "onResponse: jsonArray length" + jsonArray.length());
for(int i = 0; i < jsonArray.length(); i++){
JSONObject categories = jsonArray.getJSONObject(i);
category[i] = categories.getString("name");
Log.d(TAG, "onResponse: array input " + category[i]);
}
} catch (JSONException e) {
e.printStackTrace();
Log.d(TAG, "onResponse: catch caught...");
}
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQ.add(request);
}
}
这是您的示例,但这不是有效的JSON格式,您缺少“categories”数组的右方括号
以下是示例的正确JSON格式:
{
"categories": [
{
"uid": "1",
"name": "Arbeitsrecht/Sozialrecht"
},
{
"uid": "2",
"name": "Bankrecht/Kapitalmarktrecht"
}
]
}
尝试从第二个字符串创建JSONObject。我可以看出这是因为API调用了
onAttach(上下文)
方法。因此,当您调用API时,类别尚未初始化,onCreateDialog()
将具有空值,因为API将在主线程外调用,并且主线程不会等待API响应
顺便说一下,在调用对话框时始终调用API不是一个好主意。API调用应该以最少的调用次数进行优化,以获得更好的应用程序性能。因此,一个好的解决方案是在Activity或Fragement中调用对话框之前调用API。当调用对话框时,直接使用响应数据中的json值 你能展示一下使用json的例子吗?@KunLun是的,我的json有一小部分:{“categories”:[{“uid”:“1”,“name”:“Arbeitsrecht/Sozialrecht”},{“uid”:“2”,“name”:“Bankrecht/Kapitalmarktrecht”@昆仑这里是一个LogCat输出,当通过数组循环时,它会正确生成…2019-08-30 11:28:11.991 14065-14065/com.example.candidatesApp D/xd:onResponse:jsonArray length30数组输入Gesellschaftsrecht/Corporate/M&A数组输入Immaterialgüterrecht/wetbewerbsrecht似乎是一个期望异步工作的经典案例立即完成。您应该先获取数据,等待数据被获取,然后再显示。@TimCastelijns如何才能首先获取?我只在最后显示了json文件的一张图片,它显然已关闭。如果它满足您的要求,我希望这将是认可的答案。
{
"categories": [
{
"uid": "1",
"name": "Arbeitsrecht/Sozialrecht"
},{
"uid": "2",
"name": "Bankrecht/Kapitalmarktrecht"
}
}
{
"categories": [
{
"uid": "1",
"name": "Arbeitsrecht/Sozialrecht"
},
{
"uid": "2",
"name": "Bankrecht/Kapitalmarktrecht"
}
]
}