英文:
Unable to populate AutocompleteTextview with unnamed JSON Array in Android
问题
以下是翻译好的部分:
我一直在尝试使用 API 将自动完成文本视图(autocompletetextview)填充到 JSON 中,但由于 JSON API 没有一个数组名称,所以它无法正常工作。如果我尝试使用其他具有数组名称的 API,它就可以正常工作。
这是 API 的示例链接:link。
我尝试过实现 此答案,但它并没有起作用。
这是 API 的结构。
这是我的方法。
private void populateEduList() {
List<String> responseList = new ArrayList<>();
String url = "https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -> {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
responseList.add(object.getString("name"));
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
mAutoCompEdu.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
// 处理错误
});
requestQueue.add(request);
}
我一直在使用 Volley 库来获取 JSON 并解析它。
英文:
I have been trying to populate the autocompletetextview with JSON using an API but it's not working as the JSON API does not have an Array name. If I try to use some other API that does have an array name it's working fine.
This is the example link for the API.
I have tried implementing this answer but it's not working.
This is the structure of the API.
And this is my method.
private void populateEduList() {
List<String> responseList = new ArrayList<>();
String url = "https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -> {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
responseList.add(object.getString("name"));
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
mAutoCompEdu.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
//do something for the error
});
requestQueue.add(request);
}
I have been using the Volley Library to fetch the JSON and parse it.
答案1
得分: 0
以下是翻译好的部分:
这是答案,如果 JSON 数组没有 "Name",我们需要使用 StringRequest
而不是 JsonObjectRequest
来获取并在自动完成文本视图中显示 JSON。
更新的代码。
private void populateEduList() {
List<String> responseList = new ArrayList<>();
String url = "https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon";
StringRequest request = new StringRequest(Request.Method.GET, url, response -> {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
responseList.add(object.getString("name"));
Log.d("hasjkd", object.getString("name"));
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
mAutoCompEdu.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
// 处理错误的操作
});
requestQueue.add(request);
}
英文:
This is the answer, to fetch and display JSON in an autocompleteTextView if the JSON Array does not have a Name, we have to use StringRequest
instead of JsonObjectRequest
.
Updated Code.
private void populateEduList() {
List<String> responseList = new ArrayList<>();
String url = "https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon";
StringRequest request = new StringRequest(Request.Method.GET, url, response -> {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++){
JSONObject object = jsonArray.getJSONObject(i);
responseList.add(object.getString("name"));
Log.d("hasjkd", object.getString("name"));
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
mAutoCompEdu.setAdapter(adapter);
}
} catch (JSONException e) {
e.printStackTrace();
}
}, error -> {
//do something for the error
});
requestQueue.add(request);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论