尝试使用GSON将JSON解析为spinner android时出错

时间:2017-09-20 11:40:57

标签: java android json gson

当我使用库GSON将JSON解析到我的spinner android时出现了一些错误。错误是:

  

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第1行路径为STRING

当我点击log cat android studio:

代码行spinnerData = Arrays.asList(mGson.fromJson(response, Set_jadwal[].class));

的错误

这是我的完整源代码:

public class jadwal_Activity extends AppCompatActivity {

private String DATA_URL_KELAS = "http://smpn8pky.rf.gd/Json_kelas.php";
public static final String DATA_URL_JADWAL = "http://smpn8pky.rf.gd/Json_jadwal.php";
public List<Set_jadwal> spinnerData;
private RequestQueue queue;
Spinner spinner;

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

    queue = Volley.newRequestQueue(this);
    requestJsonObject();
}

private void requestJsonObject() {
    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.GET, DATA_URL_KELAS, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            GsonBuilder builder = new GsonBuilder();
            Gson mGson = builder.create();
            spinnerData = Arrays.asList(mGson.fromJson(response, Set_jadwal[].class));
            //display first question to the user
            if(null != spinnerData){
                spinner = (Spinner) findViewById(R.id.spiner_kelas);
                assert spinner != null;
                spinner.setVisibility(View.VISIBLE);
                SpinnerAdapter spinnerAdapter = new SpinnerAdapter(jadwal_Activity.this, spinnerData);
                spinner.setAdapter(spinnerAdapter);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    queue.add(stringRequest);
}

这是我的json回复JSON RESPON

的链接

2 个答案:

答案 0 :(得分:0)

当我尝试使用Postman(Google Chrome插件)从您的网址GET获取JSON时,回复告诉我:

Error

这意味着,除非网站禁止其要求不允许任何不具有javascript的传入请求,否则您将无法获取json数据。网站通常这样做的原因是为了防止机器人发送垃圾邮件。

所以要澄清一下:这不是你的代码的问题,而是(你的/)webhost。

答案 1 :(得分:0)

在您的示例中,您尝试将JSON数组转换为Set_jadwal的数组,然后使用Arrays.asList将其转换为列表。我认为这是多余的。

使用Gson TypeToken直接转换为列表

String response = "[{\"kd_kelas\":\"KLS-1\",\"kelas\":\"VII-1\"},{\"kd_kelas\":\"KLS-2\",\"kelas\":\"VII-2\"},{\"kd_kelas\":\"KLS-3\",\"kelas\":\"VII-3\"},{\"kd_kelas\":\"KLS-4\",\"kelas\":\"VII-4\"},{\"kd_kelas\":\"KLS-5\",\"kelas\":\"VII-5\"},{\"kd_kelas\":\"KLS-6\",\"kelas\":\"VII-6\"},{\"kd_kelas\":\"KLS-7\",\"kelas\":\"VII-7\"},{\"kd_kelas\":\"KLS-8\",\"kelas\":\"VII-8\"},{\"kd_kelas\":\"KLS-9\",\"kelas\":\"VII-9\"},{\"kd_kelas\":\"KLS-10\",\"kelas\":\"VII-10\"},{\"kd_kelas\":\"KLS-11\",\"kelas\":\"VII-11\"},{\"kd_kelas\":\"KLS-12\",\"kelas\":\"VIII-1\"},{\"kd_kelas\":\"KLS-13\",\"kelas\":\"VIII-2\"},{\"kd_kelas\":\"KLS-14\",\"kelas\":\"VIII-3\"},{\"kd_kelas\":\"KLS-15\",\"kelas\":\"VIII-4\"},{\"kd_kelas\":\"KLS-16\",\"kelas\":\"VIII-5\"},{\"kd_kelas\":\"KLS-17\",\"kelas\":\"VIII-6\"},{\"kd_kelas\":\"KLS-18\",\"kelas\":\"VIII-7\"},{\"kd_kelas\":\"KLS-19\",\"kelas\":\"VIII-8\"},{\"kd_kelas\":\"KLS-20\",\"kelas\":\"VIII-9\"},{\"kd_kelas\":\"KLS-21\",\"kelas\":\"VIII-10\"},{\"kd_kelas\":\"KLS-22\",\"kelas\":\"VIII-11\"},{\"kd_kelas\":\"KLS-23\",\"kelas\":\"IX-1\"},{\"kd_kelas\":\"KLS-24\",\"kelas\":\"IX-2\"},{\"kd_kelas\":\"KLS-25\",\"kelas\":\"IX-3\"},{\"kd_kelas\":\"KLS-26\",\"kelas\":\"IX-4\"},{\"kd_kelas\":\"KLS-27\",\"kelas\":\"IX-5\"},{\"kd_kelas\":\"KLS-28\",\"kelas\":\"IX-6\"},{\"kd_kelas\":\"KLS-29\",\"kelas\":\"IX-7\"},{\"kd_kelas\":\"KLS-30\",\"kelas\":\"IX-8\"},{\"kd_kelas\":\"KLS-31\",\"kelas\":\"IX-9\"},{\"kd_kelas\":\"KLS-32\",\"kelas\":\"IX-10\"},{\"kd_kelas\":\"KLS-33\",\"kelas\":\"IX-11\"}]";

List<Set_jadwal> result = new Gson().fromJson(response, 
                new TypeToken<List<Set_jadwal>>() {}.getType());

其中Set_jadwal是:

public class Set_jadwal {
    private String kd_kelas;
    private String kelas;
}

作为旁注,使用Set_jadwal作为班级名称是不好的做法;)