微调器未填充 json 数据
Spinner not populating with json data
我正在构建一个应用程序来将图像上传到我公司的服务器。
我有 运行 的问题我必须让 2 个旋转器一个到 select 一个上传类别,另一个到 select 一个客户端,我现在正处于测试阶段,问题是我的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);
}
}
目前您似乎没有正确解析您的 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
JSONObject jsonObject = new JSONObject(response);是错误(转换错误)
因为在你的响应中ClientID,Username和Pwd都是一个字符串所以它给出了错误。当您使用 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();
}
});
在你的请求中替换上面的请求
我正在构建一个应用程序来将图像上传到我公司的服务器。 我有 运行 的问题我必须让 2 个旋转器一个到 select 一个上传类别,另一个到 select 一个客户端,我现在正处于测试阶段,问题是我的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);
}
}
目前您似乎没有正确解析您的 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
JSONObject jsonObject = new JSONObject(response);是错误(转换错误)
因为在你的响应中ClientID,Username和Pwd都是一个字符串所以它给出了错误。当您使用 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();
}
});
在你的请求中替换上面的请求