gpt4 book ai didi

android - 微调器未填充 json 数据

转载 作者:行者123 更新时间:2023-11-29 14:57:56 25 4
gpt4 key购买 nike

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

Json 数据

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

主 Activity

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);
}
}

最佳答案

目前您似乎没有正确地将 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

关于android - 微调器未填充 json 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57474352/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com