我有这样的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
列检索到微调器中。但是在我的微调器中没有显示数据。当我运行我的应用程序时,我收到一个错误:"无法启动活动"和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();
}
}
}
答案 0 :(得分:1)
请尝试使用AsyncTask
网络操作
“无法启动活动”和j Java.lang.NullPointerException
sonArray = jSon.getJSONArray(TAG_CODE);
首先使用此代码进行解析
jsonArray = new JSONArray(jSon.toString());
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
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,items);
答案 1 :(得分:0)
JSONObject jSon = jParser.getJSONFromUrl(strUrl);
your response is JsonArray not JsonObject
if Response starts with [ it is an jsonarray
if starts with { it is an jsonObject.
使用
public static int SDK_INT = android.os.Build.VERSION.SDK_INT;
if (SDK_INT >= 10) {
ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
}
解析Json之前
或者使用AsyncTask来获取响应
http://developer.android.com/reference/android/os/AsyncTask.html
答案 2 :(得分:0)
您没有将items
数组传递给ArrayAdapter
构造函数,请执行以下操作:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,items);
而不是从主线程使用AsyncTask进行web服务请求,以便在后台执行操作,并在操作完成时更新UI元素
答案 3 :(得分:0)
问题和解决方案:
检查你的logcat,它应该有NetworkOnMainThreadException,原因是你在主线程上直接调用web API,从Honeycomb开始它不可能直接在Main UI Thread上实现长时间运行的任务。
解决方案是实现线程机制,你可以在Android中实现AsyncTask,这在Android中被称为无痛线程。
JSON解析存在问题,您需要正确学习和理解JSON Parsing。
问题示例:
JSONObject jSon = jParser.getJSONFromUrl(strUrl);
System.out.println("Hello ********************");
try {
// Getting Array of Code
jsonArray = jSon.getJSONArray(TAG_CODE);
.....
.....
通过查看相关问题的响应,我可以说你直接得到了JSONArray而不是JSONObject。
JSON解析中的第二个错误是你正在尝试获取不需要的JSONArray(TAG_CODE),因为你已经在响应中使用JSONArray,你可以遍历对象并可以获取你想要的任何值。