Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.ubprof.threadsandapis;
- import java.io.InputStream;
- import java.net.URL;
- import java.nio.charset.StandardCharsets;
- import java.util.Scanner;
- import android.os.AsyncTask;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public class TowingInfoActivity extends AppCompatActivity {
- private static String URL = "https://data.baltimorecity.gov/resource/rpx3-hpjc.json?tagnumber=";
- private static final String TAG = "Towing API Downloader";
- private Button getTowingInfoButton;
- private EditText licensePlate;
- private TextView towOrigin;
- private TextView towDestination;
- private TextView towAddress;
- private String licPlate;
- private String towOrig;
- private String towLocationCompany;
- private String towLocationAddress;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_towing_info);
- licensePlate = (EditText)findViewById(R.id.license_plate_input);
- towOrigin = (TextView)findViewById(R.id.license_plate_towed_from);
- towDestination = (TextView)findViewById(R.id.license_plate_tow_location);
- towAddress = (TextView)findViewById(R.id.license_plate_tow_address);
- getTowingInfoButton = (Button)findViewById(R.id.license_plate_get_info);
- getTowingInfoButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- licPlate = licensePlate.getText().toString().trim();
- if ( licPlate.equalsIgnoreCase("") )
- licPlate = "9GAA97";
- new GetInfoFromAPI().execute(URL, licPlate);
- }
- });
- }
- protected void loadInfo() {
- if ( licensePlate.getText().toString().trim().length() == 0 )
- licensePlate.setText(licPlate);
- towOrigin.setText(towOrig);
- towDestination.setText(towLocationCompany);
- towAddress.setText(towLocationAddress);
- }
- class GetInfoFromAPI extends AsyncTask<String, Void, String> {
- private String jsonResponse;
- @Override
- protected String doInBackground(String... strings) {
- String url_address = strings[0];
- String licPlate = strings[1];
- url_address += licPlate;
- Log.i(TAG, "doInBackground: Starting the download");
- try {
- URL url = new URL(url_address);
- InputStream in = url.openStream();
- Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name());
- jsonResponse = scanner.useDelimiter("\\A").next();
- in.close();
- Log.i(TAG, "doInBackground: " + jsonResponse);
- Log.i(TAG, "doInBackground: Done with the download");
- Log.i(TAG, "doInBackground: Working on the JSON response");
- JSONArray jsonArray = new JSONArray(jsonResponse);
- JSONObject jsonObject = jsonArray.getJSONObject(0);
- towOrig = jsonObject.getString("towedfromlocation");
- towLocationCompany = jsonObject.getString("storageyard");
- towLocationAddress = jsonObject.getString("storagelocation");
- Log.i(TAG, "doInBackground: Done with the JSON response");
- } catch (Exception e) {
- Log.i(TAG, "doInBackground: Exception occurred - " + e);
- }
- return "API Call Complete";
- }
- protected void onPostExecute(String result) {
- // Do nothing
- loadInfo();
- Log.i(TAG, "onPostExecute: Done!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement