skip to Main Content

I am trying to implement Fetch list of data using Retrofit, It’s not working, It shows blank screen, I can’t able to get data, My Json Response is as below

    [
            {
                "code": 1,
                "message": "listed!",
                "totalrecord": 2,
                "result": [
                    {
                        "FieldTypeID": "2",
                        "Name": "Marketing"
                    },
                    {
                        "FieldTypeID": "60",
                        "Name": "Designing"
                    },
                    {
                        "FieldTypeID": "61",
                        "Name": "SEO"
                    },
                    {
                        "FieldTypeID": "78",
                        "Name": "Mobile App Development"
                    }
       ]
    }
]

MainActivity.java

 public class MainActivity extends ListActivity {
        ArrayList<Result> flowerList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint("http://services.hanselandpetal.com").build();
        final RestAdapter restadapter = new RestAdapter.Builder().setEndpoint(BASE_URL).build();

        api flowerapi = restadapter.create(api.class);

        flowerapi.getData(new Callback<ArrayList<Result>>() {
            @Override
            public void success(ArrayList<Result> flowers, Response response) {
                flowerList = flowers;
                Log.d("ArrayList:: :: :::", String.valueOf(flowerList.size()));
                adapter adapt = new adapter(getApplicationContext(), R.layout.item_file, flowerList);
                //ListView listView = (ListView) findViewById(R.id.list);
                setListAdapter(adapt);
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
            }
        });
    }

Interface is api.java

    public interface api {

//    @GET("/feeds/flowers.json")
    @GET("/GetUserGroupList")
    public void getData(Callback<ArrayList<Result>> response);
}

Result.Java

 public class Result {
    @SerializedName("FieldTypeID")
    @Expose
    private String FieldTypeID;
    @SerializedName("Name")
    @Expose
    private String Name;

    /**
     *
     * @return
     * The FieldTypeID
     */
    public String getFieldTypeID() {
        return FieldTypeID;
    }

    /**
     *
     * @param FieldTypeID
     * The FieldTypeID
     */
    public void setFieldTypeID(String FieldTypeID) {
        this.FieldTypeID = FieldTypeID;
    }

    /**
     *
     * @return
     * The Name
     */
    public String getName() {
        return Name;
    }

    /**
     *
     * @param Name
     * The Name
     */
    public void setName(String Name) {
        this.Name = Name;
    }
}

Flower.java

public class Flower {
    @SerializedName("code")
    @Expose
    private Integer code;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("totalrecord")
    @Expose
    private Integer totalrecord;
    @SerializedName("result")
    @Expose
    private List<Result> result = new ArrayList<Result>();

    /**
     *
     * @return
     * The code
     */
    public Integer getCode() {
        return code;
    }

    /**
     *
     * @param code
     * The code
     */
    public void setCode(Integer code) {
        this.code = code;
    }

    /**
     *
     * @return
     * The message
     */
    public String getMessage() {
        return message;
    }

    /**
     *
     * @param message
     * The message
     */
    public void setMessage(String message) {
        this.message = message;
    }

    /**
     *
     * @return
     * The totalrecord
     */
    public Integer getTotalrecord() {
        return totalrecord;
    }

    /**
     *
     * @param totalrecord
     * The totalrecord
     */
    public void setTotalrecord(Integer totalrecord) {
        this.totalrecord = totalrecord;
    }

    /**
     *
     * @return
     * The result
     */
    public List<Result> getResult() {
        return result;
    }

    /**
     *
     * @param result
     * The result
     */
    public void setResult(List<Result> result) {
        this.result = result;
    }

}

Please help me !!!

Log:

04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ <--- HTTP 200 http://203.124.107.14:39/VrinsoftCRM.asmx/GetUserGroupList (242ms)
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Cache-Control: private, max-age=0
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Content-Length: 699
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Server: Microsoft-IIS/7.5
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ X-AspNet-Version: 2.0.50727
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ X-Powered-By: ASP.NET
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ Date: Thu, 14 Apr 2016 09:48:54 GMT
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ OkHttp-Selected-Protocol: http/1.1
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ OkHttp-Sent-Millis: 1460627392316
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ OkHttp-Received-Millis: 1460627392436
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ [{"code":1,"message":"listed!","totalrecord":2,"result":[{"FieldTypeID": "2","Name": "Marketing" },{"FieldTypeID": "60","Name": "Designing" },{"FieldTypeID": "61","Name": "SEO" },{"FieldTypeID": "78","Name": "Mobile App Development" },{"FieldTypeID": "79","Name": ".Net Development" },{"FieldTypeID": "81","Name": "QA" },{"FieldTypeID": "94","Name": "HR" },{"FieldTypeID": "100","Name": "Business Analyst" },{"FieldTypeID": "101","Name": "Business Development Executive" },{"FieldTypeID": "131","Name": "Network Engg" },{"FieldTypeID": "142","Name": "Facebook" },{"FieldTypeID": "156","Name": "PHP Development" },{"FieldTypeID": "157","Name": "Google" },{"FieldTypeID": "158","Name": "Google 2" }]}]
04-14 05:49:52.434    1375-1388/com.example.android.listviewretrofit D/__API__Service﹕ <--- END HTTP (699-byte body)
04-14 05:49:52.434    1375-1375/com.example.android.listviewretrofit D/Suceess :: ::﹕ [ 04-14 05:49:52.434  1375: 1375 D/mListResult size :: :: ]
    1

2

Answers


  1. You need create one helper class

    public class Result{
       public int code;
       public String message;
       public int totalrecord;
       public List<Flower> result;
    }
    

    Then change api method:

    public void getData(Callback<List<Result>> response);
    
    Login or Signup to reply.
  2. Try this code below

    Create class ApiService.class

    public class ApiService {
    private static final String TAG = "__API__Service";
    
    private final api mApiService;
    
    public ApiService(Context context) {
        final OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
        okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                .create();
        RestAdapter restAdapter = new RestAdapter.Builder()
                .setEndpoint(BASE_URL)
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setClient(new OkClient(okHttpClient))
                .setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
                .setLog(new AndroidLog(TAG))
                .setConverter(new CleanGsonConverter(gson))
                .setErrorHandler(new CustomErrorHandler(context))
                .build();
        this.mApiService = restAdapter.create(api.class);
    }
    
       public List<Result> getAllDataFromServer() {
        if (mApiService != null) {
            mApiService.getAllData();
        }
    }
    }
    

    Create CleanGsonConverter.class

    public class CleanGsonConverter extends GsonConverter {
    
    private Gson mGson;
    
    public CleanGsonConverter(Gson gson) {
        super(gson);
        mGson = gson;
    }
    
    public CleanGsonConverter(Gson gson, String encoding) {
        super(gson, encoding);
        mGson = gson;
    }
    
    @Override
    public Object fromBody(TypedInput body, Type type) throws ConversionException {
        boolean willCloseStream = false; // try to close the stream, if there is no exception thrown using tolerant  JsonReader
        try {
            String mDirty = toString(body);
            if (TextUtils.isEmpty(mDirty)) return null;
            String clean = mDirty.replaceAll("(^\(|\)$)", "");
            body = new JsonTypedInput(clean.getBytes(Charset.forName("UTF-8")));
            JsonReader jsonReader = new JsonReader(new InputStreamReader(body.in()));
            jsonReader.setLenient(true);
            Object o = mGson.fromJson(jsonReader, type);
            willCloseStream = true;
            return o;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (willCloseStream) {
                closeStream(body);
            }
        }
    }
    
    private String toString(TypedInput body) {
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        String line;
        try {
            br = new BufferedReader(new InputStreamReader(body.in()));
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
        return sb.toString();
    }
    
    private void closeStream(TypedInput body) {
        try {
            InputStream in = body.in();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    }
    

    Create CustomErrorHandler.class

    public class CustomErrorHandler implements ErrorHandler {
    private final Context mContext;
    
    public CustomErrorHandler(Context context) {
        this.mContext = context;
    }
    
    @Override
    public Throwable handleError(RetrofitError cause) {
        String errorDescription;
    
        if (cause.isNetworkError()) {
            errorDescription = mContext.getString(R.string.error_network);
        } else {
            if (cause.getResponse() == null) {
                errorDescription = mContext.getString(R.string.error_no_response);
            } else {
                // Error message handling - return a simple error to Retrofit handlers..
                try {
                    ErrorResponse errorResponse = (ErrorResponse) cause.getBodyAs(ErrorResponse.class);
                    errorDescription = errorResponse.error.data.message;
                } catch (Exception ex) {
                    try {
                        errorDescription = mContext.getString(R.string.error_network_http_error, cause.getResponse().getStatus());
                    } catch (Exception ex2) {
                        DebugTool.logD("handleError: " + ex2.getLocalizedMessage());
                        errorDescription = mContext.getString(R.string.error_unknown);
                    }
                }
            }
        }
    
        return new Exception(errorDescription);
    }
    
    public class ErrorResponse{
        Error error;
    
        public class Error {
            Data data;
    
            public class Data {
                String message;
            }
        }
    }
    }
    

    Create JsonTypedInput.class

    public class JsonTypedInput implements TypedInput {
    
    private final byte[] mStringBytes;
    
    JsonTypedInput(byte[] stringBytes) {
        this.mStringBytes = stringBytes;
    }
    
    
    @Override
    public String mimeType() {
        return "application/json; charset=UTF-8";
    }
    
    @Override
    public long length() {
        return mStringBytes.length;
    }
    
    @Override
    public InputStream in() throws IOException {
        return new ByteArrayInputStream(mStringBytes);
    }
    
    }
    

    add to gradle.build

    compile 'com.squareup.okhttp:okhttp:2.6.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.google.code.gson:gson:1.7.2'
    

    in MainActivity.class

    public class MainActivity extends ListActivity {
    
    private List<Result> mListResult;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListResult = new ArrayList<Result>();
        new GetDataAsync(this).execute();
    }
    
        private class GetDataAsync extends AsyncTask<Void, Integer, Void> {
        private final WeakReference<SplashActivity> mWeakReference;
        private List<Result> mApiResult;
    
        public GetDataAsync(MainActivity mainActivity) {
            mWeakReference = new WeakReference< MainActivity >(mainActivity);
    
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
        }
    
    
        @Override
        protected Void doInBackground(Void... params) {
            MainActivity activity = mWeakReference.get();
    
            if (activity != null) {
             mApiResult = activity.mApiService.getAllDataFromServer();
            }
    
     }
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
      adapter adapt = new adapter(getApplicationContext(), R.layout.item_file, mListResult);
                setListAdapter(adapt);
     }
     }
    

    class interface

    public interface api {
    @GET("/GetUserGroupList")
    List<Result> getAllData();
    }
    

    You should create customAdapter

    public class CustomAdapter extends ArrayAdapter<Result> {
    private Context mContext;
    private List<Result> mListResult;
    private int saveStateItem = 1;
    
    public CustomAdapter(Context context, int resource, List<Result> objects) {
        super(context, resource, objects);
        this.mContext = context;
        this.mListResult = objects;
    }
    
    
    @Override
    public int getCount() {
        return mListResult.size();
    }
    
    @Override
    public Result getItem(int position) {
        return mListResult.get(position);
    }
    
    @Override
    public View getView(final int position, View view, final ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.item_menu, null);
            holder = new ViewHolder();
            holder.mType = (TextView) view.findViewById(R.id.textview_type);
            holder.mName = (TextView) view.findViewById(R.id.textview_name);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
    
        Result cate = mListResult.get(position);
        holder.mType.setText(cate.getFieldTypeID());
        holder.mName.setText(cate.getName());
    
    
        return view;
    }
    
    public class ViewHolder {
        public TextView mType;
        public TextView mName;
    }
    }
    

    in MainActivity

    public class MainActivity extends Activity
    

    in activity_main.xml. You must change id of list view

    <ListView
    android:id="@+id/listview"
    >
    

    and in method onPostExecute

      @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
      CustomAdapter adapt = new CustomAdapter(getApplicationContext(), R.layout.item_file, mListResult);
                yourListView.setAdapter(adapt);
     }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search