我有一个方法可以进行api调用并解析响应。我在onResponse方法中得到当前天气的整数值。我想将此值设置为textview但是在onResponse方法值之后不保留。我把这个变量也变成了全局变量。这个类扩展了片段,所以我在onViewCreated方法中设置了值。
这是带日志的方法代码......
public class HomeScreen extends Fragment{
int currentWeather = 0;
onViewCreated(){
curWeather = ... initialisation.
curWeather.setText(""+getCurrentWeather);
}
public int getCurrentWeather(){
OkHttpClient okHttpClient = new OkHttpClient();
Request re = new Request.Builder().url(forecastURL).build();
Call call = okHttpClient.newCall(re);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String jsonResponse = response.body().string();
JSONObject forecast;
JSONObject currently;
try {
forecast = new JSONObject(jsonResponse);
currently = forecast.getJSONObject("currently");
currentTempFaron = (int) Math.round(currently.getDouble("temperature"));
currentTempCelc = (currentTempFaron - 32) * 5 / 9;
Log.d("weather in try", "" + currentTempCelc); // has weather value
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("weather in response", "" + currentTempCelc); // again has weather value.
}
});
return currentTempCelc; //nothing it has at this point after coming out of response.
}
}
答案 0 :(得分:1)
在onResponse()
之后Log.d("weather in response", "" + currentTempCelc);
内执行此操作:
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
curWeather.setText(Integer.toString(currentTempCelc));
}
});
这里的问题是你试图从后台线程更新UI线程,这是不允许的。您可以使用runOnUiThread
来实现此目的。
答案 1 :(得分:0)
使用Handler
更新onResponse
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
curWeather.setText(Integer.toString(currentTempCelc));
}
}
});
答案 2 :(得分:0)
在curWeather
中创建Fragment
成员变量。然后在setText
方法中的curWeather
上致电onResponse
。
public class HomeScreen extends Fragment{
int currentWeather = 0;
final TextView curWeather
onViewCreated(){
curWeather = ... initialisation.
// curWeather.setText(""+getCurrentWeather);
}
public int getCurrentWeather(){
OkHttpClient okHttpClient = new OkHttpClient();
Request re = new Request.Builder().url(forecastURL).build();
Call call = okHttpClient.newCall(re);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String jsonResponse = response.body().string();
JSONObject forecast;
JSONObject currently;
try {
forecast = new JSONObject(jsonResponse);
currently = forecast.getJSONObject("currently");
currentTempFaron = (int) Math.round(currently.getDouble("temperature"));
currentTempCelc = (currentTempFaron - 32) * 5 / 9;
Log.d("weather in try", "" + currentTempCelc); // has weather value
curWeather.setText(""+currentTempCelc);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("weather in response", "" + currentTempCelc); // again has weather value.
}
});
return currentTempCelc; //nothing it has at this point after coming out of response.
}
答案 3 :(得分:0)
您可以使用方法 getCurrentWeather()传递活动上下文。然后您可以访问TextView并设置TextView。我尝试了这个解决方案,它对我有用。