0

I am building an app in android, where first activity does an Rest api call and shows the result on the screen.

To do the web-service call,i can use asynctask or loader or Handlers . Services won't be good option as it's not a long-running operation and it is tied to the activity life cycle.

I can use libraries like volley and robospice. But, before using the libraries, i would like to know the efficient approach.

What to use aysncTask? If it's not recommended because of issues like restarting when screen orientation. Then, when it should be used?

Or Loaders would be the best approach, as it takes cares of all issues?

I am new to android.

Any suggestions would be appreciated.

Ritt
  • 3,181
  • 3
  • 22
  • 51
  • 1
    Use the libraries. Volley and Retrofit (http://square.github.io/retrofit/) are the main candidates. – Budius Oct 19 '15 at 20:13
  • 1
    Also, do not use AsyncTask as it is a very outdated code and gives bad results. The general approach is to just have all those stuff outside the activity and have a way for it to "connect" to the data source. Here is a very good video on the topic if you want to learn more: https://www.youtube.com/watch?v=xHXn3Kg2IQE – Budius Oct 19 '15 at 20:19
  • Thank you @Budius , thankx for the video. After, going though some docs, i think loaders would be an efficient way, for this task. or loaders with volley? – Ritt Oct 19 '15 at 20:28

2 Answers2

0

Read about Google SyncFramework.

With it you can use Volley for data fetch and in your fragment/activity you can use Cursor with Loaders for data refresh/fetch.

Kenan Begić
  • 1,228
  • 11
  • 21
0
void IncomeJsonData(String json) {

    try {
        //JSONObject jsonObject = new JSONObject(json);
        JSONArray array = new JSONArray(json);

        for (int i = 0; i < array.length(); i++)
        {

            JSONObject obj = array.getJSONObject(i);
            Income income = new Income();
            income.setId_id(obj.getInt("Id_id"));
            income.setIn_addamount(obj.getDouble("In_addamount"));
            income.setIn_amtdate(obj.getString("In_amtdate"));
            income.setIn_amtcat(obj.getString("In_amtcat"));
            income.setIn_mode(obj.getString("In_mode"));
            income.setIn_description(obj.getString("In_description"));
            income.setIn_active(obj.getBoolean("In_active"));


            Log.e("BAVO", income.toString());

            income_list.add(income);

        }
        Log.e("C", income_list.toString());
        custom_class = new Custom_class(ViewIncomeActivity.this, income_list);
        lst_income.setAdapter(custom_class);
        //lst_income.setBackgroundResource(R.color.colorPrimaryDark);

    } catch (Exception ex) {
        Log.e("Show_Error", ex.toString());
        ex.printStackTrace();
    }
}
class ConsumeIncomeWebservice extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        String jsonString = new String();
        HttpURLConnection urlConnection = null;
        URL url = null;

        try {
            String url1 = "http://192.168.43.126/Android_webservices/Pocket_expence.svc/GetAllIncome";
            URI uri = new URI(url1);
            url = new URL(uri.toString());
            urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestMethod("GET");

            //urlConnection.getResponseCode("GET");
            urlConnection.setReadTimeout(10000 /* milliseconds */);
            urlConnection.setConnectTimeout(15000 /* milliseconds */);

            urlConnection.setDoOutput(true);

            urlConnection.connect();

            Log.e("Error_error", "Hal Ne bhai");

            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));

            char[] buffer = new char[1024];


            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();

            jsonString = sb.toString();
            Log.d("", "doInBackground: " + jsonString);

        }

        catch (Exception e) {
            Log.d("Error", e.toString());
        }
        return jsonString;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);

        Log.e("Post_Exe", "PostExe");
        if (s != null && !s.equals("")) {
            IncomeJsonData(s);
        } else {
            Log.e("JSON_amit", "NOT AVAILABLE");
        }
    }
}
Tom
  • 4,257
  • 6
  • 33
  • 49
Amit Jesani
  • 155
  • 2
  • 9
  • Please add some context as to why the code you've provided solves the OP's issue. – Tom Apr 28 '17 at 10:21