我正在尝试使用以下代码获取股票价格,该股票的当前价格返回null。请让我知道我在哪里出错
package com.example.safetyapplication;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.annotation.TargetApi;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.content.SharedPreferences;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener, LocationListener {
GoogleApiClient mGoogleApiClient;
private GoogleMap mMap;
private Marker mCurrLocationMarker;
private Location mLastLocation;
public static final String KEY_EMP_ID = "id";
public static final String KEY_EMP_LAT = "lat";
public static final String KEY_EMP_LONGG = "longg";
public static final String KEY_EMP_kondisi = "tipe";
public static final String URL_UPDATE_EMP = "http://192.168.43.106/Android/updatePgw.php";
private String id;
SharedPreferences sharedpreferences;
public final static String TAG_ID = "id";
//UI Component
TextView Text_X_Gyro, Text_Y_Gyro, Text_Z_Gyro;
TextView Text_X_Acclero, Text_Y_Acclero, Text_Z_Acclero;
TextView Text_Aktivitas;
public static String klasifikasi = "";
//Bolean(Penanda/ flagging untuk merekam file)
boolean isRunning;
private static float smoothed[] = new float[3];
private static SensorManager sensorMgr = null;
private static List<Sensor> sensors = null;
private static Sensor sensorGrav = null;
private static Sensor sensorMag = null;
private static final float grav[] = new float[3];
private static final float data[] = new float[3];
//Float Variabel
float x_gyro;
float y_gyro;
float z_gyro;
float x_acclero;
float y_acclero;
float z_acclero;
//File Stream
FileWriter writer;
final String TAG = "SensorLog";
//time
SimpleDateFormat dateFormat;
String time_string;
Timer timer;
//Sensor Component
SensorManager sensorManager;
Sensor gyro_sensor;
Sensor acclero_sensor;
Button buttonStart;
Button buttonStop;
//For map
LocationRequest mLocationRequest;
LatLng latLng;
GoogleMap mGoogleMap;
SupportMapFragment mFragment;
Marker mCurrLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mFragment.getMapAsync(this);
//mMap.setMyLocationEnabled(true);
sharedpreferences = getSharedPreferences(Login.my_shared_preferences, Context.MODE_PRIVATE);
id = getIntent().getStringExtra(TAG_ID);
isRunning = false;
timer = new Timer();
time_string = new String();
dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
gyro_sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
acclero_sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
Text_X_Gyro = (TextView) findViewById(R.id.text_x_gyro);
Text_Y_Gyro = (TextView) findViewById(R.id.text_y_gyro);
Text_Z_Gyro = (TextView) findViewById(R.id.text_z_gyro);
Text_X_Acclero = (TextView) findViewById(R.id.text_x_acclero);
Text_Y_Acclero = (TextView) findViewById(R.id.text_y_acclero);
Text_Z_Acclero = (TextView) findViewById(R.id.text_z_acclero);
Text_Aktivitas = (TextView) findViewById(R.id.text_aktivitas);
buttonStart = (Button) findViewById(R.id.button_start);
buttonStop = (Button) findViewById(R.id.button_stop);
buttonStart.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);
sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE);
sensors = sensorMgr.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (sensors.size() > 0) sensorGrav = sensors.get(0);
sensorManager.registerListener(gyroListener, gyro_sensor, SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(accleroListener, acclero_sensor, SensorManager.SENSOR_DELAY_NORMAL);
Log.d(TAG, "Writing to " + getStorageDir());
try {
writer = new FileWriter(new File(getStorageDir(), "sensors_" + System.currentTimeMillis() + ".csv"));
String data = String.format("%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s\n ",
"Waktu", "x_gyro", "y_gyro", "z_gyro", "x_acclero", "y_acclero", "z_acclero", "x_filter_gyro", "y_filter_gyro", "z_filter_gyro", "x_filter_acclero", "y_filter_acclero", "z_filter_acclero");
writer.write(data);
Log.e("Data training", data);
isRunning = true;
new MyThread("wew");
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
});
buttonStop.setOnTouchListener(new View.OnTouchListener() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
buttonStart.setEnabled(true);
buttonStop.setEnabled(false);
isRunning = false;
sensorManager.flush(gyroListener);
sensorManager.flush(accleroListener);
sensorManager.unregisterListener(gyroListener);
sensorManager.unregisterListener(accleroListener);
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
});
}
public void updateData() {
final String lat = String.valueOf(mLastLocation.getLatitude());
final String longg = String.valueOf(mLastLocation.getLongitude());
final String tipe = "kecelakan";
class UpdateData extends AsyncTask<Void, Void, String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(MapsActivity.this, "Updating...", "Wait...", false, false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(MapsActivity.this, s, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Void... params) {
HashMap<String,String> hashMap = new HashMap<>();
hashMap.put(MapsActivity.KEY_EMP_ID,id);
hashMap.put(MapsActivity.KEY_EMP_LONGG,longg);
hashMap.put(MapsActivity.KEY_EMP_LAT,lat);
hashMap.put(MapsActivity.KEY_EMP_kondisi,tipe);
RequestHandler rh = new RequestHandler();
String s = rh.sendPostRequest(MapsActivity.URL_UPDATE_EMP,hashMap);
return s;
}
}
UpdateData ue = new UpdateData();
ue.execute();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setMyLocationEnabled(true);
buildGoogleApiClient();
mGoogleApiClient.connect();
}
@Override
public void onPause() {
super.onPause();
//Unregister for location callbacks:
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
protected synchronized void buildGoogleApiClient() {
Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(this,"onConnected",Toast.LENGTH_SHORT).show();
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
//place marker at current position
mGoogleMap.clear();
LatLng latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude())).zoom(14).build();
mGoogleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// create markerOptions
MarkerOptions markerOptions = new MarkerOptions().position(new LatLng(
mLastLocation.getLatitude(), mLastLocation.getLongitude()));
// ROSE color icon
markerOptions.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
markerOptions.position(latLng);
// adding markerOptions
Marker marker = mGoogleMap.addMarker(markerOptions);
//dropPinEffect(marker);
}
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000); //5 seconds
mLocationRequest.setFastestInterval(3000); //3 seconds
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
//mLocationRequest.setSmallestDisplacement(0.1F); //1/10 meter
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionSuspended(int i) {
Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show();
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
//remove previous current location marker and add new one at current position
if (mCurrLocation != null) {
mCurrLocation.remove();
}
latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocation = mGoogleMap.addMarker(markerOptions);
Toast.makeText(this,"Location Changed \n " +
"Latitude : "+ mLastLocation.getLatitude()+
"\nLongitude : "+mLastLocation.getLongitude(),Toast.LENGTH_SHORT).show();
//If you only need one location, unregister the listener
//LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
//Mulai Menjalankan Algoritma
public static class euclidean {
public static double distance(double x[], double y[]) {
double ds = 0.0;
for (int n = 0; n < x.length; n++)
ds += Math.pow(x[n] - y[n], 2.0);
ds = Math.sqrt(ds);
return ds;
}
public static double distance(int x[], int y[]) {
double ds = 0.0;
for (int n = 0; n < x.length; n++)
ds += Math.pow(x[n] - y[n], 2.0);
ds = Math.sqrt(ds);
return ds;
}
public static double[] distance(double x[][], double y[]) {
double ds[] = new double[x.length];
for (int n = 0; n < x.length; n++)
ds[n] = distance(x[n], y);
return ds;
}
}
public static void hitung(double aix2[][], double array2[]) {
double target[] = {0,3,1,2,3,0,0,1,1,3,3,1,2,0,2,0,1,2,0,3,3,3,1,1,3,1,2,0,2,1,2,3,1,2,2,2,1,2,2,3,2,2,2,0,0,0,3,2,1,0,1,0,1,0,0,1,2,2,3,0,3,2,0,2,2,};
klasifikasi = "";
double ds5[] = euclidean.distance(aix2, array2);
double indexTerkecil = ds5[0];
double jumlah = ds5[0];
int indeks = 0;
for (int n = 1; n < ds5.length; n++) {
if (indexTerkecil > ds5[n]) {
indexTerkecil = ds5[n];
indeks = n;
}
}
System.out.println("Jarak = " +jumlah);
double output = target[indeks];
if (output == 0) {
klasifikasi = "berhenti";
} else if (output == 1) {
klasifikasi = "kanan";
} else if (output == 2) {
klasifikasi = "kiri";
} else if (output == 3) {
klasifikasi = "lurus";
}
return;
}
private String getStorageDir() {
return this.getExternalFilesDir(null).getAbsolutePath();
}
@Override
protected void onResume() {
super.onResume();
sensorManager.registerListener(gyroListener, gyro_sensor, 500);
sensorManager.registerListener(accleroListener, acclero_sensor, 500);
}
public SensorEventListener gyroListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int acc) {
}
public void onSensorChanged(SensorEvent event) {
smoothed = LowPassGyro.filter(event.values, data);
data[0] = smoothed[0];
data[1] = smoothed[1];
data[2] = smoothed[2];
x_gyro = event.values[0];
y_gyro = event.values[1];
z_gyro = event.values[2];
time_string = dateFormat.format(new Date());
Text_X_Gyro.setText("X : " + (int) x_gyro + " rad/s");
Text_Y_Gyro.setText("Y : " + (int) y_gyro + " rad/s");
Text_Z_Gyro.setText("Z : " + (int) z_gyro + " rad/s");
return;
}
};
public SensorEventListener accleroListener = new SensorEventListener() {
public void onSensorChanged(SensorEvent event) {
smoothed = LowPass.filter(event.values, grav);
grav[0] = smoothed[0];
grav[1] = smoothed[1];
grav[2] = smoothed[2];
x_acclero = event.values[0];
y_acclero = event.values[1];
z_acclero = event.values[2];
//waktu dan tanggal record
time_string = dateFormat.format(new Date());
Text_X_Acclero.setText("X : " + (int) x_acclero + " m/s2");
Text_Y_Acclero.setText("Y : " + (int) y_acclero + " m/s2");
Text_Z_Acclero.setText("Z : " + (int) z_acclero + " m/s2");
if(x_acclero>4 && y_gyro<-2 && z_gyro<2.5 || x_acclero<-3 && y_gyro>1 && z_gyro>2){
//updateData();
Intent intent = new Intent(MapsActivity.this, timer.class);
finish();
startActivity(intent);
}
Text_Aktivitas.setText("Klasifikasi: "+klasifikasi);
return;
}
public void onAccuracyChanged(Sensor sensor, int i) {
}
};
class MyThread implements Runnable {
String name;
Thread t;
MyThread(String thread) {
name = thread;
t = new Thread(this, name);
t.start();
}
public void run() {
saveData();
}
}
void saveData() {
if(isRunning){
long millis = System.currentTimeMillis();
try {
double aix2[][] = {{0.035761682,-0.019344124,0.035150953,0.032311879,-0.00018369,0.100137353},
{-0.042901707,0.041317631,0.007286108,0.086716193,0.008781007,1.002297104},
{0.004676963,-0.056830688,0.018722337,0.340441302,1.086654524,-0.477470059},
{0.1463356,0.274989709,0.609545266,-0.194716174,-0.14179144,0.155942736},
{-0.145548499,-0.136981911,0.151675112,-0.198158226,-0.786070552,0.930577774},
{-0.015964601,-0.007779994,-0.016256204,0.142251001,-0.108053563,0.023068666},
{0.012686234,0.016150223,0.005017862,-0.062129863,-0.090846476,0.124672636},
{0.061686894,-0.101820197,-0.756484164,1.00267862,0.1182269,0.974536364},
{0.407312723,-0.52120923,-0.66811351,-0.300881223,-0.946452101,1.036954266},
{0.023605901,0.087869199,0.06757537,0.272246535,-0.054762143,-0.377033845},
{0.01695683,0.053927757,-0.123113426,-0.038898869,-0.365282804,0.314062935},
{0.049636594,-0.083803385,-0.666945483,0.941211502,0.643612031,-0.331097141},
{0.373466894,0.395784745,0.887235421,-0.691948812,-0.759120597,1.589346857},
{-0.013309933,0.006232733,-0.015363852,-0.040615629,0.509979926,-0.11549942},
{0.184421721,0.278026147,0.781664756,-0.163596339,-0.750397269,0.559708183},
{0.013332936,-0.010623151,-0.024132664,-0.137466058,0.145273363,0.433774299},
{0.287198767,-0.379178347,-0.331807125,0.030314293,-0.441642694,0.408603762},
{0.145136819,0.084905311,1.009578595,-1.486282588,-1.511547201,1.444342936},
{0.012498108,-0.003177879,-0.000550045,0.004745571,0.083359708,0.126703999},
{0.04147466,0.018841393,-0.189635459,-0.074927026,-0.289680525,0.251294191},
{-0.315400639,-0.151240592,-0.079700337,-0.154679858,-0.621962554,1.624364883},
{0.020117743,0.024207055,-0.034465226,0.120947137,0.484176483,-0.579600566},
{0.049636594,-0.083803385,-0.666945483,0.941211502,0.643612031,-0.331097141},
{0.108823007,-0.273502799,-0.8196025,0.215027727,-0.433102883,0.208008771},
{-0.003178423,-0.060222876,0.011683096,0.121533797,0.224970274,-0.306486697},
{0.237341013,-0.348337715,-0.774501517,1.016353871,-1.393502463,0.451865643},
{-0.0227,-0.1174,0.3691,-0.6044,-1.5173,1.7837},
{-0.041144989,-0.030366262,-0.074040188,0.184214189,-0.093618718,-0.132963026},
{0.065834627,0.141308686,-0.295023417,-0.199564627,-1.100067362,0.829617474},
{0.273723401,-0.362500435,-0.515534637,0.28202117,-0.55916247,1.120594433},
{0.02512901,-0.050467819,-0.196924717,-0.525965486,0.532652902,-0.180433814},
{-0.068288395,0.051995577,-0.151798269,-0.071247337,-0.305618206,-0.057650188},
{0.206612108,-0.239540905,-0.770686131,1.025922346,-0.897010244,0.490973972},
{0.172485793,0.242570494,0.814108013,-0.361566696,0.030220305,0.913462254},
{0.11988,0.23247,-0.03699,-0.25842,-1.85117,1.10185},
{0.192112742,0.250647558,0.735346241,-0.827323899,-0.870933232,1.203668751},
{0.215681074,-0.284584085,-0.246642682,-0.017992936,0.576622819,1.185492381},
{0.205458282,0.259219543,0.704331419,-0.654672417,-0.468033396,0.66426595},
{0.049240275,0.054313219,0.064373269,-0.170258626,1.343375242,-0.57681105},
{0.01695683,0.053927757,-0.123113426,-0.038898869,-0.365282804,0.314062935},
{0.156564947,0.09824787,0.818033366,-0.895614633,-0.979321806,0.61791721},
{0.126493319,0.295738768,0.28439107,-0.345527783,-1.811673559,1.56685716},
{0.029593513,0.002594271,0.319413672,-0.308217862,0.213225911,-0.20702309},
{-0.009316425,-0.016701904,0.007054281,-0.088892594,0.044072863,0.159027949},
{-0.015964601,-0.007779994,-0.016256204,0.142251001,-0.108053563,0.023068666},
{-0.106139798,-0.146290332,0.129990007,-0.014125984,-0.090278728,0.277535271},
{0.006303996,-0.050931863,0.000909124,-0.107186445,0.621193292,0.285097806},
{0.038390011,-0.037552388,0.659570297,-0.835606246,-0.01961661,0.544287415},
{0.29856506,-0.35160755,-0.569723861,1.274508504,-2.941398786,1.536251892},
{-0.033502042,0.040126732,-0.007655404,-0.087958052,0.241865358,0.070854432},
{0.021594795,-0.065982401,-0.661473549,0.27343122,0.121480497,0.229476172},
{0.003298395,-0.00161931,0.007807518,0.06805329,-0.123312046,0.161599157},
{0.237341013,-0.348337715,-0.774501517,1.016353871,-1.393502463,0.451865643},
{-0.047867406,0.068055436,-0.003512188,-0.392864988,-0.217282801,0.134669078},
{0.015470718,-0.030772846,0.057959928,0.120254699,0.12665832,-0.120198521},
{0.19139774,-0.223436974,-1.087257045,1.329093254,-0.744316402,1.221514437},
{0.091345578,0.105984901,0.608205328,-0.90955944,-0.826026802,0.39910964},
{0.075104355,0.111419652,0.11146942,-0.325498476,0.117224544,-0.891715139},
{-0.083326773,-0.047380145,0.032638657,0.205921416,-0.447212682,0.705134214},
{0.035072544,0.057617647,-0.022165452,0.055286205,0.154768802,0.126639895},
{0.071603983,-0.076007959,0.002430875,0.496339405,1.615074421,0.380099077},
{0.0922,-0.134,0.5634,-1.275,-1.2285,0.283},
{-0.078969036,0.123789068,-0.043583955,-0.231516381,-0.420555713,0.137106997},
{0.2976063,0.379166028,0.448869031,-0.106743007,-0.570641965,1.138460714},
{0.189349895,0.225385876,0.916334623,-1.200572283,-0.468719144,1.709720708},
};
double array2[] = new double[6];
array2[0] = data[0];
array2[1] = data[1];
array2[2] = data[2];
array2[3] = grav[0];
array2[4] = grav[1];
array2[5] = grav[2];
double raw[] = new double[6];
raw[0] = x_gyro;
raw[1] = y_gyro;
raw[2] = z_gyro;
raw[3] = x_acclero;
raw[4] = y_acclero;
raw[5] = z_acclero;
if(x_acclero>4 && y_gyro<-2 && z_gyro<2.5 || x_acclero<-3 && y_gyro>1 && z_gyro>2){
klasifikasi="Kecelakaan";
}
else {
hitung(aix2, array2);
}
writer.write(String.format("%s, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %s \n ",
time_string, x_gyro, y_gyro, z_gyro, x_acclero, y_acclero, z_acclero, data[0], data[1], data[2], grav[0], grav[1], grav[2], klasifikasi));
Thread.sleep(100 - millis % 100);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}