我有一个web api,它给了我一系列合作伙伴,它看起来像这样:
[
"partner1",
"partner2",
"partner3",
"....",
"parner222"
]
我有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 {
@SerializedName("name")
@Expose
private List<String> name = new ArrayList<String>();
public List<String> getName() {
return name;
}
public void setName(List<String> name) {
this.name = name;
}
}
这是我的界面
public interface APIService {
@GET("Partners")
Call<Partners> getPartners();
}
这是我的api url的APIHelper
public class APIHelper {
public static APIService apiService;
public static APIService getApiService() {
if (apiService == null) {
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://part-oflink.domain.com/partners.json/")
.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) {
//here I need to implement that on click downloads me data
// and save it into my Partners table
}
});
@Override
public void onResponse(Call<Partners> call, Response<Partners> response) {
//here I'm trying to put api response into array list
if (response.body() != null) {
ArrayList<String> partnersList = new ArrayList<>();
partnersList = response.body();
}
}
@Override
public void onFailure(Call<Partners> call, Throwable t) {
}
}
现在我卡住了。我想实现onClick Button方法从API获取数据。在onResponse()方法中,我试图将数据放入ArrayList以检查数据是否已收到。而且我想将这些数据保存到我的表伙伴中。
如果有人可以帮助我或指导我解决这个问题,我将不胜感激。这是我第一次使用改造和api。
有人可以帮助我或指导我成功从API获取数据并将其保存到表格合作伙伴中吗?
答案 0 :(得分:1)
您尝试解析JSON字符串(合作伙伴数组)的方式不合适。你的JSON应该是这样的:
{
"partners":
["partner1", "partner2", "partner3", ...]
}
POJO模型类应该是:
class Partners{
private List<String> partners;
public Partners(){}
public void setList(List<String> partners) {
this.partners = partners;
}
public List<String> getList() {
return this.partners;
}
//setter and getter methods
}
答案 1 :(得分:0)
如果响应不为空,则打印值:
for(Partner partner: response){
Log.d("Partner Name",partner.name);
}
如果您正在为数据库使用任何ORM,请调用其DAO并传递值以保存在数据库中。