我是改造的新手,我正在使用它而且我已经在活动中直接实现而不是在线程内部,当我开始时它会花费大量时间并阻止用户界面并且在获得响应之后并且当我再次点击时重启活动它等待无限时间。它被困在同样的要求上。
我有一个活动,有三个片段并使用寻呼机,所以首先我在活动中点击web服务并使用回调更新我的Recycler视图适配器
retrofit = new Retrofit.Builder().baseUrl("xyz.co.in/").addConverterFactory(GsonConverterFactory.create()).build();
app = retrofit.create(ApiInterface.class);
Call<List<Pojo>> listCall = app.getDetails();
sb = new StringBuffer("xyz.co.in/api/api.php?api=user&ver=1.0&key=56F353E05208C8E&cmd=OBJECT_GET_LOCATIONS");
sb.append(",");
listCall.enqueue(new Callback<List<Pojo>>() {
@Override
public void onResponse(Response<List<Pojo>> response, Retrofit retrofit) {
final ArrayList<Pojo> list = (ArrayList<Pojo>) response.body();
for (int i = 0; i < list.size(); i++) {
if (i == 0)
sb.append("" + list.get(i).getImei());
else
sb.append(";" + list.get(i).getImei());
}
Ion.with(MainActivity.this).load(sb.toString())
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if (e != null)
Toast.makeText(getApplicationContext(), "Error : " + e.toString(), Toast.LENGTH_SHORT).show();
else {
try {
list_pojo = new ArrayList<>();
address_description = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
JsonObject jsonObject = result.getAsJsonObject(list.get(i).getImei().toString());
list_pojo.add(new Imei_Pogo(jsonObject.get("dt_server").getAsString(), jsonObject.get("dt_tracker").getAsString(), jsonObject.get("lat").getAsDouble(), jsonObject.get("lng").getAsDouble(), jsonObject.get("altitude").getAsString(), jsonObject.get("angle").getAsString(), jsonObject.get("speed").getAsInt()));
geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
addresses = geocoder.getFromLocation( jsonObject.get("lat").getAsDouble(), jsonObject.get("lng").getAsDouble(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
address_description.add(address + "," + city + "," + state + "," + country + "-" + postalCode);
}
// new MyDataBase(getApplicationContext()).insertData(list,list_pojo,address_description);
callback.updateUI(list, list_pojo,address_description);
dialog.dismiss();
} catch (Exception g) {
g.printStackTrace();
}
finally {
if(dialog.isShowing())
dialog.dismiss();
}
}
}
});
}
@Override
public void onFailure(Throwable t) {
Toast.makeText(MainActivity.this, "failed", Toast.LENGTH_SHORT).show();
}
});