【发布时间】: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