使用Retrofit2在Recycleview中得到空响应?

时间:2020-04-09 13:14:31

标签: java android json android-recyclerview retrofit2

下面是我的JSON

{
   "content":[
      {
         "client":" Al Majid Science Academy",
         "city":"Bahawalpur"
      },
      {
         "client":" ENLIGHTEN CHILDREN ACADEMY ",
         "city":"Sukkur"
      },
      {
         "client":" M.U.H Model High School for Boys & Girls",
         "city":"Lahore"
      },
      {
         "client":"Al Mukhtar Public School",
         "city":"Shakargarh"
      },
      {
         "client":"Al-GHAZZI EVENING COLLEGE",
         "city":"Lahore"
      },
      {
         "client":"Al-Huda School System",
         "city":"Tandlianwala"
      },
      {
         "client":"Al-MUSLIM PUBLIC SCHOOL Garhi Sherdad",
         "city":"Peshawar"
      },
      {
         "client":"Al-Mussaver Public School Bua",
         "city":"Shakargarh"
      },
      {
         "client":"Al-Nadwa Academy Of Science Kamoke",
         "city":"Kamoke"
      },
      {
         "client":"Allied School Defense View Campus",
         "city":"Lahore"
      },
      {
         "client":"Allied School Malakwal Campus",
         "city":"Mandi Bahauddin"
      },
      {
         "client":"Best International School System",
         "city":"Arifwala"
      },
      {
         "client":"Bright Era Institute Of Sciences ",
         "city":"Gujranwala"
      },
      {
         "client":"Career School System",
         "city":"Rawalpindi"
      },
      {
         "client":"Central Science School",
         "city":"Shakargarh"
      },
      {
         "client":"CompuTech Institute (NoorKot Road Campus)",
         "city":"Shakargarh"
      },
      {
         "client":"CompuTech Institute of Computer & Technology",
         "city":"Shakargarh"
      },
      {
         "client":"CompuTech School System",
         "city":"Shakargarh"
      },
      {
         "client":"Crescent Public High School & College Nushki",
         "city":"Nushki"
      },
      {
         "client":"Divisional Model Public High School ",
         "city":"Ahmadpur East"
      },
      {
         "client":"Education For All Children School System",
         "city":"Daska"
      },
      {
         "client":"Faiz-e-Lasani Public Model School Hadnal",
         "city":"Shakargarh"
      },
      {
         "client":"Fusion College of Sciences",
         "city":"Shakargarh"
      },
      {
         "client":"Govt Post Graduate College",
         "city":"Shakargarh"
      },
      {
         "client":"IIUI Schools Jauharabad",
         "city":"Jauharabad"
      },
      {
         "client":"IIUI Schools Okara Campus",
         "city":"Okara"
      },
      {
         "client":"IIUI Schools Shakargarh",
         "city":"Shakargarh"
      },
      {
         "client":"INNOVATOR SCIENCE ACADEMY PHILOURA",
         "city":"Zafarwal"
      },
      {
         "client":"IQRA School & College Daggar",
         "city":"Buner"
      },
      {
         "client":"KBL English Academy",
         "city":"Shakargarh"
      },
      {
         "client":"KBL Science & English Academy",
         "city":"Shakargarh"
      },
      {
         "client":"Khaliqia Educational Complex",
         "city":"Shakargarh"
      },
      {
         "client":"Kids Learning Castle",
         "city":"Muzaffarabad"
      },
      {
         "client":"Kids Learning School System",
         "city":"Shakargarh"
      },
      {
         "client":"Kids Public School",
         "city":"Shakargarh"
      },
      {
         "client":"Kindergarten School System",
         "city":"Nowshera"
      },
      {
         "client":"LifeLine Model School",
         "city":"Shakargarh"
      },
      {
         "client":"Little Champs School Kot Addu",
         "city":"Multan"
      },
      {
         "client":"Little Groomers School Tarnab",
         "city":"Peshawar"
      },
      {
         "client":"Mentor Nursery (School For Kids)",
         "city":"Kamoke"
      },
      {
         "client":"Minhaj ul Hussain Higher Secondary School",
         "city":"Shakargarh"
      },
      {
         "client":"Muslim Grammar School",
         "city":"Karachi"
      },
      {
         "client":"NDS School",
         "city":"Lahore"
      },
      {
         "client":"New Concept Public School Chamal",
         "city":"Shakargarh"
      },
      {
         "client":"Oasis Foundation School",
         "city":"Shakargarh"
      },
      {
         "client":"PMG School Noorkot",
         "city":"Sharda"
      },
      {
         "client":"Raj Public school Mian channu",
         "city":"Mian Channu"
      },
      {
         "client":"Royal Public School (RPS)",
         "city":"Shakargarh"
      },
      {
         "client":"Sardar Grammar School ",
         "city":"Gujranwala"
      },
      {
         "client":"SHAKARGARH PUBLIC SCHOOL",
         "city":"Shakargarh"
      },
      {
         "client":"Shine Star Educational Complex Bagga",
         "city":"Shakargarh"
      },
      {
         "client":"Spirit ECE School",
         "city":"Shakargarh"
      },
      {
         "client":"Super Career Group of Schools",
         "city":"Shakargarh"
      },
      {
         "client":"Swat Education Complex",
         "city":"Swat"
      },
      {
         "client":"Tabinda Public Model Higher Secondary School",
         "city":"Shakargarh"
      },
      {
         "client":"Tameer -e- Nau School Abbottabad",
         "city":"Abbottabad"
      },
      {
         "client":"The Alpine Science School ",
         "city":"Haroonabad"
      },
      {
         "client":"The Eastern Grammar School",
         "city":"Shakargarh"
      },
      {
         "client":"The Garrison Science Secondary School",
         "city":"Multan"
      },
      {
         "client":"The Kids Paradise School",
         "city":"Gujranwala"
      },
      {
         "client":"The Nation School System Jhang",
         "city":"Jhang"
      },
      {
         "client":"The National School",
         "city":"Gujranwala"
      },
      {
         "client":"The New Horizon School Shakargarh",
         "city":"Shakargarh"
      },
      {
         "client":"The Scholars College of Sciences Commerce ZWL",
         "city":"Zafarwal"
      },
      {
         "client":"The Vision School & Islamic Vision Girls College",
         "city":"Hyderabad"
      },
      {
         "client":"The Wisdom School",
         "city":"Shakargarh"
      },
      {
         "client":"Umm Al-Qura Islamic School",
         "city":"Okara"
      }
   ]
}

ModelClass:

public class Rvdata {


    private String client;
    private String city;

    public Rvdata()

    {

    }

    public Rvdata(String client, String city){
        this.client=client;
        this.city=city;
    }

    public String getClient() {
        return client;
    }

    public void setClient(String client) {
        this.client = client;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

JsonResponse:

  public class JSONResponse {
    private Rvdata[] content;

    public Rvdata[] getContent(){
        return content;
    }
}

ApiInterface:

   public interface RequestInterface {
    @GET("EduPortal/Production/apis/services.php?call=our_clients")
    Call<JSONResponse> getJSON();
}

适配器:

public class RvAdapter extends RecyclerView.Adapter<RvAdapter.ViewHolder> {

private ArrayList<Rvdata> content;

public RvAdapter(ArrayList<Rvdata> content) {
    this.content = content;
}

@Override
public RvAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_one,parent,false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    holder.tv_name.setText(content.get(position).getClient());
    holder.tv_version.setText(content.get(position).getCity());
}

MainClass:

private void loadJSON() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("https://apps.eduportal-pk.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RequestInterface request = retrofit.create(RequestInterface.class);
    Call<JSONResponse> call = request.getJSON();
    call.enqueue(new Callback<JSONResponse>() {
        @Override
        public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {
            JSONResponse jsonResponse = response.body();
            data = new ArrayList<>(Arrays.asList(jsonResponse.getContent()));
            adapter = new RvAdapter(data);
            recyclerView.setAdapter(adapter);
        }

        @Override
        public void onFailure(Call<JSONResponse> call, Throwable t) {
            Log.d("Error", t.getMessage());
        }
    });
}

我是新来的,被卡住了。

注意:我还检查了一些其他JSON,这些代码在这些JSON中均能很好地工作。

1 个答案:

答案 0 :(得分:0)

无论如何,您都必须在baseUrl中添加斜杠,否则Retrofit将调用https://apps.eduportal-pk.comEduPo...,这对于URL来说不是一个很好的结构。另外,我尝试用邮递员呼叫您的URL,我得到这样的声音:

<!DOCTYPE html>
<html>

<head>
    <title>Developer Services</title>
</head>

<body>

</body>

</html>


{"content":[{"client":" Al Majid Science Academy","city":"Bahawalpur"},{"client":" ENLIGHTEN CHILDREN ACADEMY
","city":"Sukkur"},{"client":" M.U.H Model High School for Boys & Girls","city":"Lahore"},{"client":"Al Mukhtar Public
School","city":"Shakargarh"},{"client":"Al-GHAZZI EVENING COLLEGE","city":"Lahore"},{"client":"Al-Huda School
System","cit
...

您的响应不是完整的JSON,之前有一些HTML代码,应将其删除。