gpt4 book ai didi

java - Android 无法对服务器进行 GET 改造 API 调用

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:07 24 4
gpt4 key购买 nike

我正在开发一个 Android 项目。我正在使用改造来调用 API。因此,我正在执行 API 调用来获取与证书关联的类(class)列表。

来自服务器 get (BASE_URL/certificates/1) 的响应是:

{
"id": 1,
"name": "Arts-certificate",
"authorized_by": "priya",
"created_by": "self",
"created_by_id": "1",
"description": "awarded to the best",
"doc_url": "http://f8rentals.com/wp-content/uploads/2015/03/IMG_0067ft.jpg?189db0",
"category_id": "2",
"cateogory": "Arts",
"courses": [
{
"id": 1,
"course_id": "CRSXQHGZ0SCTLhklhk6JL",
"number_of_sessions": 1,
"created_at": "2015-12-16T07:10:37.000+00:00",
"updated_at": "2015-12-16T07:10:37.000+00:00",
"tags": null
},
{
"id": 1,
"course_id": "CRSXQHGZ0SCTLhklhk6JL",
"number_of_sessions": 1,
"created_at": "2015-12-16T07:10:37.000+00:00",
"updated_at": "2015-12-16T07:10:37.000+00:00",
"tags": null
}
.
.
.
.
]
}

我有证书列表,所需的操作是,单击证书后,应该转到新 fragment ,其中包含与该证书关联的所有类(class)(单击该证书)。为此,我进行了上述 API 调用。

问题是,当我单击列表中的第一个证书时,它会转到下一个 fragment 并列出所有类(class)。但是当我单击任何其他证书时,它会显示空白 fragment 。当我调试代码时,在 fetchCourses() CoursesListOfCertificatesFragment.java 中,调用 onFailure() 函数。

工作流程是CertificateFragment是列出所有证书的类。单击时,CertificateFragment 中的 CertificateClickListner() 被触发,并依次启动 CoursesListOfCertificatesFragment 并在此 fragment 的构造函数中传递证书对象。在这里,我进行 API 调用 GET (BASE_URL/certificates/1)。

这是在 CoursesListOfCertificatesFragment 的 fetchCourses() 中。但除了第一个证书外,都会调用 onFailure() 方法。

感谢任何帮助......如果需要任何其他信息,请询问。

谢谢。

下面列出了相关的类:

CoursesListOfCertificatesFragment.java

package com.localjini.learner.fragments;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.localjini.learner.R;
import com.localjini.learner.adapters.CourseListingRVAdapter;
import com.localjini.learner.api.RestClient;
import com.localjini.learner.main.HomeActivity;
import com.localjini.learner.models.Certificates;
import com.localjini.learner.models.Course;
import com.localjini.learner.models.CoursesResponse;
import com.localjini.learner.models.GetCertificateResponse;
import com.localjini.learner.utils.LogUtils;
import com.localjini.learner.utils.UIHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit.Call;
import retrofit.Callback;
import retrofit.Response;
import retrofit.Retrofit;


public class CoursesListOfCertificatesFragment extends BaseFragment {

@Bind(R.id.tv_category)
TextView tvCategory;
@Bind(R.id.tv_sort_filter)
TextView tvSortFilter;
@Bind(R.id.pb_course_listing)
ProgressBar pbCourseListing;
@Bind(R.id.rv_course_listing)
RecyclerView rvCourseListing;

public UIHelper uiHelper;
List<Course> courses;

private Certificates certificate;
private int certificate_id;

public CoursesListOfCertificatesFragment(){

}

private CourseListingRVAdapter.CourseClickListener listener = new CourseListingRVAdapter.CourseClickListener() {

@Override
public void onCourseClicked(Course course) {
interactionListener.launchCourseDetailFragment();
}
};

public CoursesListOfCertificatesFragment(Certificates certificate){
this.certificate = certificate;
this.certificate_id = certificate.getId();
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (root == null) {
root = (ViewGroup) inflater.inflate(R.layout.fragment_course_listing, container, false);

ButterKnife.bind(this, root);

initInstances();

fetchCourses();
}


return root;
}

private void initInstances() {
uiHelper = new UIHelper(getActivity());

((HomeActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((HomeActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((HomeActivity) getActivity()).getSupportActionBar().setTitle("");

rvCourseListing.setItemAnimator(new DefaultItemAnimator());
rvCourseListing.setLayoutManager(new LinearLayoutManager(getActivity()));
rvCourseListing.setHasFixedSize(true);
}

private void fetchCourses() {
Call<GetCertificateResponse> call = RestClient.getCMSServiceInstance().getCertificateById(certificate_id);
call.enqueue(new Callback<GetCertificateResponse>() {
@Override
public void onResponse(Response<GetCertificateResponse> response, Retrofit retrofit) {

if (response != null) {
GetCertificateResponse resp = response.body();

if (resp != null) {
showCourses(resp.getCourses());
} else {
uiHelper.showToast("response contain null data");
}
}
}

@Override
public void onFailure(Throwable t) {
LogUtils.d("coursesFailed", t.getMessage());
}
});

}

private void showCourses(List<Course> courses) {
if(courses != null) {
CourseListingRVAdapter courseListingRVAdapter = new CourseListingRVAdapter(getActivity(), courses);
courseListingRVAdapter.setCourseClickListener(listener);
rvCourseListing.setAdapter(courseListingRVAdapter);
}
}

public FragmentId getFragmentId() {
return FragmentId.COURSE_LISTING_FRAGMENT;
}

}

证书 fragment .java

public class CertificateFragment extends BaseFragment {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
//protected ViewGroup root;
protected Context context;
@Bind(R.id.rv_Certificates) RecyclerView rvCertificates;

private CertificateRVAdapter.CertificateClickListener listener = new CertificateRVAdapter.CertificateClickListener() {

@Override
public void onCertificateClicked(Certificates certificate) {
Toast.makeText(getActivity(), certificate.getName() +" Clicked", Toast.LENGTH_SHORT).show();
//interactionListener.launchCourseListingFragment();
interactionListener.launchCoursesListOfCertificatesFragment(certificate);
}
};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

if(root==null) {

root = (ViewGroup) inflater.inflate(R.layout.fragment_certificate, container, false);
ButterKnife.bind(this, root);

initInstances();
getCertificates();
}
return root;
}
// rvCourseListing.setItemAnimator(new DefaultItemAnimator());
// rvCourseListing.setLayoutManager(new LinearLayoutManager(getActivity()));
// rvCourseListing.setHasFixedSize(true);
//

private void initInstances() {
//uiHelper = new UIHelper(getActivity());

((HomeActivity) getActivity()).getSupportActionBar().setHomeButtonEnabled(true);
((HomeActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
((HomeActivity) getActivity()).getSupportActionBar().setTitle("");

rvCertificates.setItemAnimator(new DefaultItemAnimator());
rvCertificates.setLayoutManager(new LinearLayoutManager(getActivity()));
rvCertificates.setHasFixedSize(true);
}

private void getCertificates() {

Call<AllCertificateResponse> call = RestClient.getCMSServiceInstance().getCertificates();

call.enqueue(new Callback<AllCertificateResponse>() {
@Override
public void onResponse(Response<AllCertificateResponse> response, Retrofit retrofit) {
if (response.isSuccess()) {
//com.squareup.okhttp.Response raw = response.body().raw();

List<Certificates> certificates = response.body().getCertificates();
if (certificates != null)
showCertificates(certificates);

Toast.makeText(getActivity(), "Success in getting certificates", Toast.LENGTH_SHORT).show();
} else {
Log.d("CERT", "response is failure");
}
}

@Override
public void onFailure(Throwable t) {
Toast.makeText(getActivity(), "Failure", Toast.LENGTH_SHORT).show();
}

});
}

public void showCertificates(List<Certificates> certificates){

CertificateRVAdapter adapter = new CertificateRVAdapter(getActivity(), certificates);
adapter.setCertificateClickListener(listener);
rvCertificates.setAdapter(adapter);
}

public FragmentId getFragmentId() {
return FragmentId.COURSE_LISTING_FRAGMENT;
}

}

最佳答案

我解决了这个问题。结果服务器给出了错误的响应(仍处于测试阶段)。我期待的是字符串数组。对于第一个证书,所有类(class)都有字符串数组,但对于第二个证书,所有类(class)都有字符串值而不是数组。

关于java - Android 无法对服务器进行 GET 改造 API 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34554147/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com