我正在尝试从web api获取数据,但是出了点问题,我无法理解。
我的JSON看起来像这样
{
"partners":
[
"partner1",
"partner2",
"partner3",
... ,
"partner150"
]
}
我有Table合作伙伴(ActiveAndroid),我希望从api中保存所有合作伙伴。
@Table(name = "Partners")
public class Partners extends Model {
@Column(name = "Name")
String name;
public Partners() {}
public Partners(String name) {
this.name = name;
}
}
这是我的Pojo模型类:
public class Partners extends ArrayList<String> {
@SerializedName("partners")
@Expose
public List<String> partners = new ArrayList<String>();
public List<String> getPartners() {
return partners;
}
public void setName(List<String> partners) {
this.partners = partners;
}
}
这是我的界面
public interface APIService {
@GET("Partners")
Call<Partners> getPartners();
}
这是我的APIHelper类
public class APIHelper {
public static final String PARTNERS_URL = "https://part-of-link.com/partners.json/";
public static APIService apiService;
public static APIService getApiService() {
if (apiService == null) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(PARTNERS_URL)
.addConverterFactory(GsonConverterFactory.create()).build();
apiService = retrofit.create(APIService.class);
}
return apiService;
}
}
这就是Fragment,我有一个Button,我想在其上实现onClick方法,从API获取数据并将其保存到Partners表中。
public class DownloadMain extends Fragment implements Callback<Partners> {
private Button dloadPartners;
private Call<Partners> callPartners;
public static APIService apiService;
public DownloadMain() {}
public DownloadMain newInstance() { return new DownloadMain(); }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.download_main, container, false);
dloadPartners = (Button) view.findViewById(R.id.downloadPartners);
dloadPartners.setOnClickListener(btnListener);
callPartners = APIHelper.getApiService().getPartners();
callPartners.enqueue(this);
return view;
}
Button.OnClickListener btnListener = (new View.OnClickListener() {
@Override
public void onClick(View v) {
APIHelper helper = new APIHelper();
apiService = helper.getApiService();
Call<Partners> call = apiService.getPartners();
call.enqueue(new Callback<Partners>() {
@Override
public void onResponse(Call<Partners> call, Response<Partners> response) {
APIHelper helper = new APIHelper();
helper.getApiService();
Call<Partners> call = apiService.getPartners();
call.enqueue(new Callback<Partners>() {
@Override
public void onResponse(Call<Partners> call, Response<Partners> response) {
List<String> partners = response.body().getPartners();
Log.d(TAG, "Number of partners received: " + partners.size());
/*if (response.body() != null) {
ArrayList<String> partnersList;
partnersList = response.body();
Log.d("DOWNLOAD", String.valueOf(partnersList));
}*/
}
@Override
public void onFailure(Call<Partners> call, Throwable t) {
Toast.makeText(getActivity(), "FAIL!!!", Toast.LENGTH_SHORT).show();
}
});
}
});
@Override
public void onResponse(Call<Partners> call, Response<Partners> response) {
}
@Override
public void onFailure(Call<Partners> call, Throwable t) {
}
}
这是我的问题。一切正常,没有错误但是当我点击按钮时没有任何反应。
所以我认为我犯了一些错误,但我无法弄清楚(第一次做改造)。
点击按钮时我想要的是从网络上获取所有合作伙伴并将其保存到我的合作伙伴表中。
问题:有人可以指导我并告诉我有什么问题并帮我解决这个问题吗?
编辑:
Button.OnClickListener btnListener = (new View.OnClickListener() {
@Override
public void onClick(View v) {
APIHelper helper = new APIHelper();
apiService = helper.getApiService();
Call<ArrayList<String>> call = null;
call.enqueue(new Callback<ArrayList<String>>() {
@Override
public void onResponse(Call<ArrayList<String>> call, Response<ArrayList<String>> response) {
ArrayList<String> partners = response.body();
Log.d(TAG, "Number of partners received: " + partners.size());
}
@Override
public void onFailure(Call<ArrayList<String>> call, Throwable t) {
Toast.makeText(getActivity(), "FAIL!!!", Toast.LENGTH_SHORT).show();
}
});
}
});
错误日志:
错误:(68,60)错误:不是抽象而不是 覆盖抽象方法 onResponse(Call&gt;,Response&gt;)in 回调
错误:(72,53)错误:找不到符号方法体()
错误:(69,17)错误:方法不会覆盖或实现方法 来自超类
错误:任务':app:compileDebugJavaWithJavac'执行失败。
编译失败;有关详细信息,请参阅编译器错误输出。
答案 0 :(得分:2)
你的问题出现在onClick()
中helper.getApiService();
您没有将服务分配给变量 把它改成这个:
apiService = helper.getApiService();
修改强>
根据新的更新,我认为你不需要使用POJO
只需在Call
Call<ArrayList<String>>
并获得回复只需使用response.body()
而不使用.partners()
。
尝试在代码中的任何位置替换它并检查
答案 1 :(得分:0)
根据您的JSON响应,您的POJO类应为:
public class Partners {
private ArrayList<String> partners = new ArrayList<>(0);
// Get; Set methods //
}
答案 2 :(得分:0)
尝试将其放入您的APIHelper类中,以获取有关您要发送的内容的更多信息
public static APIService apiService;
//this http loggin interceptor allows you debug requests and responses
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
// set your desired log level
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// add your other interceptors …
// add logging as last interceptor
httpClient.addInterceptor(logging); // <-- this is the important line!
public static APIService getApiService() {
if (apiService == null) {
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
}
return apiService;
}
也许您以错误的方式发送信息...... 请记住,在服务器端,您必须解码接收的信息(使用json_decode)。