Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. public class GetTime extends AsyncTask<Void, Void, Void> {
  2.  
  3. public Context context;
  4.  
  5. public GetTime(Context context) {
  6. this.context = context;
  7. }
  8.  
  9. @Override
  10. protected void onPreExecute() {
  11. super.onPreExecute();
  12. }
  13.  
  14. @Override
  15. protected Void doInBackground(Void... arg0) {
  16.  
  17. HttpClient httpClient = new DefaultHttpClient();
  18.  
  19. HttpPost httpPost = new HttpPost(urlTime);
  20.  
  21. try {
  22. httpResponseTime = httpClient.execute(httpPost);
  23.  
  24. StringHolderTime = EntityUtils.toString(httpResponseTime.getEntity(), "UTF-8");
  25.  
  26. } catch (ClientProtocolException e) {
  27. e.printStackTrace();
  28. } catch (IOException e) {
  29. e.printStackTrace();
  30. }
  31.  
  32. try {
  33.  
  34. JSONArray jsonArray = new JSONArray(StringHolderTime);
  35.  
  36. Log.i("MyLOG", "GetTime.doInBackground(): " + jsonArray.length());
  37.  
  38. List<String> list = new ArrayList<String>();
  39.  
  40. for (int i = 0; i < jsonArray.length(); i++) {
  41. JSONObject jsonObject = jsonArray.getJSONObject(i);
  42. String tim = jsonObject.getString("time");
  43. list.add(tim);
  44. }
  45.  
  46. Log.i("MyLOG", "GetTime.doInBackground(): " + list);
  47.  
  48. ArrayAdapter<String> adapter = new ArrayAdapter<String>(RecordActivity.this, android.R.layout.simple_spinner_item, list);
  49. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  50. Spinner spinner = (Spinner) findViewById(R.id.spinner);
  51. spinner.setAdapter(adapter);
  52.  
  53. } catch (JSONException e) {
  54. e.printStackTrace();
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. return null;
  59. }
  60.  
  61. protected void onPostExecute(Void result) {
  62.  
  63. progressBar.setVisibility(View.GONE);
  64.  
  65. }
  66. }
  67.  
  68. public class GetTime extends AsyncTask<Void, Void, Void> {
  69.  
  70. ArrayList<String> list;
  71.  
  72. public Context context;
  73.  
  74. public GetTime(Context context) {
  75. this.context = context;
  76. }
  77.  
  78. @Override
  79. protected void onPreExecute() {
  80. super.onPreExecute();
  81. list=new ArrayList<>();
  82. }
  83.  
  84. @Override
  85. protected Void doInBackground(Void... arg0) {
  86.  
  87. HttpClient httpClient = new DefaultHttpClient();
  88.  
  89. HttpPost httpPost = new HttpPost(urlTime);
  90.  
  91. try {
  92. httpResponseTime = httpClient.execute(httpPost);
  93.  
  94. StringHolderTime = EntityUtils.toString(httpResponseTime.getEntity(), "UTF-8");
  95.  
  96. } catch (ClientProtocolException e) {
  97. e.printStackTrace();
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101.  
  102. try {
  103. JSONArray jArray =new JSONArray(StringHolderTime);
  104. for(int i=0;i<jArray.length();i++){
  105. JSONObject jsonObject=jArray.getJSONObject(i);
  106. list.add(jsonObject.getString("time"));
  107. }
  108.  
  109. } catch (JSONException e) {
  110. e.printStackTrace();
  111. }
  112. return null;
  113. }
  114.  
  115. protected void onPostExecute(Void result) {
  116. listItems.addAll(list);
  117.  
  118. sp=(Spinner)findViewById(R.id.spinner);
  119. adapter=new ArrayAdapter<String>(RecordActivity.this, android.R.layout.simple_spinner_dropdown_item, list);
  120. sp.setAdapter(adapter);
  121.  
  122. progressBar.setVisibility(View.GONE);
  123.  
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement