简体   繁体   English

Volley POST 字符串请求意外错误 500

[英]Volley POST string request unexpected error 500

I am using Volley library in my project and getting Unexpected response code 500 as response.我在我的项目中使用 Volley 库并得到Unexpected response code 500作为响应。

I have searched stackoverflow thoroughly and still unable to find solution that works.我已经彻底搜索了stackoverflow,但仍然无法找到有效的解决方案。

Following is my code for making GET string request以下是我发出 GET 字符串请求的代码

        val API = "http://squadtechsolution.com/android/v1/allcompany.php"
        val requestQueue = Volley.newRequestQueue(mActivity)
        val stringRequest = StringRequest(
            Request.Method.GET,
            API,
            Response.Listener { response ->
                Log.i("dxdiag", response)
                mView.onFetchHttpDataResult(true)
                Toast.makeText(context, response, Toast.LENGTH_LONG).show()
            },
            Response.ErrorListener { error ->
                Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show()
                Log.i("dxdiag", error.printStackTrace().toString())
                mView.onFetchHttpDataResult(false)
            })
        requestQueue.add(stringRequest)

Following is the stacktrace以下是堆栈跟踪

2019-09-03 17:15:53.237 3308-3892/com.squadtechs.markhor.foodapp 
E/Volley: [194] BasicNetwork.performRequest: Unexpected response code 
500 for 
http://squadtechsolution.com/android/v1/allcompany.php
2019-09-03 17:15:53.243 3308-3351/com.squadtechs.markhor.foodapp 
D/EGL_emulation: eglMakeCurrent: 0xa7d84180: ver 2 0 (tinfo 
0xa7d832b0)
2019-09-03 17:15:53.256 3308-3308/com.squadtechs.markhor.foodapp 
W/System.err: com.android.volley.ServerError
2019-09-03 17:15:53.257 3308-3308/com.squadtechs.markhor.foodapp 
W/System.err:     at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:205)2019-09-03 17:15:53.257 3308-3308/com.squadtechs.markhor.foodapp W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:131)2019-09-03 17:15:53.257 3308-3308/com.squadtechs.markhor.foodapp W/System.err:     at com.android.volley.NetworkDispatcher.processRequest(NetworkDispatcher.java:111)2019-09-03 17:15:53.257 3308-3308/com.squadtechs.markhor.foodapp W/System.err:     at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:90)

Following is PHP code that I wrote on server side:以下是我在服务器端编写的 PHP 代码:

<?php
    require 'db.php';

    $sql = "SELECT * FROM `company_profile`";
    $result = $con->query($sql);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        $id=$row['id']; 

        $company_name=$row['company_name'];
        $cuisine=$row['cuisine'];
        $conpany_phone=$row['conpany_phone'];
        $company_description=$row['company_description']; 
        $company_logo=$row['company_logo'];
        $company_type=$row['company_type'];
        $delivery_type=$row['delivery_type'];
        $delivery_range=$row['delivery_range']; 
        $delivery_fee=$row['delivery_fee'];
        $delivery_pickupinfo=$row['delivery_pickupinfo'];
        $address=$row['address'];

        $companyData[] = array('id'=> $id,'company_name'=> 
        $company_name,'cuisine'=> $cuisine,'conpany_phone'=> 
        $conpany_phone,'company_description'=> 
        $company_description,'company_logo'=> $company_logo,'company_type'=> 
        $company_type,'delivery_type'=> $delivery_type,'delivery_range'=> 
        $delivery_range,'delivery_fee'=> 
        $delivery_fee,'delivery_pickupinfo'=> $delivery_pickupinfo,'address'=> $address);   
    }
    echo $jsonformat=json_encode($companyData);
    } else {
        echo "0 results";
    }
    $conn->close();
?>

Error 500 means there is problem in server code. 错误500表示服务器代码存在问题。 Please check code written at server side. 请检查服务器端编写的代码。 Thank you. 谢谢。

HTTP status code 500 represents the internal server error , clearly states that you don't have any issue in client-side (here in your case Volly ) but the issue is at server-side. HTTP状态代码500表示internal server error ,清楚地表明您在客户端没有任何问题(在您的案例中为Volly ),但问题出在服务器端。 Try debugging your server-side php code. 尝试调试服务器端的PHP代码。 the code is not properly executing and hence it's returning the 500 internal server error in Http response header. 代码没有正确执行,因此它在Http响应头中返回500 internal server error Volly just thrown the received header as an error. Volly只是将收到的标题抛出作为错误。

If you are backend dev, you should try it first with REST Clients before integration with the mobile application. 如果您是后端开发人员,则应在与移动应用程序集成之前首先使用REST Clients进行尝试。 You can get it one from Here . 你可以从这里得到一个。 Your API should be well tested and should be in a working state otherwise you will receive errors all the time. 您的API应该经过充分测试,并且应该处于工作状态,否则您将始终收到错误。

I have something First import the okhttp inside the Gradle dependency (Library).我有一些东西首先在 Gradle 依赖项(库)中导入 okhttp。 here is the documentation这是文档

https://square.github.io/okhttp/ https://square.github.io/okhttp/

After open the postman and click on the code menu打开邮递员后点击代码菜单在此处输入图片说明

As you can see below the send Button the code button is there.正如您在发送按钮下方所看到的,代码按钮就在那里。 click it and select the java-> okhttp单击它并选择 java-> okhttp

copy the code and paste it inside android studio.复制代码并将其粘贴到 android studio 中。 it has 99.9 % chance it will work.它有 99.9% 的几率会起作用。

I modified the code a bit and checked it in postman, it works fine.我稍微修改了代码并在邮递员中检查它,它工作正常。

I moved the JSON encode statment out of the if statement.我将 JSON 编码语句移出了 if 语句。

// output data of each row
while($row = $result->fetch_assoc()) {

    $id=$row['id']; 

    $company_name=$row['company_name'];
    $cuisine=$row['cuisine'];
    $conpany_phone=$row['conpany_phone'];
    $company_description=$row['company_description']; 
    $company_logo=$row['company_logo'];
    $company_type=$row['company_type'];
    $delivery_type=$row['delivery_type'];
    $delivery_range=$row['delivery_range']; 
    $delivery_fee=$row['delivery_fee'];
    $delivery_pickupinfo=$row['delivery_pickupinfo'];
    $address=$row['address'];

    $companyData[] = array('id'=> $id,'company_name'=> 
    $company_name,'cuisine'=> $cuisine,'conpany_phone'=> 
    $conpany_phone,'company_description'=> 
    $company_description,'company_logo'=> $company_logo,'company_type'=> 
    $company_type,'delivery_type'=> $delivery_type,'delivery_range'=> 
    $delivery_range,'delivery_fee'=> 
    $delivery_fee,'delivery_pickupinfo'=> $delivery_pickupinfo,'address'=> $address);   
}
echo $jsonformat=json_encode($companyData);

$conn->close();

This error "500" means serverside error.This errors occurs in these case:- 此错误“500”表示服务器端错误。在这些情况下会发生此错误: -

  1. May be wrong spell on the parameter or URLs 可能是参数或URL上的错误拼写
  2. Lack of Parameter. 缺乏参数。
  3. Or you are sending something that isn't accepted by Server. 或者您正在发送服务器不接受的内容。
  4. Also Check "http:/" or"https:/" on Urls. 同时在Urls上检查“http:/”或“https:/”。

Check these conditions.I have also faced same problem .APIs are working on Post Man but not working on devices using volley. 检查这些条件。我也遇到了同样的问题.API正在研究Post Man,但没有使用凌空的设备。 Hope this may help you. 希望这可以帮到你。

Please check if all params you are trying to send are the same which server is expecting. 请检查您尝试发送的所有参数是否与服务器预期的相同。 Also wrong or missing parameters return 500 Error. 参数错误或缺失也会返回500错误。 Reverify this param Company_mobile in your request. 在您的请求中重新验证此param Company_mobile Hope this resolves your issue. 希望这可以解决您的问题。

Remove below line from your server-side code and check 从服务器端代码中删除以下行并检查

echo $jsonformat=json_encode($companyData);

and the same is not working on postman also 同样也不适用于邮递员

在此输入图像描述

HTTP Status Codes starting with 5 inform that the error is on server side. 从5开始的HTTP状态代码通知错误在服务器端。 The code 500 is interpreted as Internal Server Error, to solve this you have to check what might cause it. 代码500被解释为内部服务器错误,要解决此问题,您必须检查可能导致它的原因。 It may be caused by a mistake in the code in that case you can open your error_log to see the error and act accordingly. 它可能是由代码中的错误引起的,在这种情况下,您可以打开error_log来查看错误并采取相应措施。

It can be caused by server features being unavailable momently like accessing the database or having many simultaneous opened connections that exceed the associated mysql resources. 它可能是由服务器功能暂时不可用引起的,例如访问数据库或者有许多同时打开的连接超过相关的mysql资源。

Some other times, the error is not logged into the error_log file. 其他一些时候,错误未记录到error_log文件中。 If you use a cpanel, at the homepage, under Metrics tab open Errors and check according to the time you requested to the server. 如果使用cpanel,请在主页的“度量标准”选项卡下打开“错误”,然后根据您向服务器请求的时间进行检查。 If you are not using cpanel look for a corresponding server log. 如果您不使用cpanel,请查找相应的服务器日志。

follow this Right mark Answer.. it may help you 按照这个正确的标记答案..它可能会帮助你

How to deal with unexpected response 500 in Android 如何处理Android中的意外响应500

Check your db.php file and then see variable that return object of connection. 检查db.php文件,然后查看返回连接对象的变量。

I see it should be $con->close() with one n not $conn->close() with double n 我看到它应该是$con->close() ,其中一个n不是$conn->close() ,双n

Try including in your php file :尝试包含在您的 php 文件中:

header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");

I am not familliar with Kotlin but it is working fine for me using Java , here is the code :我不熟悉 Kotlin,但使用 Java 对我来说效果很好,这是代码:

private void loadRecyclerViewData2(){
        URR_DATA="http://squadtechsolution.com/android/v1/allcompany.php";
        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                URR_DATA,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println("****"+response);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.println("***"+error.getMessage());                    }
                });
        requestQueue = Volley.newRequestQueue(Home.this);
        requestQueue.add(stringRequest);
    }

And i get the following output :我得到以下输出:

I/System.out: ****
    [{"id":"1","company_name":"ABC","cuisine":"QATAR","conpany_phone":"4535345","company_description":"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut ","company_logo":"company_logo5d80bac690f2e.png","company_type":"Food","delivery_type":"yes","delivery_range":"55","delivery_fee":"","delivery_pickupinfo":"Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, ","address":"33.58539655788556,71.43779296427965"}]

@Muhammad Faizan ignore your doubts, the issue IS a 500 server error @Muhammad Faizan 忽略您的疑虑,问题IS 500 服务器错误

THIS IS THE CAUSE这就是原因

  • POSTMAN and VOLLEY are not the same POSTMAN 和 VOLLEY 不一样
  • a) there is a bug in your PHP code or B) there is an environment setting that this exposes a) 您的 PHP 代码中存在错误or B) 存在暴露的环境设置

TO DIAGNOSE A PHP ISSUE诊断 PHP 问题

and you need to open your error_log on php.并且您需要在 php 上打开您的error_log Depending on your server this can be tricky to explain how so the other way is to change the php.ini to error_log to do this try this ON THE SERVER根据您的服务器,这可能很难解释如何,所以另一种方法是将php.ini更改为 error_log 以执行此操作在服务器上尝试此操作

( lifted from here deliberately ) 特意从这里抬起

/*This always works for me:*/

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

/*However, this doesn't make PHP to show parse errors - the only way to show those errors is to modify your php.ini with this line:*/

display_errors = on
/*(if you don't have access to php.ini, then putting this line in .htaccess might work too):*/

php_flag display_errors 1

TO DIAGNOSE A ENVIRONMENT ISSUE ON SERVER诊断服务器上的环境问题

If the error_log yields nothing try the access_log .如果error_log没有产生任何尝试access_log Why?为什么? Because it will probably show a different type of request has come in. and POSTMAN and VOLLEY are not the same因为它可能会显示不同类型的请求进来了。 POSTMAN 和 VOLLEY 不一样

You need to look at your access_log too potentially and watch it in real time as you click postman then click the volley client您需要潜在地查看您的access_log并在您单击邮递员然后单击排球客户端时实时查看它

FACTS: (why we can be so sure)事实:(为什么我们可以如此确定)

My gut says you have an issue in a wrongly configured server.我的直觉说您在错误配置的服务器中遇到了问题。 Believe it or not the access_log will help you the most if this is the case.信不信由你,如果是这种情况,access_log 对你的帮助最大。

Regardless: There is clearly an issue between what you are putting into POSTMAN and what your volley client is sending - that is causing this.无论如何:在您放入 POSTMAN 的内容与您的 volley 客户端发送的内容之间显然存在问题 - 这就是导致这种情况的原因。

Things I have seen cause this include:我所看到的原因包括:

  • It could be a SAME ORIGIN error.可能是SAME ORIGIN错误。
  • It could be a INTERNAL SERVER REDIRECT issue.这可能是INTERNAL SERVER REDIRECT问题。
  • It could be an imprecise forwarding of $_GET to $_POST (I have seen that issue on nginx)这可能是 $_GET 到 $_POST 的不精确转发(我在 nginx 上看到过这个问题)
  • It could be a bad path issue that is being mangled with no-redirect and lack of security.这可能是一个糟糕的路径问题,正在因no-redirect和缺乏安全性而受到影响。

but without telling us the platform or the server (Windows? Linux? nginx? php-fpm? apache? etc) we can't get to help you on those但如果不告诉我们平台或服务器(Windows?Linux?nginx?php-fpm?apache?等),我们就无法在这些方面为您提供帮助

I am posting another answer If it is still unsolved.如果仍未解决,我将发布另一个答案。 In the postman copy all the headers like在邮递员中复制所有标题,例如

.addHeader("content-type", "application/json")
.addHeader("cache-control", "no-cache")
.addHeader("postman-token", "fa72f792-c9d3-7891-a544-f37f634f6ee1")

and put that in the volly params并将其放入 volly 参数中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM