1

I using retrofit to display data but data is not getting displayed.i don't know where am I getting wrong.in logcat im getting response as:

2019-12-26 20:34:39.847 28305-28305/com.example.androidtutorial E/helloash: [com.example.androidtutorial.GetAheadFolder.JavaDatum@36ebcd9]

I need help..thanks

GetAHeadModelsnext:

public class GetAHeadModelsnext{

@SerializedName("id")
@Expose
private String id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("java_data")
@Expose
private List<JavaDatum> java_data ;
@SerializedName("xml_data")
@Expose
private List<XmlDatum> xmlData;

public GetAHeadModelsnext(String id, String title, List<JavaDatum> java_data, List<XmlDatum> xmlData) {
    this.id = id;
    this.title = title;
    this.java_data = java_data;
    this.xmlData = xmlData;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public List<JavaDatum> getJavaData() {
    return java_data;
}

public void setJavaData(List<JavaDatum> javaData) {
    this.java_data = javaData;
}

public List<XmlDatum> getXmlData() {
    return xmlData;
}

public void setXmlData(List<XmlDatum> xmlData) {
    this.xmlData = xmlData;
}

submodel(ListJavaDatum):

public class JavaDatum {


@SerializedName("file_name")
@Expose
private String fileName;
@SerializedName("code")
@Expose
private String code;
public JavaDatum(String fileName, String code) {
    this.fileName = fileName;
    this.code = code;
}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public String getCode() {
    return code;
}

public void setCode(String code) {
    this.code = code;
}}

interface:

public interface DescriptService {
@GET("/v1/android_tutorials/single_advance?")
Call<GetAHeadModelsnext> getaheadjava(@Query("advance_id") String id);}

activity:

 @Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    /*Create handle for the RetrofitInstance interface*/
    progressDialog = new ProgressDialog(getContext());
    progressDialog.setMessage("Loading....");
    progressDialog.show();
    Intent intent = getActivity().getIntent();

    String id = intent.getStringExtra("idGHnext");
    Log.e("ashwiniiii", String.valueOf(id));
    GetAheadApiService service = GetAheadApiClient.getRetrofitInstance().create(GetAheadApiService.class);
    Call<GetAHeadModelsnext> call = service.getaheadjava(id);
    call.enqueue(new Callback<GetAHeadModelsnext>() {
        @Override
        public void onResponse(Call<GetAHeadModelsnext> call, Response<GetAHeadModelsnext> response) {
            progressDialog.dismiss();

            List<JavaDatum> retro=response.body().getJavaData();
            generateDataList(retro);
            Log.e("helloash", String.valueOf(response.body().getJavaData()));
        }

        @Override
        public void onFailure(Call<GetAHeadModelsnext> call, Throwable t) {
            progressDialog.dismiss();

            Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
        }
    });
}
private void generateDataList(List<JavaDatum> photoList) {
    recyclerView = getView().findViewById(R.id.nextGHrecycle);
    LinearLayoutManager manager = new LinearLayoutManager(getContext());
    recyclerView.setLayoutManager(manager);
    recyclerView.setHasFixedSize(true);
    adapter = new JavaGetAheadAdapter(getContext(),photoList);
    recyclerView.setAdapter(adapter);
}}

ADapter:

public class JavaGetAheadAdapter extends RecyclerView.Adapter<JavaGetAheadAdapter.CustomViewHolder> {

List<JavaDatum> GHmdel;
Context context;

public JavaGetAheadAdapter(Context context,List<JavaDatum> employees) {
    this.GHmdel = employees;
    this.context = context;
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.getaheadnext_item, parent, false);

    return new CustomViewHolder(itemView);
}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    holder.employeeName.setText(GHmdel.get(position).getFileName());//
    holder.textView.setText(GHmdel.get(position).getCode());
    Log.d("adapter",GHmdel.get(position).getFileName());
    Log.d("adapter2",GHmdel.get(position).getCode());

}

@Override
public int getItemCount() {
    return GHmdel.size();
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
    public TextView employeeName;
    TextView textView;

    public CustomViewHolder(View view) {
        super(view);
        employeeName = (TextView) view.findViewById(R.id.detailsgetaheadtitle);
       textView = view.findViewById(R.id.detailsgetahead);}}}

my json :

{
"id": "1",
"title": "ViewPager and TabHost",
"java_data": [
    {
        "file_name": "ListDisplay.java",
        "code": "package com.example.ListDisplay;\r\n\r\nimport android.os.Bundle;\r\nimport android.app.Activity;\r\nimport android.view.Menu;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ListView;\r\n\r\npublic class ListDisplay extends Activity {\r\n   // Array of strings...\r\n   String[] mobileArray = {\"Android\",\"IPhone\",\"WindowsMobile\",\"Blackberry\",\r\n      \"WebOS\",\"Ubuntu\",\"Windows7\",\"Max OS X\"};\r\n   \r\n   @Override\r\n   protected void onCreate(Bundle savedInstanceState) {\r\n      super.onCreate(savedInstanceState);\r\n      setContentView(R.layout.activity_main);\r\n      \r\n      ArrayAdapter adapter = new ArrayAdapter<String>(this, \r\n         R.layout.activity_listview, mobileArray);\r\n      \r\n      ListView listView = (ListView) findViewById(R.id.mobile_list);\r\n      listView.setAdapter(adapter);\r\n   }\r\n}"
    }
],
"xml_data": [
    {
        "file_name": "activity_main.xml",
        "code": "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\r\n   xmlns:tools=\"http://schemas.android.com/tools\"\r\n   android:layout_width=\"match_parent\"\r\n   android:layout_height=\"match_parent\"\r\n   android:orientation=\"vertical\"\r\n   tools:context=\".ListActivity\" >\r\n\r\n   <ListView\r\n      android:id=\"@+id/mobile_list\"\r\n      android:layout_width=\"match_parent\"\r\n      android:layout_height=\"wrap_content\" >\r\n   </ListView>\r\n \r\n</LinearLayout>\r\nimport android.os.Bundle;\r\nimport android.app.Activity;\r\nimport android.view.Menu;\r\nimport android.widget.ArrayAdapter;\r\nimport android.widget.ListView;\r\n\r\npublic class ListDisplay extends Activity {\r\n   // Array of strings...\r\n   String[] mobileArray = {\"Android\",\"IPhone\",\"WindowsMobile\",\"Blackberry\",\r\n      \"WebOS\",\"Ubuntu\",\"Windows7\",\"Max OS X\"};\r\n   \r\n   @Override\r\n   protected void onCreate(Bundle savedInstanceState) {\r\n      super.onCreate(savedInstanceState);\r\n      setContentView(R.layout.activity_main);\r\n      \r\n      ArrayAdapter adapter = new ArrayAdapter<String>(this, \r\n         R.layout.activity_listview, mobileArray);\r\n      \r\n      ListView listView = (ListView) findViewById(R.id.mobile_list);\r\n      listView.setAdapter(adapter);\r\n   }\r\n}"
    },
    {
        "file_name": "strings.xml",
        "code": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<resources>\r\n   <string name=\"app_name\">ListDisplay</string>\r\n   <string name=\"action_settings\">Settings</string>\r\n</resources>"
    },
    {
        "file_name": "activity_listview.xml",
        "code": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<!--  Single List Item Design -->\r\n\r\n<TextView xmlns:android=\"http://schemas.android.com/apk/res/android\"\r\n   android:id=\"@+id/label\"\r\n   android:layout_width=\"fill_parent\"\r\n   android:layout_height=\"fill_parent\"\r\n   android:padding=\"10dip\"\r\n   android:textSize=\"16dip\"\r\n   android:textStyle=\"bold\" >\r\n</TextView>"
    }
]

}

Wini
  • 1,906
  • 1
  • 12
  • 31
  • okay, code seems to be right, have you debugged if the data is coming from api for sure. – vikas kumar Dec 26 '19 at 15:13
  • I have a put a log at onresponse " Log.e("helloash", String.valueOf(response.body().getJavaData())); " got response as **2019-12-26 20:34:39.847 28305-28305/com.example.androidtutorial E/helloash: [com.example.androidtutorial.GetAheadFolder.JavaDatum@36ebcd9]** – Wini Dec 26 '19 at 15:16
  • can you post ur json structure, the list is not null seems but have to make sure the data is there is so try to log the size of the list as well – vikas kumar Dec 26 '19 at 15:26
  • I have put log under adapter...under bindviewholder..im not getting anything..in logcat...that means have I made mistake in adapter? – Wini Dec 26 '19 at 16:17
  • most probably let's see, im suggesting some code below as answer lets apply and see – vikas kumar Dec 26 '19 at 16:18
  • Can you try removing `?` from end of the `@GET("/v1/android_tutorials/single_advance?")` ? – shafayat hossain Dec 27 '19 at 04:26
  • @shafayathossain..still doesn't worked.. – Wini Dec 27 '19 at 06:40
  • @shafayathossain...got any solution? – Wini Dec 27 '19 at 11:16
  • Try to print your json response and list using this two line `Log.e("reponse",new Gson().toJson(response.body()));` and `Log.e("list",new Gson().toJson(retro));` – Rahul sharma Dec 27 '19 at 11:26
  • @frankenstein..im getting response in logcat as " E/reponse: {"id":"10","java_data":[{"code":"","file_name":""}],"title":"List View","xml_data":[{"code":"","file_name":""}]}" and E/list: [{"code":"","file_name":""}]....then why am I not getting data displayed? – Wini Dec 27 '19 at 11:41
  • `"file_name":"" ` this string contain the information or this are just like this mean empty ? – Rahul sharma Dec 27 '19 at 12:04
  • my api has data ..but in logcat im like this only "file_name":"" – Wini Dec 27 '19 at 12:06
  • this mean you are not getting information from api . Try to call same get request from Postman or resttest . – Rahul sharma Dec 27 '19 at 12:09
  • @frankenstein..I put url in postman..am getting response in there...see I have edited json data above from postman – Wini Dec 27 '19 at 12:11
  • Try to check what is your list size using this `Log.e("list",new Gson().toJson(retro.size))` – Rahul sharma Dec 27 '19 at 12:20
  • getting "E/list: 1" in logcat – Wini Dec 27 '19 at 12:23
  • I add my answer check it. – Rahul sharma Dec 27 '19 at 12:37

3 Answers3

1

Try to remove / before v1 in your interface GET method and add that into your base url. In short in any method of retrofit interface don't start with /.

haresh
  • 1,424
  • 2
  • 12
  • 18
  • I removed / before v1...still doesn't worked..got any other solution? – Wini Dec 26 '19 at 15:23
  • I have put log under adapter...under bindviewholder..im not getting anything..in logcat – Wini Dec 26 '19 at 16:02
  • hello haresh.. can you help me out over here --> https://stackoverflow.com/questions/59532667/particular-titlefetched-from-api-using-searchview – Wini Jan 02 '20 at 14:50
1

Assuming data is there try making below changes

public class JavaGetAheadAdapter extends RecyclerView.Adapter<JavaGetAheadAdapter.CustomViewHolder> {

List<JavaDatum> GHmdel = new ArrayList(); //new change
Context context;

//removed the list from constructor
public JavaGetAheadAdapter(Context context) {
    this.GHmdel = employees;
}

//new method for updating the data items
public void updateData(List<JavaDatum> employees){
  this.GHmdel.addAll(employees);
  notifyDataSetChanged();
}

@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.getaheadnext_item, parent, false);

    return new CustomViewHolder(itemView);
}

@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
    holder.employeeName.setText(GHmdel.get(position).getFileName());//
    holder.textView.setText(GHmdel.get(position).getCode());
    Log.d("adapter",GHmdel.get(position).getFileName());
    Log.d("adapter2",GHmdel.get(position).getCode());

}

@Override
public int getItemCount() {
    return GHmdel.size();
}

public class CustomViewHolder extends RecyclerView.ViewHolder {
    public TextView employeeName;
    TextView textView;

    public CustomViewHolder(View view) {
        super(view);
        employeeName = (TextView) view.findViewById(R.id.detailsgetaheadtitle);
       textView = view.findViewById(R.id.detailsgetahead);}}}

At the calling side just make the mentioned below changes.

 @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        /*Create handle for the RetrofitInstance interface*/
        progressDialog = new ProgressDialog(getContext());
        progressDialog.setMessage("Loading....");
        progressDialog.show();
        Intent intent = getActivity().getIntent();

        String id = intent.getStringExtra("idGHnext");
        Log.e("ashwiniiii", String.valueOf(id));

        // new changes
        generateDataList(view);

        GetAheadApiService service = GetAheadApiClient.getRetrofitInstance().create(GetAheadApiService.class);
        Call<GetAHeadModelsnext> call = service.getaheadjava(id);
        call.enqueue(new Callback<GetAHeadModelsnext>() {
            @Override
            public void onResponse(Call<GetAHeadModelsnext> call, Response<GetAHeadModelsnext> response) {
                progressDialog.dismiss();

                List<JavaDatum> retro=response.body().getJavaData();
                //new changes
                adapter.updateData(retro);
                Log.e("helloash", String.valueOf(response.body().getJavaData()));
            }

            @Override
            public void onFailure(Call<GetAHeadModelsnext> call, Throwable t) {
                progressDialog.dismiss();

                Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
            }
        });
    }
    //new changes goes here
    private void generateDataList(View view) {
        recyclerView = view.findViewById(R.id.nextGHrecycle);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        recyclerView.setLayoutManager(manager);
        recyclerView.setHasFixedSize(true);
        adapter = new JavaGetAheadAdapter(getContext());
        recyclerView.setAdapter(adapter);
    }}

Try and comment below if its working or not still.

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • I will try and notify you tomorrow.. Okay? – Wini Dec 26 '19 at 16:49
  • still doesnt ..I applied your solution but still doesnt worked – Wini Dec 27 '19 at 06:46
  • do you have any other solutions? – Wini Dec 27 '19 at 11:17
  • if possible create a sample project with the issue i will look into it because either the data is missing or some condition and what ever you posted seems correct. let me know if you can – vikas kumar Dec 27 '19 at 11:19
  • actually the thing is im getting data in id=1 (which is present in down at the end of the list)...at id=10 (top of the list)the data was absent that's why im getting blank...blunder mistake I have done..it was nobody's mistake ..it is the mistake of the api provider (my institute)..they put data in id=1,2...rest kept blank...which I didn't noticed – Wini Dec 27 '19 at 12:55
  • hello can you help me out here --> https://stackoverflow.com/questions/63071359/error-response-500-is-not-getting-diaplayed-when-password-and-confirm-password-t – Wini Jul 24 '20 at 11:35
1

Might be i got the problem. Problem is here @GET("/v1/android_tutorials/single_advance?") you have? in the end of your url and you also have @Query("advance_id") field that mean your request contain two ?. you request currently look like this.

/v1/android_tutorials/single_advance??advance_id="xxxx"

Remember when you add @Query("advance_id") to your request this by default add ? to your your. So remove? from your url end. Your url should look like this

 @GET("/v1/android_tutorials/single_advance")
Rahul sharma
  • 1,492
  • 12
  • 26
  • I have done that also ...removed ? but still no response – Wini Dec 27 '19 at 12:38
  • use this `Log.e("Url", String.valueOf(call.request().url()));` to print your final your .Add this just after where you declare your call. – Rahul sharma Dec 27 '19 at 12:42
  • hey problem solved..I found mistake ..thanks for your time and effort – Wini Dec 27 '19 at 12:46
  • What was the mistake ? – Rahul sharma Dec 27 '19 at 12:48
  • actually the thing is im getting data in id=1 (which is present in down at the end of the list)...at id=10 (top of the list)the data was absent that's why im getting blank...blunder mistake I have done..it was nobody's mistake ..it is the mistake of the api provider (my institute)..they put data in id=1,2...rest kept blank...which I didn't noticed – Wini Dec 27 '19 at 12:56
  • ok always try use `use this Log.e("Url", String.valueOf(call.request().url()));` this will reduce your error. And accept the answer. – Rahul sharma Dec 27 '19 at 12:57
  • hey can you help me over here --> https://stackoverflow.com/questions/63071359/error-response-500-is-not-getting-diaplayed-when-password-and-confirm-password-t – Wini Jul 24 '20 at 10:15