数据未通过齐射传递到服务器

时间:2018-09-19 11:12:43

标签: android android-volley

我正在尝试使用齐射将数据发送到服务器以检查登录,但是服务器端未接收到数据。我收到一条错误消息

  

注意:未定义的索引:第5行的C:\ xampp2 \ htdocs \ serverconnect.php中的用户名

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

        StringRequest stringRequest  = new StringRequest(Request.Method.GET, "http://192.168.43.144/mr_loc_tracker.php",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
                    Log.i("Server Response",response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
                    Log.i("Server Error Response",error.toString());
                }
            }
        ){

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<String,String>();
                params.put("username","Pranay");


                return params;
            }
        };
        MySingleton.getInstance(MainActivity.this).addToRequestQueue(stringRequest);
    }
}

MySingleton.java代码:

public class MySingleton {
    private static MySingleton mInstance;
    private RequestQueue requestQueue;
    private  static Context mCtx;

    private MySingleton(Context context){
        mCtx = context;
        requestQueue = getRequestQueue();
    }

    public static synchronized MySingleton getInstance(Context context){
        if (mInstance==null)
        {
            mInstance= new MySingleton(context);
        }
        return mInstance;
    }

    public <T>void addToRequestQueue(Request<T> request){
        requestQueue.add(request);
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue==null)
        {
            requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());

        }
        return requestQueue;
    }
}

serverconnect.php的代码

 <?php

 $username = $_GET["username"];

 echo $username;

 ?>

我遇到错误

  

注意:未定义的索引:第5行的C:\ xampp2 \ htdocs \ serverconnect.php中的用户名

3 个答案:

答案 0 :(得分:1)

只需更改$_GET-> $_POSTRequest.Method.GET-> Request.Method.POST

如果您想使用GET方法,请删除getParams()函数,则必须在网址ex中传递参数:http://192.168.43.144/mr_loc_tracker.php?username=doe

答案 1 :(得分:0)

上传您的(php)服务器连接代码和数据库名称以及所有

答案 2 :(得分:0)

<?php
 require 'config.php'; // your databsee configuration file
 $username = $_GET['username'];

$sql = "SELECT * FROM users(user table) where email='$email'";
$result = mysqli_query($con,$sql);
if(mysqli_num_rows($result)>0)
{
    $status = "exist";
}
echo json_encode(array("response"=>$status));
mysqli_close($con);

 ?>