通过Android Studio和Volley请求CURL



我正在尝试使用 REST API,我需要提交一个如下所示的 CURL 请求

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

我目前正在Android Studio上使用Volley,在过去的项目中,我已经能够成功生成HTTP POST和GET请求。但是,当我尝试与 REST API 通信时,我的整个应用程序都崩溃了。这是我的代码,

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

我的堆栈跟踪是

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@app@com.outsystemstest-2@split_lib_dependencies_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_0_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_1_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_2_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_3_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_4_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_5_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_6_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_7_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_8_apk.apk@classes.dex) 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@app@com.outsystemstest-2@split_lib_slice_9_apk.apk@classes.dex) 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)

我不明白我做错了什么。

杰罗姆·

我找出我哪里出错了,

我需要改变 headers.put("Content-Type", "application/json");

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

和使用

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

而不是

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

因为我应该将 POST 数据作为封装内容而不是参数发送,并插入

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

从以下来源获得答案:

Android-Volley : 为 JsonArrayRequest 设置 HTTP 标头

Android 凌空错误请求代码 415 与 Rest API?

相关内容

  • 没有找到相关文章