Unable to fetch any response from the api (Volley)

时间:2019-04-08 13:45:36

标签: java android android-volley httpresponse

I am unable to parse the request from the url http://shikshan.indiansmarthub.co.in/api/menu_t.php?token=SHIK9306488&catt=3

My other Api's follows the same structure but I am unable to get any response from this api while integrating it in my app.

Android Volley Code

private void doLogin() {
        String url = "http://shikshan.indiansmarthub.co.in/api/menu_t.php?token=SHIK9306488&catt=3";
        StringRequest strReq = new StringRequest(
                Request.Method.POST, url,
                new Response.Listener<String>() {

                    @Override
                    public void onResponse(String response) {

                        Log.e("tag_r", "tag_r");
                        Log.e("tag_res", response);

                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Toast.makeText(Login.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();

                    }
                });

        //Creating a Request Queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        //Adding request to the queue
        requestQueue.add(strReq);
    }

Api Code


<?php
include("connection.php");
include("connect.php");
ini_set('error_reporting', E_STRICT);
$token=$_GET['token'];

$cat=$_GET['catt'];
if (!$DEVICE) {
if($token=="SHIK9306488")
{

if($cat==2)
{      

$m1['MenuName']="Attendance";
$m1['Type']="M";

$m2['MenuName']="Attendance Status";
$m2['Type']="A";
// Leave
$m3['MenuName']="Leave";
$m3['Type']="M";

$m4['MenuName']="Apply Leave";
$m4['Type']="A";

$m5['MenuName']="Leave Status";
$m5['Type']="A";
// Assignment
$m6['MenuName']="Assignment";
$m6['Type']="M";

$m7['MenuName']="Upload Assignment";
$m7['Type']="A";

$m8['MenuName']="Download Assignment";
$m8['Type']="A";

// Exit Option
$m9['MenuName']="Exit";
$m9['Type']="M";

$status['Status']="1";

$data1['getMenu']=array($m1,$m2);
$data2['getMenu']=array($m3,$m4,$m5);
$data3['getMenu']=array($m6,$m7,$m8);
$data4['getMenu']=array($m9);
$data5['Status']=array($status);

$main['Status']="1";
$main['data']=array($data1,$data2,$data3,$data4);


echo json_encode($main);

}
else if($cat==3)
{

$m1['MenuName']="Attendance";
$m1['Type']="M";

$m2['MenuName']="Mark Self Attendance";
$m2['Type']="A";

$m9['MenuName']="Mark Student Attendance";
$m9['Type']="A";

$m10['MenuName']="Attendance Status";
$m10['Type']="A";
// Leave
$m3['MenuName']="Leave";
$m3['Type']="M";

$m4['MenuName']="Apply Leave";
$m4['Type']="A";

$m5['MenuName']="Leave Status";
$m5['Type']="A";

$m11['MenuName']="Leave Approval";
$m11['Type']="A";
// assignment
$m6['MenuName']="Assignment";
$m6['Type']="M";

$m7['MenuName']="Upload Assignment";
$m7['Type']="A";

$m8['MenuName']="Download Assignment";
$m8['Type']="A";

//$m12['MenuName']="Assignment Approval";
//$m12['Type']="A";

// Exit Option
$m13['MenuName']="Exit";
$m13['Type']="M";

$status['Status']="1";

$data1['getMenu']=array($m1,$m2,$m9,$m10);
$data2['getMenu']=array($m3,$m4,$m5,$m11);
$data3['getMenu']=array($m6,$m7,$m8);
$data4['getMenu']=array($m13);
$data5['Status']=array($status);


$main['Status']="1";
$main['data']=array($data1,$data2,$data3,$data4);

echo json_encode($main);

}

else
{
 $data=array(response=>"Invalid_token",staus=>0);
 $data2=array($data);
 $myJson=json_encode($data2);
echo $myJson;
}
}
else
{
 $data=array(response=>"Invalid_token",status=>"0");
 $data2=array($data);
 $myJson=json_encode($data2);
    echo $myJson;

}
}
else
{
 echo $useragent;
 echo "hello";
}

?>


The Api is successfully hitting on the post man and the browser but volley on android is unable to hit this api.

Response on Post man

{
    "Status": "1",
    "data": [
        {
            "getMenu": [
                {
                    "MenuName": "Attendance",
                    "Type": "M"
                },
                {
                    "MenuName": "Mark Self Attendance",
                    "Type": "A"
                },
                {
                    "MenuName": "Mark Student Attendance",
                    "Type": "A"
                },
                {
                    "MenuName": "Attendance Status",
                    "Type": "A"
                }
            ]
        },
        {
            "getMenu": [
                {
                    "MenuName": "Leave",
                    "Type": "M"
                },
                {
                    "MenuName": "Apply Leave",
                    "Type": "A"
                },
                {
                    "MenuName": "Leave Status",
                    "Type": "A"
                },
                {
                    "MenuName": "Leave Approval",
                    "Type": "A"
                }
            ]
        },
        {
            "getMenu": [
                {
                    "MenuName": "Assignment",
                    "Type": "M"
                },
                {
                    "MenuName": "Upload Assignment",
                    "Type": "A"
                },
                {
                    "MenuName": "Download Assignment",
                    "Type": "A"
                }
            ]
        },
        {
            "getMenu": [
                {
                    "MenuName": "Exit",
                    "Type": "M"
                }
            ]
        }
    ]
}

2 个答案:

答案 0 :(得分:0)

change request method from post to Get

答案 1 :(得分:0)

problem with $DEVICE variable in Api Code.that's why return useragent and hello keyword.Your API is working fine in PC but not working on mobile browser.

Check Screenshot