title: Uploading data to server using JSON
body: <p>I'm trying to upload multiple images to the server (PHP back end) from android phone , what I'm trying to do is that i want my function to call it self when it returns success on response but with different data , so i want to move to the next object in my array list if the first object was uploaded successfully . I'm currently calling the function using for loop which is uploading multiple images at the same time which will consume a lot of connection on 3g so i prefer to upload them one by one and retry in case i lost connection or failed to upload .
  please any one who would help me i would be thankful</p>
  
  <p>that is my code </p>
  
  <pre><code>private void uploaddata() {
      // Tag used to cancel the request
      String tag_string_req = "req_register";
  
      pDialog.setMessage("uploading to server ...");
      showDialog();
  
      StringRequest strReq = new StringRequest(Request.Method.POST,
              AppConfig.URL_Uploadfirstparty, new Response.Listener&lt;String&gt;() {
  
          @Override
          public void onResponse(String response) {
              Log.d(TAG, "Register Response: " + response.toString());
              hideDialog();
  
              try {
                  JSONObject jObj = new JSONObject(response);
                  boolean error = jObj.getBoolean("error");
                  if (!error) {
                      String accidentid="";
                      if(acc.getAccid()==null)
                      {
                           accidentid = jObj.getString("recordid");
                          Toast.makeText(getApplicationContext(),
                                  "accident"+accidentid, Toast.LENGTH_LONG).show();
                      }
                      else
                      {
                           accidentid = acc.getAccid();
                      }
  
  
  
                      String thid="0";
                      for(int i=0;i&lt;10;i++)
                      {
                          if(acc.p[i]!=null)
                          {
                              uploadimages(imgstring(acc.p[i].getPhoto()),acc.p[i].getPosition(),Integer.parseInt(accidentid),acc.p[i].getComment(),thid);
  
                          }
                      }
  
  
  
                  } else {
  
                      // Error occurred in registration. Get the error
                      // message
                      String errorMsg = jObj.getString("error_msg");
                      Toast.makeText(getApplicationContext(),
                              errorMsg, Toast.LENGTH_LONG).show();
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
          }
      }, new Response.ErrorListener() {
  
          @Override
          public void onErrorResponse(VolleyError error) {
              Log.e(TAG, "Registration Error: " + error.getMessage());
              Toast.makeText(getApplicationContext(),
                      error.getMessage(), Toast.LENGTH_LONG).show();
              hideDialog();
          }
      }) {
  
          @Override
          protected Map&lt;String, String&gt; getParams() {
              // Posting params to register url
              Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
              params.put("carid", Integer.toString(acc.getCarid()));
              params.put("expertid",Integer.toString(acc.getExpertid()));
              params.put("longtitude",Double.toString(acc.getLongtitude()));
              params.put("latitude",Double.toString(acc.getLatitude()));
              params.put("injuries",Integer.toString(acc.getInjuries()));
              params.put("location",acc.getLocation());
              params.put("time",acc.getTime());
              params.put("accid",acc.getAccid());
              return params;
          }
  
      };
  
      // Adding request to request queue
      AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
      strReq.setRetryPolicy(new DefaultRetryPolicy(
              30000,
              DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
              DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  }
  
  
  private void uploadimage(final String image,final String thid,final int pos,final int id) {
      // Tag used to cancel the request
      String tag_string_req = "req_register";
  
      pDialog.setMessage("uploading images to server ...");
      showDialog();
  
      StringRequest strReq = new StringRequest(Request.Method.POST,
              AppConfig.URL_Uploadfirstpartyphoto, new Response.Listener&lt;String&gt;() {
  
          @Override
          public void onResponse(String response) {
              Log.d(TAG, "Register Response: " + response.toString());
              hideDialog();
  
              try {
                  JSONObject jObj = new JSONObject(response);
                  boolean error = jObj.getBoolean("error");
                  if (!error) {
                      String noerror = jObj.getString("noerror");
                      Toast.makeText(getApplicationContext(),
                              noerror, Toast.LENGTH_LONG).show();
  
                  } else {
  
                      // Error occurred in registration. Get the error
                      // message
                      String errorMsg = jObj.getString("error_msg");
                      Toast.makeText(getApplicationContext(),
                              errorMsg, Toast.LENGTH_LONG).show();
                  }
              } catch (JSONException e) {
                  e.printStackTrace();
              }
  
          }
      }, new Response.ErrorListener() {
  
          @Override
          public void onErrorResponse(VolleyError error) {
              Log.e(TAG, "Registration Error: " + error.getMessage());
              Toast.makeText(getApplicationContext(),
                      error.getMessage(), Toast.LENGTH_LONG).show();
              hideDialog();
          }
      }) {
  
          @Override
          protected Map&lt;String, String&gt; getParams() {
              // Posting params to register url
              Map&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
  
              params.put("image",image);
              params.put("pos",Integer.toString(pos));
              params.put("thid",thid);
              params.put("accidentid",Integer.toString(id));
  
              return params;
          }
  
      };
      strReq.setRetryPolicy(new DefaultRetryPolicy(
              30000,
              DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
              DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
      // Adding request to request queue
      AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  
  }
  </code></pre>