使用密码保护的PHP脚本将数据发布到数据库中

时间:2017-07-02 14:15:39

标签: php android mysql android-volley

我需要使用php脚本将数据从Android应用程序上传到数据库。此脚本也受用户名和密码保护,当我尝试将数据上传到数据库时,会显示“意外响应代码401”错误。当我从脚本中删除密码一切正常,所以问题不在PHP脚本或数据库中。这是我的请求类:

package com.home.gio.autostop.server;

import android.util.Base64;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import com.home.gio.autostop.model.Position;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class UploadPositionRequest extends StringRequest {
    private static final String REQUEST_URL = "http://autostop1.000webhostapp.com/upload.php";
    private Map<String, String> params;

    public UploadPositionRequest(Position position, Response.Listener<String> listener) {
        super(Request.Method.POST, REQUEST_URL, listener, null);
        params = new HashMap<>();
        params.put("mac", position.getMac());
        params.put("android_id", position.getAndroidId());
        params.put("latitude", position.getLatitude() + "");
        params.put("longitude", position.getLongitude() + "");
        params.put("latitudeDestination", position.getLatitudeDestination() + "");
        params.put("longitudeDestination", position.getLongitudeDestination() + "");
        params.put("kindOfUser", position.getIsKindOfUser().toString() + "");
    }

    @Override
    public Map<String, String> getParams() {
        return params;
    }


}

这就是我使用该类的方式:

Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject jsonResponse = new JSONObject(s);
                    boolean success = jsonResponse.getBoolean("success");
                    if (!success) {
                        builder = new AlertDialog.Builder(context);
                        builder.setMessage("uploading position failed")
                                .setNegativeButton("retry", null)
                                .create()
                                .show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        };
        UploadPositionRequest upload = new UploadPositionRequest(position, responseListener);
        RequestQueue queue = Volley.newRequestQueue(context);
        queue.add(upload);

我试图像这样覆盖getHeader()方法

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<>();
    String credentials = "username:password";
    String auth = "Basic "
            + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
    headers.put("Content-Type", "application/json");
    headers.put("Authorization", auth);
    return headers;
}

401错误消失,但没有上传数据库。那么我该如何解决这个问题呢? 修改 这是php脚本:

<?php

$mac=null;
$android_id=null;
$longitude=null;
$latitude=null;
$latitudeDestination=null;
$longitudeDestination=null;
$kindOfUser=null;
$con=mysqli_connect("localhost","...","...","...");
if(!empty($_POST["mac"])){
$mac=$_POST["mac"];
echo "mac";
}
if(!empty($_POST["android_id"])){
$android_id=$_POST["android_id"];
echo "android_id";
}
if(!empty($_POST["latitude"])){
$latitude=$_POST["latitude"];
echo "latitude";
}
if(!empty($_POST["longitude"])){
$longitude=$_POST["longitude"];
echo "longitude";
}
if(!empty($_POST["latitudeDestination"])){
$latitudeDestination=$_POST["latitudeDestination"];
echo "latitudeDestination";
}
if(!empty($_POST["longitudeDestination"])){
$longitudeDestination=$_POST["longitudeDestination"];
echo "longitudeDestination";
}
if(!empty($_POST["kindOfUser"])){
$kindOfUser=$_POST["kindOfUser"];
echo "kindOfUser";
}
$query="INSERT INTO marker (mac,android_id,latitude,longitude,latitudeDestination,longitudeDestination,kindOfUser) 
VALUES (?,?,?,?,?,?,?)
ON DUPLICATE KEY UPDATE latitude = VALUES(latitude), longitude = VALUES(longitude), latitudeDestination = VALUES(latitudeDestination), longitudeDestination = VALUES(longitudeDestination), kindOfUser = VALUES(kindOfUser)";
$statement=mysqli_prepare($con,$query) or die(mysqli_error($con));
mysqli_stmt_bind_param($statement,"ssdddds",$mac,$android_id,$latitude,$longitude,$latitudeDestination,$longitudeDestination,$kindOfUser);
mysqli_stmt_execute($statement);
$response=array();
$response["success"]=true;
echo json_encode($response);
?>

0 个答案:

没有答案