URL请求在android Request.Method.POST中不起作用?

Posted

技术标签:

【中文标题】URL请求在android Request.Method.POST中不起作用?【英文标题】:URL Request not working in android Request.Method.POST? 【发布时间】:2020-12-17 14:42:58 【问题描述】:

尝试使用此方法发送 URL 请求,但不起作用.... 网址是 -https://example.site/sendtp?otp="+randonnumber+"&number="+phoneNumber (添加了第 514,509 行参考)

    void sendotp(String phoneNumber)
    Random random = new Random();
    randonnumber=random.nextInt(999999);
    try 
        if (new ExtraOperations().haveNetworkConnection(getApplicationContext())) 
       [509]StringRequest request = new StringRequest(Request.Method.POST, "https://example.site/sendotp?otp="+randonnumber+"&number="+phoneNumber, new Response.Listener<String>() 
                @Override
                public void onResponse(String response) 
                    try 
                   [514]JSONObject jsonObject=new JSONObject(response);
                        JSONArray jsonArray=jsonObject.getJSONArray("result");
                        JSONObject jsonObject1=jsonArray.getJSONObject(0);

                        String success = jsonObject1.getString("success");

                        if (success.equals("1")) 

                        
                        else 
                            Toast.makeText(getApplicationContext(),"Something went wrong", Toast.LENGTH_LONG).show();
                        
                     catch (JSONException e) 
                        e.printStackTrace();
                    
                
            , new Response.ErrorListener() 
                @Override
                public void onErrorResponse(VolleyError error) 
                    error.printStackTrace();
                

logcat 错误是这个.. 不知道这是什么或如何修复...

2020-08-28 21:42:46.682 1844-27397/? I//system/bin/netd: gethostby*.getanswer: asked for "blitzplay.in IN AAAA", got type "RRSIG"
2020-08-28 21:42:46.683 1844-27397/? I//system/bin/netd: gethostby*.getanswer: asked for "blitzplay.in IN A", got type "RRSIG"
2020-08-28 21:42:48.352 27123-27123/com.blitzplay.in W/System.err: org.json.JSONException: Value ERR of type java.lang.String cannot be converted to JSONObject
2020-08-28 21:42:48.352 27123-27123/com.blitzplay.in W/System.err:     at org.json.JSON.typeMismatch(JSON.java:112)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:163)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:176)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at com.blitzplay.in.activity.OTPActivity$13.onResponse(OTPActivity.java:514)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at com.blitzplay.in.activity.OTPActivity$13.onResponse(OTPActivity.java:509)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:82)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:29)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:102)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at android.os.Looper.loop(Looper.java:193)
2020-08-28 21:42:48.353 27123-27123/com.blitzplay.in W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6669)
2020-08-28 21:42:48.354 27123-27123/com.blitzplay.in W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-08-28 21:42:48.354 27123-27123/com.blitzplay.in W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
2020-08-28 21:42:48.354 27123-27123/com.blitzplay.in W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

【问题讨论】:

看来你在onResponse方法上得到的String response不能转换成JSONObject。尝试记录您的响应字符串以检查 Log.d("TAG", "response: " + response); 有什么问题 Log.d("TAG", "response:" + response);未在 logcat 中返回任何信息 【参考方案1】:

解决了这个问题,(是数字输入的问题)

【讨论】:

以上是关于URL请求在android Request.Method.POST中不起作用?的主要内容,如果未能解决你的问题,请参考以下文章

Android Gradle 多环境URL请求设置

URL请求在android Request.Method.POST中不起作用?

当JSON对象URL字段在android studio中包含多个URL时,如何存储来自JSON对象请求的URL?

在 kotlin (Android ) 中发送获取请求

android的url请求返回json数据

Android网络请求加密机制