Advertisement
rcalvente

Untitled

Dec 9th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.78 KB | None | 0 0
  1. import android.content.Intent;
  2. import android.os.AsyncTask;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.text.TextUtils;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.Toast;
  10.  
  11. import java.io.BufferedInputStream;
  12. import java.io.BufferedReader;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.io.InputStreamReader;
  16. import java.net.HttpURLConnection;
  17. import java.net.URL;
  18.  
  19. public class Screen2 extends AppCompatActivity {
  20.  
  21.     EditText matriculatxt, vintxt;
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_screen2);
  27.         matriculatxt = (EditText) findViewById(R.id.matricula);
  28.         vintxt = (EditText) findViewById(R.id.vin);
  29.     }
  30.  
  31.     public void consultar(View v){
  32.         if(TextUtils.isEmpty(matriculatxt.getText().toString()) && TextUtils.isEmpty(vintxt.getText().toString())){
  33.             Toast.makeText(this, "Por favor ingresa una matricula o un bastidor.", Toast.LENGTH_SHORT).show();
  34.             return;
  35.         }
  36.         try{
  37.             URL url;
  38.             url = new URL("http://dbpolicial.x10host.com/?action=consulta&matricula="+matriculatxt.getText().toString()+"&vin="+vintxt.getText().toString());
  39.             GetConsultaTask gct = new GetConsultaTask();
  40.             gct.execute(url);
  41.         }catch(Exception e ){
  42.             e.printStackTrace();
  43.         }
  44.     }
  45.  
  46.     public void volver(View v){
  47.         this.onBackPressed();
  48.         this.finish();
  49.     }
  50.  
  51.     public class GetConsultaTask extends AsyncTask<URL, String, String>{
  52.  
  53.         @Override
  54.         protected String doInBackground(URL... params) {
  55.             try {
  56.                 HttpURLConnection con = (HttpURLConnection) params[0].openConnection();
  57.  
  58.                 // Obtener el estado del recurso
  59.                 int statusCode = con.getResponseCode();
  60.  
  61.                 if(statusCode!=200) {
  62.                     runOnUiThread(new Runnable() {
  63.                         @Override
  64.                         public void run() {
  65.                             Toast.makeText(getApplicationContext(),"No se pudo hacer la consulta, intenta de nuevo más tarde", Toast.LENGTH_SHORT).show();
  66.                         }
  67.                     });
  68.                     return null;
  69.                 }
  70.  
  71.                 InputStreamReader in = new InputStreamReader(con.getInputStream());
  72.                 BufferedReader bufferedReader = new BufferedReader(in);
  73.                 StringBuilder total = new StringBuilder();
  74.                 String line;
  75.  
  76.                 while((line= bufferedReader.readLine()) != null){
  77.                     total.append(line);
  78.                 }
  79.  
  80.                 return total.toString();
  81.  
  82.             } catch (IOException e) {
  83.                 runOnUiThread(new Runnable() {
  84.                     @Override
  85.                     public void run() {
  86.                         Toast.makeText(getApplicationContext(), "No se pudo hacer la consulta, intenta de nuevo más tarde", Toast.LENGTH_SHORT).show();
  87.                     }
  88.                 });
  89.                 return null;
  90.             }
  91.         }
  92.  
  93.         @Override
  94.         protected void onPostExecute(String s) {
  95.             Intent i = null;
  96.             if(s == null){
  97.             }else{
  98.                 if(s.equals("")){
  99.                     i = new Intent(getApplicationContext(), Screen4_5.class);
  100.                     i.putExtra("layout", 4);
  101.                 }else {
  102.                     i = new Intent(getApplicationContext(), Screen3.class);
  103.                     i.putExtra("auto", s);
  104.                 }
  105.             }
  106.             startActivity(i);
  107.             finish();
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement