gpt4 book ai didi

java - 在 Android 中从 JSON 填充微调器

转载 作者:行者123 更新时间:2023-12-01 23:26:32 25 4
gpt4 key购买 nike

我有这样的 JSON 数据:

[{"id":"1",
"client_name":"Glasco Smith Klin",
"campaign_name":"Health Medicine Survay",
"question":"MODE OF COMPLAINT CAN BE GIVEN THROUGH __________________",
"option_A":"WRITTEN",
"option_B":"ORAL",
"option_C":"PHONE CALL",
"option_D":"ALL OF THE ABOVE",
"q_prefix":"GSKHMSQ"},

{"id":"2",
"client_name":"Glasco Smith Klin",
"campaign_name":"Health Medicine Survay",
"question":"ADDING NEW FIR CAN BE DONE FROM WHICH ROLE",
"option_A":"IO",
"option_B":"TRAFFIC",
"option_C":"SHO-CRIME",
"option_D":"ADMINISTRATOR",
"q_prefix":"GSKHMJD"}]

我想将所有 q_prefix 列检索到微调器中。但在我的微调器中没有显示数据。当我运行我的应用程序时,我收到错误:“无法启动 Activity”并且在这一行出现 Java.lang.NullPointerException:jsonArray = jSon.getJSONArray(TAG_CODE);.

这是我的代码:

public class SpinDemo extends Activity {

private static String strUrl = "http://192.168.1.61/jyoti/android_app/all_questions.php";
private static String TAG_ID = "id";
private static String TAG_CODE = "q_prefix";
JSONArray jsonArray = null;
Spinner codeSpinner, spinner2;

@SuppressWarnings("unused")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


codeSpinner = (Spinner) findViewById(R.id.spinner2);

ArrayList<String> codeList = new ArrayList<String>();

// creating instance of JSONPrser
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject jSon = jParser.getJSONFromUrl(strUrl);
System.out.println("Hello ********************");

try {
// Getting Array of Code
jsonArray = jSon.getJSONArray(TAG_CODE);
final String[] items = new String[jsonArray.length()];
// looping through All Contacts
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject c = jsonArray.getJSONObject(i);

// Storing each json item in variable
String strCode = c.getString(TAG_CODE);
items[i] = c.getString(TAG_CODE);
System.out.println("Hello events " + items);
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
codeSpinner.setAdapter(adapter);
} catch (JSONException je) {
je.printStackTrace();

}

}

}

最佳答案

您可以检查以下代码,在 Android 应用程序中使用 spinner 解析 json 数据:

public class ParseJsonAndroidExample extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parse_json_android_example);


final Spinner output = (Spinner) findViewById(R.id.output);
final Button bparsejson = (Button) findViewById(R.id.bparsejson);
String OutputData ;
ArrayList<String> outputDataList = new ArrayList<String>();


/************ Static JSON data ***********/
final String strJson = "{ "Android" :[{"id":"1",
"client_name":"Glasco Smith Klin",
"campaign_name":"Health Medicine Survay",
"question":"MODE OF COMPLAINT CAN BE GIVEN THROUGH __________________",
"option_A":"WRITTEN",
"option_B":"ORAL",
"option_C":"PHONE CALL",
"option_D":"ALL OF THE ABOVE",
"q_prefix":"GSKHMSQ"},

{"id":"2",
"client_name":"Glasco Smith Klin",
"campaign_name":"Health Medicine Survay",
"question":"ADDING NEW FIR CAN BE DONE FROM WHICH ROLE",
"option_A":"IO",
"option_B":"TRAFFIC",
"option_C":"SHO-CRIME",
"option_D":"ADMINISTRATOR",
"q_prefix":"GSKHMJD"}] }";


/******** Listener for button click ********/
bparsejson.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

String OutputData = "";
JSONObject jsonResponse;

try {

/****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
jsonResponse = new JSONObject(strJson);

/***** Returns the value mapped by name if it exists and is a JSONArray. ***/
/******* Returns null otherwise. *******/
JSONArray jsonMainNode = jsonResponse.optJSONArray("Android");

/*********** Process each JSON Node ************/

int lengthJsonArr = jsonMainNode.length();

for(int i=0; i < lengthJsonArr; i++)
{
/****** Get Object for each JSON node.***********/
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

/******* Fetch node values **********/
int id = Integer.parseInt(jsonChildNode.optString("id").toString());
String client_name = jsonChildNode.optString("client_name").toString();
String campaign_name= jsonChildNode.optString("campaign_name").toString();
String question= jsonChildNode.optString("question").toString();
String option_A= jsonChildNode.optString("option_A").toString();

String option_B= jsonChildNode.optString("option_B").toString();
String option_C= jsonChildNode.optString("option_C").toString();
String option_D= jsonChildNode.optString("option_D").toString();
String q_prefix= jsonChildNode.optString("q_prefix").toString();


OutputData += "Node : \n\n "+ id +" | "
+ client_name+" | "
+ campaign_name+"
+ question+" | "
+ option_A+" | "
+ option_B+" | "
+ option_C+" | "
+ option_D+" | "
+ q_prefix+" | " +"\n\n ";




}
outputDataList.add(OutputData);


/************ Show Output on Spinner **********/

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item ,outputDataList );
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
codeSpinner.setAdapter(adapter);



} catch (JSONException e) {

e.printStackTrace();
}

}
});
}
}

关于java - 在 Android 中从 JSON 填充微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19922058/

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