java - 来自 Android 的 MySQL 请求错误

标签 java android

我有 MySQL 数据库。我正在尝试使用 PHP 从 android 获取一些数据。

这是我的 PHP 代码:在这里我尝试使用用户的 ID 获取用户的信息。

    <?php
// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["id"])) {
    $id = $_GET['id'];


// get a product from products table
$result = mysql_query("SELECT * FROM app WHERE vk_id = $id");
//echo $result;
if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $result = mysql_fetch_array($result);

        $users = array();

        $product["id"] = $result["id"];
        $product["first_name"] = $result["first_name"];
        $product["last_name"] = $result["last_name"];
        $product["vk_id"] = $result["vk_id"];
        $product["points"] = $result["points"];
        // success
        $response["success"] = 1;

        // user node
        $response["users"] = array();

        array_push($response["users"], $product);

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no product found
    $response["success"] = 0;
    $response["message"] = "No user found";

    // echo no users JSON
    echo json_encode($response);
}
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

使用浏览器测试给出了成功结果,这意味着 PHP 代码工作正常

所以,这是 Android 部分:

public class DB_read_by_id extends AsyncTask<String, String, String> {

    Context context;
    private static String url_read_by_id = "http://women.egeshki.ru/blockphonedb/read_by_id.php";

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_USERS = "users";
    private static final String TAG_POINTS = "points";


    JSONArray users = null;
    String vk_id;
    String points;

    JSONParser jParser = new JSONParser();

    public DB_read_by_id(String _vk_id, Context _context) {
        vk_id = _vk_id;
        context = _context;
        url_read_by_id += "?id=" + vk_id;
        Log.e("DB_read_by_id", "Constructor");
    }

    @Override
    protected String doInBackground(String... strings) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_read_by_id, "GET", params);
        Log.e("DB_read_by_id", "doInBackground");
        Log.e("DB_read_by_id: ", url_read_by_id);
        Log.e("DB_read_by_id json ", json.toString());
        // Check your log cat for JSON response
        //Log.e("Users_all: ", json.toString());
        try {
            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);
            String message = json.getString(TAG_MESSAGE);
            users = json.getJSONArray(TAG_USERS);

            Log.e("DB_read_by_id", "Attempt to get from db");
            if (success == 1) {
                JSONObject c = users.getJSONObject(0);
                points = c.getString(TAG_POINTS);
                Log.e("DB_read_by_id Points", points);
            } else if (message.equals("No user found")) {
                Log.e("DB_read_by_id", "No user found");
            } else if (message.equals("Required field(s) is missing")) {
                Log.e("DB_read_by_id", "Required field(s) is missing");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
}

这是日志猫:

com.example.myapplication E/DB_read_by_id﹕ Constructor
com.example.myapplication E/DB_read_by_id﹕ doInBackground
com.example.myapplication E/DB_read_by_id:﹕ http://women.egeshki.ru/blockphonedb/read_by_id.php?id=150923434
com.example.myapplication E/DB_read_by_id json﹕ {"message":"No user found","success":0}

当我从浏览器测试从 logcat 获得的链接时,它再次工作正常。但是从android 上它给了我一个错误。 问题是链接没问题,但是android给出了错误

我的错误在哪里?

最佳答案

您没有将 ID 发送到服务器

   List<NameValuePair> params = new ArrayList<NameValuePair>();
    //add this
    params.add(new BasicNameValuePair("id","the value here));//
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_read_by_id, "GET", params);

编辑:阅读您的评论后 我认为问题是你在链接中使用你的参数而不是使用正确的方式..只需尝试使用这个:

  List<NameValuePair> params = new ArrayList<NameValuePair>();
        //add this
        params.add(new BasicNameValuePair("id","150923434"));//
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_read_by_id, "GET", params);

尝试一下并报告

关于java - 来自 Android 的 MySQL 请求错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784980/

相关文章:

android - 检查是否违反了 android :minSdkVersion automatically

Android 动态壁纸触摸事件层次结构

Android:使用支持 fragment 管理器时复制 fragment

java - 从 JPanel 中动态删除组件

java - 如何从 Scala 连接 Amazon Redshift

java - 如何在另一个应用程序模块中使用项目源代码

java替换字符串中的多个字符,包括 "\u00A2"

java - 边缘和属性的复合索引(tinkerpop/orientDB)

通过 Socket 进行 Java TCP 通信

java - 为什么 Java 不允许将 List<String> 对象分配给 List<Object> 对象?