Advertisement
NexGenration

Untitled

Dec 15th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. package com.example.ubprof.threadsandapis;
  2.  
  3. import java.io.InputStream;
  4. import java.net.URL;
  5. import java.nio.charset.StandardCharsets;
  6. import java.util.Scanner;
  7.  
  8. import android.os.AsyncTask;
  9. import android.support.v7.app.AppCompatActivity;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.View;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import org.json.JSONArray;
  19. import org.json.JSONObject;
  20.  
  21. public class TowingInfoActivity extends AppCompatActivity {
  22.  
  23.     private static String URL = "https://data.baltimorecity.gov/resource/rpx3-hpjc.json?tagnumber=";
  24.     private static final String TAG = "Towing API Downloader";
  25.  
  26.     private Button getTowingInfoButton;
  27.     private EditText licensePlate;
  28.     private TextView towOrigin;
  29.     private TextView towDestination;
  30.     private TextView towAddress;
  31.  
  32.     private String licPlate;
  33.     private String towOrig;
  34.     private String towLocationCompany;
  35.     private String towLocationAddress;
  36.  
  37.     @Override
  38.     protected void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         setContentView(R.layout.activity_towing_info);
  41.  
  42.         licensePlate = (EditText)findViewById(R.id.license_plate_input);
  43.         towOrigin = (TextView)findViewById(R.id.license_plate_towed_from);
  44.         towDestination = (TextView)findViewById(R.id.license_plate_tow_location);
  45.         towAddress = (TextView)findViewById(R.id.license_plate_tow_address);
  46.  
  47.         getTowingInfoButton = (Button)findViewById(R.id.license_plate_get_info);
  48.         getTowingInfoButton.setOnClickListener(new View.OnClickListener() {
  49.             @Override
  50.             public void onClick(View v) {
  51.                 licPlate = licensePlate.getText().toString().trim();
  52.                 if ( licPlate.equalsIgnoreCase("") )
  53.                     licPlate = "9GAA97";
  54.                 new GetInfoFromAPI().execute(URL, licPlate);
  55.             }
  56.         });
  57.     }
  58.  
  59.     protected void loadInfo() {
  60.         if ( licensePlate.getText().toString().trim().length() == 0 )
  61.             licensePlate.setText(licPlate);
  62.         towOrigin.setText(towOrig);
  63.         towDestination.setText(towLocationCompany);
  64.         towAddress.setText(towLocationAddress);
  65.     }
  66.  
  67.     class GetInfoFromAPI extends AsyncTask<String, Void, String> {
  68.  
  69.         private String jsonResponse;
  70.  
  71.         @Override
  72.         protected String doInBackground(String... strings) {
  73.             String url_address = strings[0];
  74.             String licPlate = strings[1];
  75.  
  76.             url_address += licPlate;
  77.  
  78.             Log.i(TAG, "doInBackground: Starting the download");
  79.  
  80.             try {
  81.                 URL url = new URL(url_address);
  82.  
  83.                 InputStream in = url.openStream();
  84.                 Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name());
  85.                 jsonResponse = scanner.useDelimiter("\\A").next();
  86.  
  87.                 in.close();
  88.  
  89.                 Log.i(TAG, "doInBackground: " + jsonResponse);
  90.                 Log.i(TAG, "doInBackground: Done with the download");
  91.  
  92.                 Log.i(TAG, "doInBackground: Working on the JSON response");
  93.  
  94.                 JSONArray jsonArray = new JSONArray(jsonResponse);
  95.                 JSONObject jsonObject = jsonArray.getJSONObject(0);
  96.  
  97.                 towOrig = jsonObject.getString("towedfromlocation");
  98.                 towLocationCompany = jsonObject.getString("storageyard");
  99.                 towLocationAddress = jsonObject.getString("storagelocation");
  100.  
  101.                 Log.i(TAG, "doInBackground: Done with the JSON response");
  102.  
  103.             } catch (Exception e) {
  104.                 Log.i(TAG, "doInBackground: Exception occurred - " + e);
  105.             }
  106.  
  107.             return "API Call Complete";
  108.         }
  109.  
  110.         protected void onPostExecute(String result) {
  111.             // Do nothing
  112.             loadInfo();
  113.             Log.i(TAG, "onPostExecute: Done!!!");
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement