I get the answer from the server like this:

[{"time": "09:00"}, {"time": "09:01"}, {"time": "09:02"}, {"time": "09:03"}, { "time": "09:04"}, {"time": "09:05"}, {"time": "09:06"}, {"time": "09:07"}, {"time ":" 09:08 "}, {" time ":" 09:09 "}, {" time ":" 09:10 "}, {" time ":" 09:11 "}]

I try to push all this stuff into an ordinary Spinner, but it doesn’t work.
Tell me please, good people, how it is done correctly.

That's what happened with me:

public class GetTime extends AsyncTask<Void, Void, Void> { public Context context; public GetTime(Context context) { this.context = context; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... arg0) { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(urlTime); try { httpResponseTime = httpClient.execute(httpPost); StringHolderTime = EntityUtils.toString(httpResponseTime.getEntity(), "UTF-8"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { JSONArray jsonArray = new JSONArray(StringHolderTime); Log.i("MyLOG", "GetTime.doInBackground(): " + jsonArray.length()); List<String> list = new ArrayList<String>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String tim = jsonObject.getString("time"); list.add(tim); } Log.i("MyLOG", "GetTime.doInBackground(): " + list); ArrayAdapter<String> adapter = new ArrayAdapter<String>(RecordActivity.this, android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; } protected void onPostExecute(Void result) { progressBar.setVisibility(View.GONE); } } 

I / MyLOG: GetTime.doInBackground (): 12
I / MyLOG: GetTime.doInBackground (): [09:01, 09:02, 09:03, 09:04, 09:05, 09:06, 09:07, 09:08, 09:09, 09:10 , 09:11, 09:12]

And the spinner is empty.

  • adapter.notifyDataSetChanged(); - Suvitruf

1 answer 1

Solved the issue in this way.

 public class GetTime extends AsyncTask<Void, Void, Void> { ArrayList<String> list; public Context context; public GetTime(Context context) { this.context = context; } @Override protected void onPreExecute() { super.onPreExecute(); list=new ArrayList<>(); } @Override protected Void doInBackground(Void... arg0) { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(urlTime); try { httpResponseTime = httpClient.execute(httpPost); StringHolderTime = EntityUtils.toString(httpResponseTime.getEntity(), "UTF-8"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { JSONArray jArray =new JSONArray(StringHolderTime); for(int i=0;i<jArray.length();i++){ JSONObject jsonObject=jArray.getJSONObject(i); list.add(jsonObject.getString("time")); } } catch (JSONException e) { e.printStackTrace(); } return null; } protected void onPostExecute(Void result) { listItems.addAll(list); sp=(Spinner)findViewById(R.id.spinner); adapter=new ArrayAdapter<String>(RecordActivity.this, android.R.layout.simple_spinner_dropdown_item, list); sp.setAdapter(adapter); progressBar.setVisibility(View.GONE); } }