jlieow
jlieow

Reputation: 53

CURL request through Android Studio and Volley

I am trying to consume a REST API and I need to submit a CURL request which looks like this

curl -H "Content-Type: application/json" -d '{"Name":"Noah Pasquale","JobTitle":"Owner","Phone":"089-0877311"}' -D- https://osacademy.outsystemscloud.com/ContactsAPI/rest/Contacts/CreateContact

I am currently using Volley on Android Studio and I have been able to generate HTTP POST and GET requests successfully in past projects. However when I try to communicate with the REST API, my entire app crashes. This is my code,

// Instantiate the RequestQueue.
final RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
String server_urlpost = "https://osacademy.outsystemscloud.com/ContactsAPI/rest/Contacts/CreateContact"; //Points to target which is obtained from IPV4 Address from IP Config

      StringRequest stringRequestpost = new StringRequest(Request.Method.POST, server_urlpost,

              new Response.Listener<String>() {
                  @Override
                  public void onResponse(String response) {   //Server Response Handler
                      PostResponse.setText(response);
                      requestQueue.stop();
                  }
              }, new Response.ErrorListener() {
          @Override
          public void onErrorResponse(VolleyError error) {    //On Error Response Handler
              PostResponse.setText("Something went wrong...");
              error.printStackTrace();
              requestQueue.stop();
          }
      }){
          @Override
          protected Map<String, String> getParams() throws AuthFailureError {
              Map<String,String> params = new HashMap<String, String>();
              params.put("Name","Noah Pasquale");
              params.put("JobTitle", "Owner");
              params.put("Phone", "089-0877311");

              Log.i(TAG, params.toString());
              return params;
          }
          @Override
          public Map<String, String> getHeaders() throws AuthFailureError {
              Map<String,String> headers=new HashMap<String,String>();
              headers.put("Content-Type", "application/json");

              return headers;
          }
      };

      //Starts Request
      requestQueue.add(stringRequestpost);

My stack trace is

05-21 00:04:53.981 23838-23838/com.outsystemstest I/art: Late-enabling -Xcheck:jni
05-21 00:04:54.013 23838-23838/com.outsystemstest D/TidaProvider: TidaProvider()
05-21 00:04:54.170 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.306 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_0_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.340 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_1_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.382 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_2_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.435 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_3_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.482 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_4_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.532 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_5_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status

                                                         [ 05-21 00:04:54.539 23907:23907 I/         ]
                                                         ARTPROXY OFFSET: method_entry_offset = 48

                                                         [ 05-21 00:04:54.539 23907:23907 I/         ]
                                                         ARTPROXY OFFSET: method_interpreter_entry_offset = 32

                                                         [ 05-21 00:04:54.539 23907:23907 I/         ]
                                                         ARTPROXY OFFSET: managed_stack_size = 24

                                                         [ 05-21 00:04:54.540 23907:23907 I/         ]
                                                         ARTPROXY OFFSET: thread_deliver_exception_entry_offset = 1088

                                                         [ 05-21 00:04:54.540 23907:23907 I/         ]
                                                         ARTPROXY OFFSET: thread_exception_offset = 136

                                                         [ 05-21 00:04:54.540 23907:23907 I/         ]
                                                         ARTPROXY OFFSET: method_code_size_offset = 4
05-21 00:04:54.573 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_6_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.603 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_7_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.634 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_8_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.675 23838-23838/com.outsystemstest W/art: Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --instruction-set=arm64 --instruction-set-features=smp,a53 --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=generic --instruction-set-features=default --dex-file=/data/app/com.outsystemstest-2/split_lib_slice_9_apk.apk --oat-file=/data/dalvik-cache/arm64/data@[email protected]@[email protected]) because non-0 exit status
05-21 00:04:54.676 23838-23838/com.outsystemstest W/System: ClassLoader referenced unknown path: /data/app/com.outsystemstest-2/lib/arm64
05-21 00:04:54.682 23838-23838/com.outsystemstest I/InstantRun: starting instant run server: is main process
05-21 00:04:54.859 23838-23838/com.outsystemstest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
05-21 00:04:54.986 23838-23838/com.outsystemstest V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
05-21 00:04:54.986 23838-23838/com.outsystemstest V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
05-21 00:04:54.986 23838-23838/com.outsystemstest V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
05-21 00:04:54.987 23838-23838/com.outsystemstest V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
05-21 00:04:54.987 23838-23838/com.outsystemstest V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
05-21 00:04:54.992 23838-23838/com.outsystemstest V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@87ac85
05-21 00:04:54.993 23838-23838/com.outsystemstest V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@4332ada
05-21 00:04:55.163 23838-24046/com.outsystemstest D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-21 00:04:55.187 23838-23838/com.outsystemstest D/ActivityThreadInjector: clearCachedDrawables.
05-21 00:04:55.356 23838-24046/com.outsystemstest I/Adreno: QUALCOMM build                   : a7823f5, I59a6815413
                                                            Build Date                       : 09/23/16
                                                            OpenGL ES Shader Compiler Version: XE031.07.00.00
                                                            Local Branch                     : mybranch22028469
                                                            Remote Branch                    : quic/LA.BR.1.3.3_rb2.26
                                                            Remote Branch                    : NONE
                                                            Reconstruct Branch               : NOTHING
05-21 00:04:55.397 23838-24046/com.outsystemstest I/OpenGLRenderer: Initialized EGL, version 1.4
05-21 00:04:55.413 23838-24046/com.outsystemstest E/HAL: hw_get_module_by_class: module name gralloc
05-21 00:04:55.413 23838-24046/com.outsystemstest E/HAL: hw_get_module_by_class: module name gralloc
05-21 00:04:55.571 23838-23838/com.outsystemstest W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
05-21 00:04:57.644 23838-23838/com.outsystemstest I/Main: Sending POST request to Apache Server
05-21 00:04:57.684 23838-24193/com.outsystemstest I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor
05-21 00:04:57.684 23838-24193/com.outsystemstest I/Main: {Phone=Test, JobTitle=Test, Name=Test}
05-21 00:04:58.832 23838-24193/com.outsystemstest E/Volley: [92131] BasicNetwork.performRequest: Unexpected response code 400 for https://jerome-lieow.outsystemscloud.com/ContactsAPI/rest/Contacts/CreateContact
05-21 00:04:58.835 23838-23838/com.outsystemstest D/AndroidRuntime: Shutting down VM


                                                                    --------- beginning of crash
05-21 00:04:58.836 23838-23838/com.outsystemstest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.outsystemstest, PID: 23838
                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                        at com.outsystemstest.MainActivity$2.onErrorResponse(MainActivity.java:77)
                                                                        at com.android.volley.Request.deliverError(Request.java:564)
                                                                        at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:101)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5441)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

I cannot understand what I am doing wrong.

Jerome L

Upvotes: 1

Views: 5465

Answers (1)

jlieow
jlieow

Reputation: 53

I figured out where I went wrong,

I needed to change headers.put("Content-Type", "application/json");

to headers.put("Accept", "application/json");

and use

@Override
public byte[] getBody() throws AuthFailureError {}

instead of

@Override
protected Map<String, String> getParams() throws AuthFailureError {}

as I should have been sending the POST data as encapsulated content instead of parameters, and also insert

@Override
public String getBodyContentType() {    //Sets type to json
          return "application/json";
}

Got the answers from the following sources:

Android-Volley : set HTTP Header for JsonArrayRequest

Android volley error request code 415 with Rest API?

Upvotes: 3

Related Questions