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

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

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.

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

This is the structure of the API.

And this is my method.

private void populateEduList() {
    List&lt;String&gt; responseList = new ArrayList&lt;&gt;();
    String url = &quot;https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon&quot;;
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, response -&gt; {

        try {
            JSONArray jsonArray = new JSONArray(response);
            for (int i = 0; i &lt; jsonArray.length(); i++){
                JSONObject object = jsonArray.getJSONObject(i);
                responseList.add(object.getString(&quot;name&quot;));
                ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
                mAutoCompEdu.setAdapter(adapter);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }, error -&gt; {
        //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&lt;String&gt; responseList = new ArrayList&lt;&gt;();
        String url = &quot;https://autocomplete.clearbit.com/v1/companies/suggest?query=amazon&quot;;
        StringRequest request = new StringRequest(Request.Method.GET, url, response -&gt; {
            try {
                JSONArray jsonArray = new JSONArray(response);
                for (int i = 0; i &lt; jsonArray.length(); i++){
                    JSONObject object = jsonArray.getJSONObject(i);
                    responseList.add(object.getString(&quot;name&quot;));
                    Log.d(&quot;hasjkd&quot;, object.getString(&quot;name&quot;));
                    ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, responseList);
                    mAutoCompEdu.setAdapter(adapter);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }, error -&gt; {
            //do something for the error
        });

        requestQueue.add(request);
    }

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:

确定