0
votes

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);

}
1
everything fine in your code. can you pls show your API code here.Rubanraj Ravichandran
#rubanraj : my code is going through the OnErrorResponse: and i can check the error in the log as ArrayIndexOutOfBound:Harish Chauhan
see my answer below. @HarishRubanraj Ravichandran

1 Answers

0
votes

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,.