【问题标题】:Spinner not populating with json dataSpinner 未填充 json 数据
【发布时间】:2019-08-13 08:59:21
【问题描述】:

我正在构建一个应用程序来将图像上传到我的公司服务器。 我遇到的问题我必须让 2 个微调器一个来选择上传类别,另一个来选择一个客户端,我现在正处于测试阶段,问题是我的 Spinner 没有在所有 json 数据中填充 JSON 数据如果这很重要,则存储在 ASP 文件中 我只编写了大约 2 周的代码,因此我们将不胜感激。

Json 数据

{ "success": 1, "Name": [ { "Country": "India" }, { "Country": "US" }, { "Country": "UK" }, { "Country": "Australia" }, { "Country": "Canada " } ] }

MainActivity

public class MainActivity extends AppCompatActivity {
    Spinner spinner;
    String URL="https://www.smartpractice.co.za/app-categories.asp";
    ArrayList<String> CountryName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CountryName=new ArrayList<>();
        spinner=(Spinner)findViewById(R.id.country_Name);
        loadSpinnerData(URL);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String country=   spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString();
                Toast.makeText(getApplicationContext(),country,Toast.LENGTH_LONG).show();
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                // DO Nothing here
            }
        });
    }
    private void loadSpinnerData(String url) {
        RequestQueue requestQueue=Volley.newRequestQueue(getApplicationContext());
        StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try{
                    JSONObject jsonObject=new JSONObject(response);
                    if(jsonObject.getInt("success")==1){
                        JSONArray jsonArray=jsonObject.getJSONArray("Name");
                        for(int i=0;i<jsonArray.length();i++){
                            JSONObject jsonObject1=jsonArray.getJSONObject(i);
                            String country=jsonObject1.getString("Country");
                            CountryName.add(country);
                        }
                    }
                    spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, CountryName));
                }catch (JSONException e){e.printStackTrace();}
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });
        int socketTimeout = 30000;
        RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
        stringRequest.setRetryPolicy(policy);
        requestQueue.add(stringRequest);
    }
}

【问题讨论】:

  • 在 forloop 中做一些事情,比如 Log.d("Country Name",country);并检查您是否收到日志
  • @MayurCoceptioni 我不知道你在问什么我知道如何把日志代码放在哪里,但我不知道在哪里阅读所说的日志
  • for(int i=0;i

标签: android json spinner string-parsing


【解决方案1】:

目前,您似乎没有正确地将 JSON 解析为对象。 如果您在 catch e.printStackTrace(); 上添加断点,您可以看到它。和调试。

我的建议是使用 Gson 将数据解析为对象(这样您出错的机会就会降低)。

见参考:https://medium.com/@ankit.sinhal/parsing-json-with-gson-library-184d94a04dec

我还建议对网络请求使用 Retrofit https://medium.com/@prakash_pun/retrofit-a-simple-android-tutorial-48437e4e5a23

【讨论】:

  • 谢谢,我正忙着检查,会尽快回复你
  • 嘿,谢谢您的帮助,问题是与代码完全无关的问题,这是我使用 json 数据初始化的 ASP 文件中的一些东西,该文件在我之前有电子邮件,密码在 ASP json 数据并将其移动到我的 json 数据下方修复了它,无论如何谢谢你
【解决方案2】:

JSONObject jsonObject = new JSONObject(response); 是错误(转换错误)

因为在您的响应中 ClientID、用户名和密码都是字符串,所以它给出了错误。当您使用 try-catch 方法编写时,您的应用不会崩溃。

=> 在响应 JSONObject 之前,您只需执行 String array = response.substring(47);。 (因此它会删除所有上述值并从具有值的数组开始)

=> 然后替换对数组的响应。它肯定会起作用。

示例:-

StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i(TAG, "onResponse: " + response);
            String array = response.substring(47);
            Log.i(TAG, "onResponse: array " + array);
            try {
                JSONObject jsonObject = new JSONObject(array);
                Log.i(TAG, "onResponse: success :- " + jsonObject.getInt("success"));
                if (jsonObject.getInt("success") == 1) {
                    Log.i(TAG, "onResponse: Name :- " + jsonObject.getJSONArray("Name"));
                    JSONArray jsonArray = jsonObject.getJSONArray("Name");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                        Log.i(TAG, "onResponse: Country :- " + jsonObject1.getString("Country"));
                        String country = jsonObject1.getString("Country");
                        CountryName.add(country);
                    }
                }
                spinner.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, CountryName));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
     }, new Response.ErrorListener() {
     @Override
     public void onErrorResponse(VolleyError error) {
         error.printStackTrace();
     }
});

在您的请求中替换上述请求

【讨论】:

  • 谢谢老兄,你的代码帮助我更好地理解了一点,但解决方案完全不同,在我的包含 json 数据的 ASP 文件中,它包含用于 clientID、密码和电子邮件的 ASP 代码,以及我只需要把它移到我的 json 数据下面就可以了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多