I want use Volley instead of Asynctask

SoundCloud clone for Android and Web
androidguy
Posts: 52
Joined: Tue Aug 25, 2015 11:28 am

I want use Volley instead of Asynctask

Postby androidguy » Tue Aug 25, 2015 12:05 pm

Hi,

In Soundstar project i want use Volley library suggested by Google instead of Asynctask.guide me how convert Asynctask to volley library?

Sharan
Posts: 15
Joined: Mon Aug 24, 2015 2:05 pm

Re: I want use Volley instead of Asynctask

Postby Sharan » Tue Aug 25, 2015 3:48 pm

Follow below steps
  1. Download volley.jar from this link:http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
  2. add volley.jar to libs folder of soundstar project in android studio.
  3. add this statement compile files('libs/volley.jar') to build.gradle of soundstar
    ex:dependencies {
    compile files('libs/volley.jar')
    }
  4. below showing an example of converting asynctask to volley

    Code: Select all


    public class Unfollow extends AsyncTask<String, Void, Void> {
       String res_ult, msg;

       @Override
       protected void onPreExecute() {
          super.onPreExecute();

       }

       @Override
       protected Void doInBackground(String... params) {
          sendSoundToserver(params[0], params[1]);



          if (res_ult != null) {
             try {
                JSONObject job = new JSONObject(res_ult);
                msg = job.getString("message");
             } catch (JSONException e) {
                e.printStackTrace();
             }
          }
          return null;
       }

       @Override
       protected void onPostExecute(Void result) {
          super.onPostExecute(result);

       }

       public String sendSoundToserver(String follow_id, String user_id) {

          try {
             HttpClient httpClient = SingupActivity
                   .getHttpsClient(new DefaultHttpClient());
             HttpPost postRequest = new HttpPost(Constant.unfollowurl);

             MultipartEntity reqEntity = new MultipartEntity(
                   HttpMultipartMode.STRICT);
             if (!follow_id.isEmpty()) {

                // Path of the file to be uploaded
                reqEntity.addPart("user_id", new StringBody(follow_id));
                reqEntity.addPart("follower_id", new StringBody(user_id));
             }

             postRequest.setEntity(reqEntity);
             HttpResponse response = httpClient.execute(postRequest);

             BufferedReader reader = new BufferedReader(new InputStreamReader(
                   response.getEntity().getContent(), "UTF-8"));
             String sResponse;
             StringBuilder s = new StringBuilder();

             while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
             }

             res_ult = s.toString();
             Log.d("parmit", "this is response of sound upload" + res_ult);
          } catch (ClientProtocolException e) {
             e.printStackTrace();
          } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
          } catch (IllegalStateException e) {
             e.printStackTrace();
          } catch (IOException e) {
             e.printStackTrace();
          }

          return res_ult;
       }

    }


  5. below is code for volley

    Code: Select all




            RequestQueue queue = Volley.newRequestQueue(getActivity());
            StringRequest stringRequest=new StringRequest(Request.Method.POST, Constant.unfollowurl , new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject job = new JSONObject(response);
                   
                   msg = job.getString("message");
                       
                    }

                    catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    progrsBar.dismiss();

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }){
                @Override

                protected Map<String,String> getParams(){
                    Map<String,String> params = new HashMap<String, String>();
                    params.put("user_id",params[0]r);
                    params.put("follower_id",params[1]);
                    return params;
                }
            };
            queue.add(stringRequest);


Return to “SoundStar”

Who is online

Users browsing this forum: No registered users and 1 guest