繁体   English   中英

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

[英]getting empty response in Recycleview using 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"
      }
   ]
}

模型类:

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;
    }
}

响应:

  public class JSONResponse {
    private Rvdata[] content;

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

接口接口:

   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());
}

主类:

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 ,其中此代码运行良好。

无论如何,您必须将尾部斜杠添加到您的 baseUrl 中,否则 Retrofit 将调用https://apps.eduportal-pk.comEduPo...这对于 ZE6B0031A8D8B65855 来说不是一个好的结构。 我也试过在 postman 中调用你的 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 代码,您应该将其删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM