Ошибка модификации - java.lang.IllegalArgumentException
Ссылка URL: http://www.factsplanet.info/cities.php Ошибка модификации метода: java.lang.IllegalArgumentException
Основной класс
public class MainActivity extends AppCompatActivity {
Retrofit retrofit;
Factory service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
retrofit = new Retrofit.Builder()
.baseUrl("https://factsplanet.info/")
.build();
service = retrofit.create(Factory.class);
Call<ThisIsPojo> call = service.getnames();
call.enqueue(new Callback<ThisIsPojo>() {
@Override
public void onResponse(Response<ThisIsPojo> response, Retrofit retrofit) {
Toast.makeText(MainActivity.this, response.body().getData().get(1).getCountry(), Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Throwable t) {
}
});
}
}
Класс интерфейса:
public interface Factory {
@GET ("http://www.factsplanet.info/cities.php")
Call<ThisIsPojo> getnames();
}
POJO CLASS:
открытый класс ThisIsPojo {
private List<Datum> data = null;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public List<Datum> getData() {
return data;
}
public void setData(List<Datum> data) {
this.data = data;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
Говорит ошибка:java.lang.RuntimeException: Невозможно начать деятельность
java.lang.IllegalArgumentException: невозможно создать конвертер для класса app.com.alphaapps.android.suntest.pojo.ThisIsPojo для метода Factory.getnames
2 ответа
Решение
Изменить это так:
retrofit = new Retrofit.Builder().baseUrl("https://factsplanet.info/").addConverterFactory(GsonConverterFactory.create()).build();
Здравствуйте, попробуйте этот способ, если это может помочь
Сервисный интерфейс
public interface ServiceInterface {
@GET(HttpConstants.CITIESJSON)
Call<GeonameResponse>getCities(@Query("north")double north, @Query("south")double south, @Query("east")double east, @Query("west")double west, @Query("lang")String lang, @Query("username")String username);
@GET(HttpConstants.USERDATAJSON)
Call<ListData>taskData(@Query("method")String method,@Query("stdID")int stdID);
@POST(HttpConstants.USERDATAJSON)
Call<ListData> saveUser(@Body DataPojo pojo);
}
ServiceClass
public class ServiceClass {
static ServiceInterface serviceInterface;
// public static final String baseUrl= HttpConstants.BASE_URL_GEONAME;
public static final String baseUrl= HttpConstants.baseUrl;
public static ServiceInterface connection(){
if(serviceInterface==null){
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient();
client.interceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Response response=chain.proceed(chain.request());
return response;
}
});
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseUrl)
.build();
serviceInterface=retrofit.create(ServiceInterface.class);
}
return serviceInterface;
}
}
Реализация
public void addUser(DataPojo pojo){
ServiceInterface serviceInterface=ServiceClass.connection();
Call<ListData> call=serviceInterface.saveUser(pojo);
call.enqueue(new Callback<ListData>() {
@Override
public void onResponse(Response<ListData> response, Retrofit retrofit) {
Log.v("@@@Response",""+response.toString());
if(response.isSuccess()){
Toast.makeText(AddNewUser.this,"User Added",Toast.LENGTH_LONG).show();
supportFinishAfterTransition();
}
}
@Override
public void onFailure(Throwable t) {
Log.v("@@@Failure"," Message"+t.getMessage());
}
});
}
Для полного примера, пожалуйста, посетите ссылку ниже https://github.com/pratikvyas1991/NetworkingExample/tree/master/app/src/main/java/com/ims/tasol/networkingexample