英文:
getting empty response in Recycleview using Retrofit2?
问题
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());
}
});
}
Please note that the provided code snippets are just translations of the given code, and they might not include any functional changes or optimizations. If you're facing issues with your code, make sure to review it thoroughly for any errors or inconsistencies specific to your development environment and requirements.
英文:
Below is my 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();
}
Adapter:
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());
}
});
}
I'm new and got stuck.
Note: I also checked some other JSON in which this code works perfectly.
答案1
得分: 0
你必须在baseUrl后面添加斜杠,否则Retrofit会调用 https://apps.eduportal-pk.comEduPo...
,这不是一个良好的URL结构。另外,我已经尝试在Postman中调用您的URL,结果如下:
{
"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":...
您的响应不是完整的JSON,之前有一些HTML代码,您应该将其删除。
英文:
You have to add the trailing slash to your baseUrl anyway otherwise Retrofit will call https://apps.eduportal-pk.comEduPo...
and that's not a good structure for an URL. Also I've tried calling your URL in postman and I get soemthing like this:
<!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
...
Your response is not full JSON, there is some HTML code before, you should remove it.
专注分享java语言的经验与见解,让所有开发者获益!
评论