在没有 RXJava 的 OKHTTP 网络调用上更新 UI
Update UI on OKHTTP network call without RXJava
我在模型 class 中进行网络调用。在 runOnUIThread
中,我希望能够更新 UI,但我不想将对 UI 中任何内容的引用传递到我的网络 class ].我怎样才能更新 UI,而不给网络 class 一个对 UI class 的引用,并且不使用像 RXJava 这样的库?
这是我正在使用的代码。
public class 天气网络 {
public CurrentWeather getDailyWeather(float lat, float lng) {
String URL = "";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
final String passingData = jsonData;
if(response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
});
}
});
}
} catch(JSONException e) {
Log.d(ACTIVITY + " JSONEXCEPTION", e.getMessage());
} catch(IOException e){
Log.d(ACTIVITY + " IOEXCEPTION", e.getMessage());
}
}
});
return new CurrentWeather();
}
}
您可以将 Callback
作为方法参数传递。
public void getDailyWeather(float lat, float lng, Callback callback) {
当你排队时,给它。
client.newCall(request).enqueue(callback);
return; // This method is done now. Make it void
// You cannot return from a async method
}
然后,在别处,
float lat = 0;
float lng = 0;
api.getDailyWeather(lat, lng, new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
final String passingData = jsonData;
if(response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// This is still necessary
// But you can now access Views from your UI class
}
});
}
} catch(JSONException e) {
Log.d(ACTIVITY + " JSONEXCEPTION", e.getMessage());
} catch(IOException e){
Log.d(ACTIVITY + " IOEXCEPTION", e.getMessage());
}
}
});
如果你想清理它,你可以定义自己的回调,例如
public interface CurrentWeatherCallback {
void onWeatherDataReceived(CurrentWeather weather);
}
然后保留现有的,但使用另一个回调
final CurrentWeather weather = new CurrentWeather();
// TODO: Parse JSON
runOnUiThread(new Runnable() {
@Override
public void run() {
if (callback != null) callback.onWeatherDataReceived(weather);
}
});
无论哪种方式,您传递的都是接口,而不是对任何直接 UI 元素的引用
我在模型 class 中进行网络调用。在 runOnUIThread
中,我希望能够更新 UI,但我不想将对 UI 中任何内容的引用传递到我的网络 class ].我怎样才能更新 UI,而不给网络 class 一个对 UI class 的引用,并且不使用像 RXJava 这样的库?
这是我正在使用的代码。
public class 天气网络 {
public CurrentWeather getDailyWeather(float lat, float lng) {
String URL = "";
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
final String passingData = jsonData;
if(response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
});
}
});
}
} catch(JSONException e) {
Log.d(ACTIVITY + " JSONEXCEPTION", e.getMessage());
} catch(IOException e){
Log.d(ACTIVITY + " IOEXCEPTION", e.getMessage());
}
}
});
return new CurrentWeather();
}
}
您可以将 Callback
作为方法参数传递。
public void getDailyWeather(float lat, float lng, Callback callback) {
当你排队时,给它。
client.newCall(request).enqueue(callback);
return; // This method is done now. Make it void
// You cannot return from a async method
}
然后,在别处,
float lat = 0;
float lng = 0;
api.getDailyWeather(lat, lng, new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
final String passingData = jsonData;
if(response.isSuccessful()) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// This is still necessary
// But you can now access Views from your UI class
}
});
}
} catch(JSONException e) {
Log.d(ACTIVITY + " JSONEXCEPTION", e.getMessage());
} catch(IOException e){
Log.d(ACTIVITY + " IOEXCEPTION", e.getMessage());
}
}
});
如果你想清理它,你可以定义自己的回调,例如
public interface CurrentWeatherCallback {
void onWeatherDataReceived(CurrentWeather weather);
}
然后保留现有的,但使用另一个回调
final CurrentWeather weather = new CurrentWeather();
// TODO: Parse JSON
runOnUiThread(new Runnable() {
@Override
public void run() {
if (callback != null) callback.onWeatherDataReceived(weather);
}
});
无论哪种方式,您传递的都是接口,而不是对任何直接 UI 元素的引用