改造错误 - java.lang.IllegalArgumentException
Retrofit Error - java.lang.IllegalArgumentException
URL Link : http://www.factsplanet.info/cities.php
方法改造
错误:java.lang.IllegalArgumentException
主要Class
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) {
}
});
}
}
接口Class:
public interface Factory {
@GET ("http://www.factsplanet.info/cities.php")
Call<ThisIsPojo> getnames();
}
POJO CLASS:
public 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: 无法启动 activity
java.lang.IllegalArgumentException: 无法为方法 class app.com.alphaapps.android.suntest.pojo.ThisIsPojo 创建转换器 Factory.getnames
修改如下:
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);
}
服务类
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());
}
});
}
完整示例请访问下方link
https://github.com/pratikvyas1991/NetworkingExample/tree/master/app/src/main/java/com/ims/tasol/networkingexample
URL Link : http://www.factsplanet.info/cities.php 方法改造 错误:java.lang.IllegalArgumentException
主要Class
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) {
}
});
}
}
接口Class:
public interface Factory {
@GET ("http://www.factsplanet.info/cities.php")
Call<ThisIsPojo> getnames();
}
POJO CLASS:
public 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: 无法启动 activity
java.lang.IllegalArgumentException: 无法为方法 class app.com.alphaapps.android.suntest.pojo.ThisIsPojo 创建转换器 Factory.getnames
修改如下:
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);
}
服务类
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());
}
});
}
完整示例请访问下方link https://github.com/pratikvyas1991/NetworkingExample/tree/master/app/src/main/java/com/ims/tasol/networkingexample