无法在Android中使用未命名的JSON数组填充AutocompleteTextView。

huangapple go评论120阅读模式
英文:

Unable to populate AutocompleteTextview with unnamed JSON Array in Android

问题

以下是翻译好的部分:

我一直在尝试使用 API 将自动完成文本视图(autocompletetextview)填充到 JSON 中,但由于 JSON API 没有一个数组名称,所以它无法正常工作。如果我尝试使用其他具有数组名称的 API,它就可以正常工作。

这是 API 的示例链接:link

我尝试过实现 此答案,但它并没有起作用。

这是 API 的结构。

这是我的方法。

  1. private void populateEduList() {
  2. List<String> responseList = new ArrayList<>();
  3. String url = "https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon";
  4. JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -> {
  5. try {
  6. JSONArray jsonArray = new JSONArray(response);
  7. for (int i = 0; i < jsonArray.length(); i++){
  8. JSONObject object = jsonArray.getJSONObject(i);
  9. responseList.add(object.getString("name"));
  10. ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
  11. mAutoCompEdu.setAdapter(adapter);
  12. }
  13. } catch (JSONException e) {
  14. e.printStackTrace();
  15. }
  16. }, error -> {
  17. // 处理错误
  18. });
  19. requestQueue.add(request);
  20. }

我一直在使用 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.

无法在Android中使用未命名的JSON数组填充AutocompleteTextView。

This is the structure of the API.

And this is my method.

  1. private void populateEduList() {
  2. List&lt;String&gt; responseList = new ArrayList&lt;&gt;();
  3. String url = &quot;https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon&quot;;
  4. JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -&gt; {
  5. try {
  6. JSONArray jsonArray = new JSONArray(response);
  7. for (int i = 0; i &lt; jsonArray.length(); i++){
  8. JSONObject object = jsonArray.getJSONObject(i);
  9. responseList.add(object.getString(&quot;name&quot;));
  10. ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
  11. mAutoCompEdu.setAdapter(adapter);
  12. }
  13. } catch (JSONException e) {
  14. e.printStackTrace();
  15. }
  16. }, error -&gt; {
  17. //do something for the error
  18. });
  19. requestQueue.add(request);
  20. }

I have been using the Volley Library to fetch the JSON and parse it.

答案1

得分: 0

以下是翻译好的部分:

这是答案,如果 JSON 数组没有 "Name",我们需要使用 StringRequest 而不是 JsonObjectRequest 来获取并在自动完成文本视图中显示 JSON。

更新的代码。

  1. private void populateEduList() {
  2. List<String> responseList = new ArrayList<>();
  3. String url = "https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon";
  4. StringRequest request = new StringRequest(Request.Method.GET, url, response -> {
  5. try {
  6. JSONArray jsonArray = new JSONArray(response);
  7. for (int i = 0; i < jsonArray.length(); i++){
  8. JSONObject object = jsonArray.getJSONObject(i);
  9. responseList.add(object.getString("name"));
  10. Log.d("hasjkd", object.getString("name"));
  11. ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
  12. mAutoCompEdu.setAdapter(adapter);
  13. }
  14. } catch (JSONException e) {
  15. e.printStackTrace();
  16. }
  17. }, error -> {
  18. // 处理错误的操作
  19. });
  20. requestQueue.add(request);
  21. }
英文:

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.

  1. private void populateEduList() {
  2. List&lt;String&gt; responseList = new ArrayList&lt;&gt;();
  3. String url = &quot;https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon&quot;;
  4. StringRequest request = new StringRequest(Request.Method.GET, url, response -&gt; {
  5. try {
  6. JSONArray jsonArray = new JSONArray(response);
  7. for (int i = 0; i &lt; jsonArray.length(); i++){
  8. JSONObject object = jsonArray.getJSONObject(i);
  9. responseList.add(object.getString(&quot;name&quot;));
  10. Log.d(&quot;hasjkd&quot;, object.getString(&quot;name&quot;));
  11. ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
  12. mAutoCompEdu.setAdapter(adapter);
  13. }
  14. } catch (JSONException e) {
  15. e.printStackTrace();
  16. }
  17. }, error -&gt; {
  18. //do something for the error
  19. });
  20. requestQueue.add(request);
  21. }

huangapple
  • 本文由 发表于 2020年10月5日 12:08:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64202342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定