我正在使用排球库与hackerrank API进行通信。我的代码如下。但我每次都会收到400的错误。我尝试通过POSTMAN将数据发布到此API,它运行正常。但这里没有用。请帮帮我。
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);
}
}
这是控制台日志
08-24 22:08:57.254 26345-26345/com.suvariyaraj.algorithms D/myapp: Error: null