La partie principale de cette question est, lorsque j'exécute ce code, les TextViews latitudeTextView et longitudeTextView sont mises à jour correctement, donc la variable globale est en train de changer les valeurs correctes. Mais quand j'essaie d'y accéder à nouveau après un async, ils sont 0.0, 0.0? Ne devraient-ils pas rester les mêmes valeurs après la fin de onPostExecute? Variable globale retournant à 0.0 après l'achèvement d'un AsyncTask
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Weather>{
private static final String GOOGLE_CONVERTER = "https://maps.googleapis.com/maps/api/geocode/json";
private static final String GOOGLE_KEY = "AIzaSyBtt8yaXoRvLTkJHUXrhl5pQaLxomReHIA";
public static final int LOADER_ID = 0;
String jsonResponse = "";
private String address;
private TextView latitudeTextView;
private TextView longitudeTextView;
private TextView summaryTextView;
private TextView tempuratureTextView;
private TextView timezoneTextView;
private TextView textTextView;
private double latitude = 0.0;
private double longitude = 0.0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitudeTextView = (TextView) findViewById(R.id.latitude);
longitudeTextView = (TextView) findViewById(R.id.longitude);
summaryTextView = (TextView) findViewById(R.id.summaryTextView);
tempuratureTextView = (TextView) findViewById(R.id.temperatureTextView);
timezoneTextView = (TextView) findViewById(R.id.timezoneTextView);
textTextView = (TextView) findViewById(R.id.test);
final EditText addressEditText = (EditText) findViewById(R.id.edittext_address);
Button submitButton = (Button) findViewById(R.id.submit_button);
submitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
address = addressEditText.getText().toString().trim();
address = "5121+Paddock+Court+Antioch+Ca+94531";
String fullUrl = GOOGLE_CONVERTER + "?address=" + address + "&key=" + GOOGLE_KEY;
new getlongAndLat().execute(fullUrl);
textTextView.setText(latitude + "");
//Log.e("TAG", latitude + " " + longitude);
// getLoaderManager().initLoader(LOADER_ID, null, MainActivity.this);
}
});
}
@Override
public android.content.Loader<Weather> onCreateLoader(int id, Bundle args) {
return new WeatherAsyncTaskLoader(this, latitude, longitude);
}
@Override
public void onLoadFinished(android.content.Loader<Weather> loader, Weather data) {
}
@Override
public void onLoaderReset(android.content.Loader<Weather> loader) {
}
public class getlongAndLat extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
Log.e("TAG", connection.getResponseCode() + "");
if (connection.getResponseCode() == 200) {
inputStream = connection.getInputStream();
jsonResponse = readFromStream(inputStream);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
//trouble closing input stream
e.printStackTrace();
}
}
}
extractJsonResponse(jsonResponse);
return null;
}
@Override
protected void onPostExecute(String s) {
latitudeTextView.setText(latitude + "");
longitudeTextView.setText(longitude + "");
super.onPostExecute(s);
}
}
private void extractJsonResponse(String jsonResponse) {
try {
JSONObject rootJsonObject = new JSONObject(jsonResponse);
JSONArray nodeResultsArray = rootJsonObject.getJSONArray("results");
JSONObject nodeFirstObject = nodeResultsArray.getJSONObject(0);
JSONObject nodeGeometryObject = nodeFirstObject.getJSONObject("geometry");
JSONObject nodeLocation = nodeGeometryObject.getJSONObject("location");
latitude = nodeLocation.getDouble("lat");
longitude = nodeLocation.getDouble("lng");
} catch (JSONException e) {
e.printStackTrace();
}
}
private String readFromStream(InputStream inputStream) throws IOException{
StringBuilder output = new StringBuilder();
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
BufferedReader reader = new BufferedReader(inputStreamReader);
String line = reader.readLine();
while (line != null) {
output.append(line);
line = reader.readLine();
}
}
return output.toString();
}
}
Parlez-vous de cette ligne 'textTextView.setText (latitude +" ")'? et ici vous obtenez la latitude zéro? –
On ne sait pas où est votre problème, mais un problème potentiel est que vous définissez un champ 'private double' dans un thread, puis le lisez dans un autre thread sans mot clé' volatile' ou 'synchronized' et sans autre barrière de mémoire. Qu'est-ce qui vous fait penser que vous lirez une valeur mise à jour de ce champ plutôt qu'une ancienne valeur provenant d'un registre ou d'une ligne de cache? –