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

标签 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"
      }
   ]
}

模型类:

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

Api接口(interface):

   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... 这对于 URL 来说不是一个好的结构。我也尝试在 postman 中调用你的网址,我得到这样的东西:

<!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 代码,您应该将其删除。

关于java - 使用 Retrofit2 在 Recycleview 中得到空响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61122020/

相关文章:

java - Jax-RS - 获取 header 值的自定义属性

java - 使用 Intellij 运行时 Desktop.getDesktop() 会使 JVM 崩溃

json - Angular JS。将表单字段转换为 json

json - dart,是否可以将功能反序列化到json/从json反序列化?

java - 有没有更好的方法在 java 中压缩大文件?

java - 使用 Selenium 按以字母开头的 id 过滤 DOM 元素

php - 在mysql上用foreach显示数据数组

android - 单击按钮 10 秒后如何使 imageView 可见?

java - Android项目构建失败: Java class duplication

javascript - 在 Javascript 中添加数组对象中对象的值