0

I am using volley library to communicate with the hackerrank API. My code is as below. But I am getting an error of 400 everytime. I tried with POSTing data to this API through POSTMAN and it works fine. But here it is not working. Please help me.

package com.suvariyaraj.algorithms;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class CodeEditor extends AppCompatActivity {

    public static final String TAG = "myapp";
    ProgressDialog pDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code_editor);

        // Tag used to cancel the request
        String url = "http://api.hackerrank.com/checker/submission.json";

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.show();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    pDialog.hide();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                    Log.d(TAG, "Error: " + error.getLocalizedMessage());
                    pDialog.hide();
                }
            }
        ) {
            @Override
            public HashMap<String, String> getHeaders() {
                HashMap<String, String> headers = new HashMap<>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }


            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("source", "import java.util.Scanner;   class AddNumbers {    public static void main(String args[])    {       int x, y, z;             Scanner in = new Scanner(System.in);       x = in.nextInt();       y = in.nextInt();       z = x + y;       System.out.println(z);    } }");
                params.put("lang", "3");
                params.put("testcases", "[\"3 0\"] ");
                params.put("api_key", "MYAPIKEY"); // I have replaced my api_key with MYAPIKEY
                return params;
            }

        };

        jsonObjReq.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }
}

This is the console log

08-24 22:08:57.254 26345-26345/com.suvariyaraj.algorithms D/myapp: Error: null
Raj Suvariya
  • 249
  • 1
  • 4
  • 14
  • http://stackoverflow.com/questions/19671317/400-bad-request-http-error-code-meaning – TWL Aug 24 '16 at 16:47
  • 400 usually means that you are forgetting do add some request parameter, header, etc. Try to print out headers to the console and compare them to your Postman request. – pawelo Aug 24 '16 at 16:56
  • @pawelo how to print the headers. And I am pretty sure that I am not missing any parameters – Raj Suvariya Aug 24 '16 at 17:25
  • Try jsonObjReq.getHeaders(). It may not be enough to find issue. Try to use some proxy tools like Charles Proxy or Fidler to compare request from Postman/browser and your app, letter by letter. Check if types are matching etc. – pawelo Aug 24 '16 at 18:29

0 Answers0