Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating an app which upload user data to the database as well as read the all user data. I have two urls, the "http://192.168.10.7:3000/api/v1/services/read/" is for only reading existing data, and "http://192.168.10.7:3000/api/v1/services/create/" is only for creating data on my database. When i run an app now, i can only either read or create data with only one url. I want to perform both operations on my app by exectuing two urls at the same time. How can i do this?
public class CloudRequestTask<T> extends Task<T> {

  CloudRequest object;
  String Url;

  public CloudRequestTask(CloudRequest object, String url) {
      this.object = object;
      Url = "http://192.168.10.7:3000/api/v1/services/read/";
  }

  @Override
  public void run() {

      StringRequest request= new StringRequest(Request.Method.POST,Url, new Response.Listener<String>() {
          @Override
          public void onResponse(String response) {
              TaskResult result=new TaskResult();
              try{
                  result=TaskResult.parseFrom(response);
                  if(result==null)
                      throw new Exception("Error parsing response from server");
              }catch (Exception e)
              {
                  result=new TaskResult();
                  result.setSuccess(false);
                  result.setMessage(e.getMessage());
              }finally {
                  fireOnComplete(result);
              }

          }
      },



       new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {
              TaskResult result=new TaskResult();
              result.setSuccess(false);
              try{
                  if(error instanceof TimeoutError)
                  {
                      result.setMessage("Request Timed out. Server unavailable or busy");
                  }
                  else
                  {
                      String reponse=new String(error.networkResponse.data);
                      result=TaskResult.parseFrom(reponse);
                  }


              }catch (Exception e)
              {
                  result.setMessage(e.getMessage());
              }
              finally {
                  fireOnComplete(result);
              }

          }
      }){
          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
              HashMap<String, String> headers = new HashMap<String, String>();
              headers.put("Content-Type", "application/json; charset=utf-8");
              headers.put("device_fingerprint",HelpingHandsCloudApp.getInstance().getThisDevice().toJSON());
              return headers;
          }

          @Override
          public byte[] getBody() {
              return new Gson().toJson(object).getBytes();
          }
      };
      VolleySingletonQueue.getInstance(HelpingHandsCloudApp.getInstance().getmContext()).addToRequestQueue(request);
  }

}


What I have tried:

I have tried executing read request only after write request is executed successfully. But i want them to be executed at the same time
Posted
Updated 29-Jul-21 17:50pm
v2
Comments
David Crow 30-Jul-21 8:27am    
" I want to perform both operations on my app by exectuing two urls at the same time. How can i do this?"

Create two separate connections, each in its own thread.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900