我正在开发一个Android应用。我有这种形式,当单击按钮时,它执行一个称为insertData()的方法。在此函数内部,我使用另一个函数getAddress()更改了一个名为“ address”的局部变量。
在函数getAddress中,成功更改了局部变量“地址”。但是,当我们输入函数insertData()时,变量“ address”仍然包含旧数据,在这种情况下为“ Location Unknown”。
我在这里没看到什么?
insertData()
public String address = "Location unknown";
public boolean insertData(String title, String description, String image, Double longitude, Double latitude, String date) {
String location = Double.toString(longitude) + "," + Double.toString(latitude);
getAddress(location);
Log.d("TAG2", address); // Still "Location unknown"
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, title);
contentValues.put(COL_3, description);
contentValues.put(COL_4, image);
contentValues.put(COL_5, longitude);
contentValues.put(COL_6, latitude);
contentValues.put(COL_7, date);
contentValues.put(COL_8, address);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
getAddress()
public void getAddress(String coordination) {
String token = "{removed for a reason haha}";
String url = "https://api.mapbox.com/geocoding/v5/mapbox.places/" + coordination + ".json?access_token=" + token;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.isSuccessful()) {
String data = response.body().string();
JsonParser parser = new JsonParser();
try {
JsonObject obj = (JsonObject) parser.parse(data);
JsonArray arr = obj.getAsJsonArray("features");
JsonObject objj = arr.get(2).getAsJsonObject();
String place = objj.get("place_name").getAsString();
address = place;
Log.d("TAG", address); // Gives an address
}
catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
Logcat调试器
2019-06-15 20:12:58.968 31465-31465/com.example.triptracker D/TAG2: Location unkown
2019-06-15 20:12:59.269 31465-31569/com.example.triptracker D/TAG: Mountain View, California, United States
我无法将onResponse()方法更改为返回String。我不知道为什么,但这是一个只读方法。
getAddress {changed}
public void getAddress(String coordination) {
String token = "{empty}";
String url = "https://api.mapbox.com/geocoding/v5/mapbox.places/" + coordination + ".json?access_token=" + token;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
try (Response response = client.newCall(request).execute()) {
String data = response.body().string();
JsonParser parser = new JsonParser();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
JsonObject obj = (JsonObject) parser.parse(data);
JsonArray arr = obj.getAsJsonArray("features");
JsonObject objj = arr.get(2).getAsJsonObject();
String place = objj.get("place_name").getAsString();
address = place;
Log.d("TAG", address);
}
catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:2)
getAddress()
异步完成其工作。到getAddress()
返回时,您的HTTP请求将尚未完成,因此address
尚未更改。
由于insertData()
正在执行磁盘I / O,因此您应该在其自己的后台线程上调用insertData()
,因此在磁盘I / O正在进行时,不要冻结UI。如果是这种情况,则可以使用execute()
代替enqueue()
同步使用OkHttp。这样您就可以让getAddress()
返回地址,以便将其插入数据库中。