Я пытаюсь передать переменные Obs_alt
, Obs_lat
, Obs_lng
из MainActivity в класс AsyncTask, но не могу распознать их в AsyncTask
Main Activity
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
EditText Obs_latitude_et, Obs_longitude_et, Obs_altitude_et;
String Obs_latitude, Obs_longitude, Obs_altitude;
Obs_altitude_et = findViewById(R.id.obs_alt);
Obs_latitude_et = findViewById(R.id.obs_lat);
Obs_longitude_et = findViewById(R.id.obs_lng);
Obs_altitude = Obs_altitude_et.getText().toString();
Obs_latitude = Obs_latitude_et.getText().toString();
Obs_longitude = Obs_longitude_et.getText().toString();
final AsyncTask<Void, Void, Void> getCoordinatesTask;
getCoordinatesTask = new getCoordinatesTask();
Runnable runnable = new Runnable() {
public void run() {
getCoordinatesTask.execute();
}
};
Thread thread = new Thread(runnable);
thread.start();
TextView iss_altitude_tv = findViewById ( R.id.ISS_alt );
TextView iss_longitude_tv = findViewById ( R.id.ISS_lng );
TextView iss_latitude_tv = findViewById ( R.id.ISS_lat );
TextView iss_azimuth_tv = findViewById ( R.id.ISS_azm );
TextView iss_elevation_tv = findViewById ( R.id.ISS_elv );
TextView iss_ra_tv = findViewById ( R.id.ISS_RA );
TextView iss_dec_tv = findViewById ( R.id.ISS_dec );
TextView iss_timestamp_tv =findViewById ( R.id.ISS_ts );
iss_altitude_tv.setText ( JSON.getIss_altitude ( ) );
iss_latitude_tv.setText ( JSON.getIss_latitude ( ) );
iss_longitude_tv.setText ( JSON.getIss_longitude ( ) );
iss_azimuth_tv.setText ( JSON.getIss_azimuth ( ) );
iss_elevation_tv.setText ( JSON.getIss_elevation ( ) );
iss_ra_tv.setText ( JSON.getIss_ra ( ) );
iss_dec_tv.setText ( JSON.getIss_dec ( ) );
iss_timestamp_tv.setText ( JSON.getIss_timestamp ( ) );
TextView hst_altitude_tv = findViewById ( R.id.HST_alt );
TextView hst_longitude_tv = findViewById ( R.id.HST_lng );
TextView hst_latitude_tv = findViewById ( R.id.HST_lat );
TextView hst_azimuth_tv = findViewById ( R.id.HST_azm );
TextView hst_elevation_tv = findViewById ( R.id.HST_elv );
TextView hst_ra_tv = findViewById ( R.id.HST_RA );
TextView hst_dec_tv = findViewById ( R.id.HST_dec );
TextView hst_timestamp_tv = findViewById ( R.id.HST_ts );
hst_altitude_tv.setText ( JSON.getHst_altitude ( ) );
hst_latitude_tv.setText ( JSON.getHst_latitude ( ) );
hst_longitude_tv.setText ( JSON.getHst_longitude ( ) );
hst_azimuth_tv.setText ( JSON.getHst_azimuth ( ) );
hst_elevation_tv.setText ( JSON.getHst_elevation ( ) );
hst_ra_tv.setText ( JSON.getHst_ra ( ) );
hst_dec_tv.setText ( JSON.getHst_dec ( ) );
hst_timestamp_tv.setText ( JSON.getHst_timestamp ( ) );
TextView ls2_altitude_tv = findViewById ( R.id.LS2_alt );
TextView ls2_longitude_tv = findViewById ( R.id.LS2_lng );
TextView ls2_latitude_tv = findViewById ( R.id.LS2_lat );
TextView ls2_azimuth_tv = findViewById ( R.id.LS2_azm );
TextView ls2_elevation_tv = findViewById ( R.id.LS2_elv );
TextView ls2_ra_tv = findViewById ( R.id.LS2_ra );
TextView ls2_dec_tv = findViewById ( R.id.LS2_dec );
TextView ls2_timestamp_tv = findViewById ( R.id.LS2_ts );
ls2_altitude_tv.setText ( JSON.getLs2_altitude ( ) );
ls2_latitude_tv.setText ( JSON.getLs2_latitude ( ) );
ls2_longitude_tv.setText ( JSON.getLs2_longitude ( ) );
ls2_azimuth_tv.setText ( JSON.getLs2_azimuth ( ) );
ls2_elevation_tv.setText ( JSON.getLs2_elevation ( ) );
ls2_ra_tv.setText ( JSON.getLs2_ra ( ) );
ls2_dec_tv.setText ( JSON.getLs2_dec ( ) );
ls2_timestamp_tv.setText ( JSON.getLs2_timestamp ( ) );
}
}
Async Task
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
class getCoordinatesTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
URL url_hst = null;
try {
url_hst = new URL("https://www.n2yo.com/rest/v1/satellite/positions/20580/" + Obs_alt + "/" + Obs_lng + "/" + Obs_alt + "/1/&apiKey=blahblahblah");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection httpConn_hst = null;
try {
assert url_hst != null;
httpConn_hst = (HttpURLConnection) url_hst.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
int responseCode_hst = 0;
try {
assert httpConn_hst != null;
responseCode_hst = httpConn_hst.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
if (responseCode_hst == HttpURLConnection.HTTP_OK) {
InputStream inputStream_hst = null;
try {
inputStream_hst = httpConn_hst.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream outputStream_hst = null;
try {
outputStream_hst = new FileOutputStream("/storage/emulated/0/SatTracker/" + File.separator + "hst.json");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int bytesRead_hst = 0;
byte[] buffer_hst = new byte[4096];
while (true) {
try {
assert inputStream_hst != null;
if ((bytesRead_hst = inputStream_hst.read(buffer_hst)) == -1) break;
} catch (IOException e) {
e.printStackTrace();
}
assert outputStream_hst != null;
try {
outputStream_hst.write(buffer_hst, 0, bytesRead_hst);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
assert outputStream_hst != null;
outputStream_hst.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream_hst.close();
} catch (IOException e) {
e.printStackTrace();
}
}
URL url_iss = null;
try {
url_iss = new URL("https://www.n2yo.com/rest/v1/satellite/positions/25544/" + obs_latitude + "/" + obs_longitude + "/" + obs_altitude + "/1/&apiKey=blahblahblah");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection httpConn_iss = null;
try {
assert url_iss != null;
httpConn_iss = (HttpURLConnection) url_iss.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
int responseCode_iss = 0;
try {
assert httpConn_iss != null;
responseCode_iss = httpConn_iss.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
if (responseCode_iss == HttpURLConnection.HTTP_OK) {
InputStream inputStream_iss = null;
try {
inputStream_iss = httpConn_iss.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream outputStream_iss = null;
try {
outputStream_iss = new FileOutputStream("/storage/emulated/0/SatTracker/" + File.separator + "iss.json");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int bytesRead_iss = 0;
byte[] buffer_iss = new byte[4096];
while (true) {
try {
assert inputStream_iss != null;
if ((bytesRead_iss = inputStream_iss.read(buffer_iss)) == -1) break;
} catch (IOException e) {
e.printStackTrace();
}
assert outputStream_iss != null;
try {
outputStream_iss.write(buffer_iss, 0, bytesRead_iss);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
assert outputStream_iss != null;
outputStream_iss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream_iss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
URL url_ls2 = null;
try {
url_ls2 = new URL("https://www.n2yo.com/rest/v1/satellite/positions/44420/" + obs_latitude + "/" + obs_longitude + "/" + obs_altitude + "/1/&apiKey=blahblahblah");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection httpConn_ls2 = null;
try {
assert url_ls2 != null;
httpConn_ls2 = (HttpURLConnection) url_ls2.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
int responseCode_ls2 = 0;
try {
assert httpConn_ls2 != null;
responseCode_ls2 = httpConn_ls2.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
if (responseCode_ls2 == HttpURLConnection.HTTP_OK) {
InputStream inputStream_ls2 = null;
try {
inputStream_ls2 = httpConn_ls2.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream outputStream_ls2 = null;
try {
outputStream_ls2 = new FileOutputStream("/storage/emulated/0/SatTracker/" + File.separator + "ls2.json");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
int bytesRead_ls2 = 0;
byte[] buffer_ls2 = new byte[4096];
while (true) {
try {
assert inputStream_ls2 != null;
if ((bytesRead_ls2 = inputStream_ls2.read(buffer_ls2)) == -1) break;
} catch (IOException e) {
e.printStackTrace();
}
assert outputStream_ls2 != null;
try {
outputStream_ls2.write(buffer_ls2, 0, bytesRead_ls2);
} catch (IOException e) {
e.printStackTrace();
}
}
try {
assert outputStream_ls2 != null;
outputStream_ls2.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream_ls2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
JSONObject joHST = (JSONObject) new JSONParser().parse(new FileReader("hst.json"));
JSONObject joISS = (JSONObject) new JSONParser().parse(new FileReader("hst.json"));
JSONObject joLS2 = (JSONObject) new JSONParser().parse(new FileReader("hst.json"));
JSON.setHst_latitude(joHST.get("satlatitude").toString());
JSON.setHst_altitude(joHST.get("sataltitude").toString());
JSON.setHst_longitude(joHST.get("satlongitude").toString());
JSON.setHst_azimuth(joHST.get("azimuth").toString());
JSON.setHst_elevation(joHST.get("elevation").toString());
JSON.setHst_ra(joHST.get("ra").toString());
JSON.setHst_dec(joHST.get("dec").toString());
JSON.setHst_timestamp(joHST.get("timestamp").toString());
JSON.setIss_latitude(joISS.get("satlatitude").toString());
JSON.setIss_altitude(joISS.get("sataltitude").toString());
JSON.setIss_longitude(joISS.get("satlongitude").toString());
JSON.setIss_azimuth(joISS.get("azimuth").toString());
JSON.setIss_elevation(joISS.get("elevation").toString());
JSON.setIss_ra(joISS.get("ra").toString());
JSON.setIss_dec(joISS.get("dec").toString());
JSON.setIss_timestamp(joISS.get("timestamp").toString());
JSON.setLs2_latitude(joLS2.get("satlatitude").toString());
JSON.setLs2_longitude(joLS2.get("satlongitude").toString());
JSON.setLs2_altitude(joLS2.get("sataltitude").toString());
JSON.setLs2_azimuth(joLS2.get("azimuth").toString());
JSON.setLs2_elevation(joLS2.get("elevation").toString());
JSON.setLs2_ra(joLS2.get("ra").toString());
JSON.setLs2_dec(joLS2.get("dec").toString());
JSON.setLs2_timestamp(joLS2.get("timestamp").toString());
} catch (JSONException | ParseException | IOException e) {
e.printStackTrace();
}
return null;
}
}
Мне нужно получить эти переменные в моем классе AsyncTask