网址链接: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
创建转换器答案 0 :(得分:1)
像这样修改:
retrofit = new Retrofit.Builder().baseUrl("https://factsplanet.info/").addConverterFactory(GsonConverterFactory.create()).build();
答案 1 :(得分:0)
你好尝试这种方式,如果它可以帮助
服务界面
<!Doctype HTML>
<html>
<body>
Enter Value: <input type="number" id='rCircle'> 
<button onclick="calcAreaCircle()">Calculate</button>
<br/>
<br/>
<span id="circleArea"></span>
<script>
var calcAreaCircle = function(){
var circle = document.getElementById("rCircle"),
circleArea = document.getElementById("circleArea");
radius = parseInt(circle.value,10);
console.log(parseInt(circle.value,10));
console.log(typeof radius+": "+radius);
var circleResult = Math.PI * Math.pow(radius, 2);
circleArea.innerHTML = ("The area is: " + circleResult)
};
</script>
</body>
</html>
<强>服务类强>
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;
}
}