Retrofit如何实现同步
同步请求服务器数据,应该很常用吧。
比如登陆 验证登陆。要写到一个方法里,而不是直接在Activity里写吧。
public static Boolean Validate()
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://test9.**.com")
.addConverterFactory(retrofit2.converter.gson.GsonConverterFactory.create()).build();
ApiService apiService = retrofit.create(ApiService.class);
Call<HashMap<String,String>> call = apiService.getIpInfo();
try
{
HashMap hmap= call.execute().body();
returnStr=hmap.get("Success").toString();
}
catch (Exception ex)
{
returnStr=ex.getMessage();
}
}
总是提示 android.os.NetworkOnMainThreadException