Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2014
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. package com.lleguebien.prototipo1;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.client.ClientProtocolException;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.impl.client.DefaultHttpClient;
  14. import org.json.JSONArray;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17.  
  18. import android.app.Activity;
  19. import android.os.AsyncTask;
  20. import android.os.Bundle;
  21. import android.support.v4.app.FragmentActivity;
  22. import android.util.Log;
  23. import android.view.Menu;
  24. import android.view.MenuItem;
  25. import android.view.Window;
  26. import android.widget.ArrayAdapter;
  27. import android.widget.ListView;
  28. import android.widget.Spinner;
  29. import android.widget.Toast;
  30.  
  31. public class CheckIn extends FragmentActivity {
  32.  
  33. @Override
  34. protected void onCreate(Bundle savedInstanceState) {
  35. Log.i("boton", "Llegue");
  36. super.onCreate(savedInstanceState);
  37. requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  38. setContentView(R.layout.check_in);
  39.  
  40.  
  41. String patente = this.getIntent().getExtras().getString("patente");
  42. new LlenarLV().execute(patente);
  43. }
  44.  
  45. private class LlenarLV extends AsyncTask<String, Void, JSONObject>{
  46.  
  47. @Override
  48. protected JSONObject doInBackground(String... patente) {
  49. Log.i("PruebasBla", "Iniciando Asyncreq");
  50. JSONObject alumnos = ScrapDB(patente[0]);
  51. Log.i("PruebasBla", "Tengo JSONObject");
  52.  
  53. Log.i("PruebasBla", alumnos.toString());
  54. return alumnos;
  55. }
  56.  
  57. private JSONObject ScrapDB(String pat){
  58. // TODO Auto-generated method stub
  59. StringBuilder stringBuilder = new StringBuilder();
  60. try {
  61. HttpGet httpget = new HttpGet("http://testmobile.xephiro.com/ws/rest.php/load/alumnos/"+pat);
  62. HttpClient client = new DefaultHttpClient();
  63. HttpResponse response;
  64. stringBuilder = new StringBuilder();
  65.  
  66. Log.i("PruebasBla", "Antes de ejecutar post");
  67. response = client.execute(httpget);
  68. HttpEntity entity = response.getEntity();
  69. InputStream stream = entity.getContent();
  70. int b;
  71.  
  72. while ((b = stream.read()) != -1) {
  73. stringBuilder.append((char) b);
  74. }
  75. } catch (ClientProtocolException e) {
  76. Log.i("AGREGAR", "Client protocol exception");
  77. } catch (IOException e) {
  78. Log.i("AGREGAR", "IOException");
  79. }
  80. Log.i("PruebasBla", "Antes de crear json");
  81. JSONObject jsonObject = new JSONObject();
  82. try {
  83. jsonObject = new JSONObject(stringBuilder.toString());
  84. } catch (JSONException e) {
  85. e.printStackTrace();
  86. }
  87.  
  88. return jsonObject;
  89. }
  90.  
  91. @Override
  92. protected void onPreExecute(){
  93. setProgressBarIndeterminateVisibility(true);
  94. }
  95.  
  96. @Override
  97. protected void onPostExecute(JSONObject result){
  98. //pb.setVisibility(View.GONE);
  99. Log.i("PruebasBla", "dentro de post execute");
  100. if (result == null)
  101. {
  102. Log.e("AGREGAR", "NO SE OBTUVIERON ALUMNOS");
  103. }
  104. else{
  105. List<String> listArray = new ArrayList<String>();
  106. //spinnerArray.add("item1");
  107. //spinnerArray.add("item2");
  108.  
  109. JSONArray pats;
  110. try {
  111. pats = result.getJSONArray("results");
  112. for (int i = 0, size = pats.length(); i < size; i++)
  113. {
  114.  
  115. JSONObject furgoni = pats.getJSONObject(i);
  116.  
  117. String patentei = furgoni.getString("Patente");
  118. listArray.add(patentei);
  119.  
  120. }
  121. } catch (JSONException e) {
  122. // TODO Auto-generated catch block
  123. Log.i("PruebasBla", "catch");
  124. e.printStackTrace();
  125. }
  126.  
  127. ArrayAdapter<String> adapter = new ArrayAdapter<String>(CheckIn.this, android.R.layout.simple_list_item_1, listArray);
  128.  
  129. ListView lv = (ListView) findViewById(R.id.listView1);
  130. lv.setAdapter(adapter);
  131. setProgressBarIndeterminateVisibility(false);
  132.  
  133. }
  134. }
  135. }
  136.  
  137. @Override
  138. public boolean onCreateOptionsMenu(Menu menu) {
  139. // Inflate the menu; this adds items to the action bar if it is present.
  140. getMenuInflater().inflate(R.menu.check_in, menu);
  141. return true;
  142. }
  143.  
  144. @Override
  145. public boolean onOptionsItemSelected(MenuItem item) {
  146. // Handle action bar item clicks here. The action bar will
  147. // automatically handle clicks on the Home/Up button, so long
  148. // as you specify a parent activity in AndroidManifest.xml.
  149. int id = item.getItemId();
  150. if (id == R.id.action_settings) {
  151. return true;
  152. }
  153. return super.onOptionsItemSelected(item);
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement