0

I'm using Volley to interact with an API. I need to send a post request (with parameters) to a service that returns a JSON Array.

My Api code is having a function like:

public function Add($tripData) {

    $response = "Unsuccessful";
    if (isset($tripData)) {
        //print_r($tripData);
        $tripsModel = new TasksModel();
        $tripsModel->user_id = $tripData["user_id"];
        $tripsModel->date = $tripData["date"];
        $tripsModel->latitude = $tripData["latitude"];
        $tripsModel->longitude = $tripData["longitude"];
        $tripsModel->location = $tripData["location"];
        $tripsModel->notes = $tripData["notes"];
        $tripsModel->trip = $tripData["trip"];
        $tripsModel->city = $tripData["city"];

        $method = new CommonMethods();
        $response = $method->save("locationdetails", $tripsModel);
    }
    echo json_encode($response);
}

I just need to put params.put("data", data) to be in following kind of array:

$taskData["user_id"]=2;
$tripData["date"]="2015-06-02 16:01";
$tripData["latitude"]="41.213181";
$tripData["longitude"]="-124.004631";
$tripData["location"]="Redwood National";
$tripData["notes"]="hello";
$tripData["trip"]=3;
$tripData["city"]="California";

so that it could be used as:

$obj = new Myclass;
$var = $obj->Add($tripData);

I have checked the service already but could not post it from android code using hashmap.

private void insertTripData(final String[] tripData) {
    // TODO Auto-generated method stub

    // Tag used to cancel the request
    String tag_string_req = "req_login";

    StringRequest strReq = new StringRequest(Method.POST,
            AppConfig.URL_REGISTER, new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    Log.d("inserttrip",
                            "inserttrip Response: " + response.toString());

                    try {
                        JSONObject jObj = new JSONObject(response);
                        boolean error = jObj.getBoolean("error");

                        // Check for error node in json
                        if (!error) {
                            // user successfully logged in
                            // Create login session

                            // Launch main activity
                            Toast.makeText(getApplicationContext(),
                                    "user data inserted", Toast.LENGTH_LONG)
                                    .show();
                        } else {
                            // Error in login. Get the error message
                            String errorMsg = jObj.getString("error_msg");
                            // String errorMsg =
                            // "Please check your network connection";
                            Toast.makeText(getApplicationContext(),
                                    errorMsg, Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        // JSON error
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("insertTripError",
                            "Data Error: " + error.getMessage());
                    String errorMsg = "Please check your network connection";
                    Toast.makeText(getApplicationContext(), errorMsg,
                            Toast.LENGTH_LONG).show();

                }
            }) {

        @Override
        protected HashMap<String, String> getParams() {
            // Posting parameters to login url
            JSONObject jsonObject = new JSONObject();
            for (int i = 0; i <= tripData.length; i++) {
                tripData[i] = "questionId_" + i + "_" + "ans_" + i;
                try {
                    jsonObject.put("params_" + i, tripData[i]);
                    
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            String data= jsonObject.toString();
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("tag", "trip");
            params.put("action", "add");
            params.put("data", data);

            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

}
3
  • everything fine in your code. can you pls show your API code here. Aug 14, 2015 at 6:55
  • #rubanraj : my code is going through the OnErrorResponse: and i can check the error in the log as ArrayIndexOutOfBound: Aug 14, 2015 at 10:38
  • see my answer below. @Harish Aug 14, 2015 at 13:15

1 Answer 1

0

You said, getting "ArrayIndexOutOfBound" error in your logcat.

May be problem with your getParams() method.

Change the for loop this:

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

To:

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

Check now and if you get any other problem, please show your logcat here,.

1
  • If this answer helped you, you can do upvote and accept the answer rite.. @HarishChauhan Aug 17, 2015 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.