【问题标题】:type java.lang.string cannot be converted to jsonarray类型 java.lang.string 无法转换为 jsonarray
【发布时间】:2016-10-08 00:18:39
【问题描述】:

我正在使用 volley 从 android 连接 php,但它向我显示错误,请帮我看看我的 java & php 代码 .php 连接到数据库 php show -1 'success'=>false 结果。

public class main extends Activity{

   String i = "";
   String d = "";
   String ya = "";
   String is = "";
   String to="";

   final Response.Listener<String> responseListener = new Response.Listener<String>() {

    @Override
    public void onResponse(String response)
    {
        JSONObject jsonResponse = null;

        try {
            JSONArray array = new JSONArray(response);
            jsonResponse = array.getJSONObject(0);
            Log.w(TAG, "onResponse: jsonresponse" + response.toString());
            boolean success = jsonResponse.getBoolean("success");
            if (success) {
                i = jsonResponse.getString("i");
                d = jsonResponse.getString("d");
            } else {
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    Inp insQ = new Inp(ya,is ,to ,responseListener);
    RequestQueue queue = Volley.newRequestQueue(main.this);
    queue.add(insQ);}


// next ins class  - commented at edit by Jeff
public class Ins extends StringRequest 
{
    public static final String REGISTER_REQUEST_URL = "edu.php";

    private static final String TAG = "Ins";
    private Map<String,String> params;
    public Ins(String ya, String is, String to, Response.Listener listener)
    {
        super(Request.Method.POST, REGISTER_REQUEST_URL, listener, null);
        Log.w(TAG, "Ins: " + ya + is + to );
        params = new HashMap<>();
        params.put("ya", ya);
        params.put("is",is);
        params.put("to", to + "");
        Log.w(TAG, "Ins working well  " + ya + is +to );
    } 

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

php代码开始

<?php

$servername = "localhost";
$username = "****";
$password = "*****";
$dbname = "*****";


$em = $_POST['ya'];
$one = $_POST['is'];
$to = $_POST['to'];


$d = date('Y-m-d');
$y = date('y');
$m = date('m');
$d = date('d'); 
$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
$sqll = "SELECT * FROM jio";
$res = mysqli_query($conn,$sqll);
$rowC = mysqli_num_rows($res);

$rowC = $rowC%999 + 1;
if($rowC < 10){
    $i = $year.$mon.$day.'00'.$rowC;
}elseif (rowC < 100) {
    $i = $year.$mon.$day.'0'.$rowC;
}else {
    $i = $year.$mon.$day.$rowC;
}
$sql = "INSERT INTO jio(iu,i, d, ya, is, qs,to,ra,wto,wi,wk,)VALUES('0',".$i."','".$d."','".$em."','".$one."', '-1','".$to."','0','0','0','0')";

$r = mysqli_query($conn, $sql);
$rows=mysqli_affected_rows($conn);
$result = array();

if($rows>0) {
    array_push($result,array(
            'success'=>true,
            'i' => $i,
            'd' =>$d
    ));
}
else
array_push($result,array(
        'success'=>false
));

echo json_encode($result);

mysqli_close($conn);
?>                         

【问题讨论】:

  • $rowC%999 + 1 是怎么回事,没有人,我的意思是没有人在 PHP 中使用 array_push ......你可以这样做 $result = [ ... ]; }else{ $result = [ ... ];
  • 您必须至少有 5 个语法错误。在 Java/Android 和 php 中。以及VALUES('0',".$i."', 处的 sql 语法错误

标签: php android-studio android-volley


【解决方案1】:

根据您描述的“问题”,我猜您代码中的所有语法错误都来自将代码移植到此处。

似乎 - 如果我理解正确的话 - 你唯一的问题是你的 sql 中缺少 '

                                                                    //  here
$sql = "INSERT INTO jio(iu,i, d, ya, is, qs,to,ra,wto,wi,wk,)VALUES('0',".$i."','".$d."','".$em."','".$one."', '-1','".$to."','0','0','0','0')";

这将导致$rows 为假(由于 mysqli 错误),因此您的 if 将“成功”设置为假。

修正后的 sql 应该是

$sql = "INSERT INTO jio(iu,i, d, ya, is, qs,to,ra,wto,wi,wk,)VALUES('0','".$i."','".$d."','".$em."','".$one."', '-1','".$to."','0','0','0','0')";
// this is the critical part:
// ...,'".$i."',...

注意事项
您最好切换到prepared statements,因为您对sql-injection 开放。 此外,最好先检查您的查询是否成功或是否有任何错误。

$r = mysqli_query($conn, $sql);
if($r) {
   // work with the result
} else {
   // an error occurred. Show it, handle it, whatever.
   echo mysqli_error($conn);
}

另外,您不需要在 php.ini 中使用array_push。使用这种语法要容易得多:

$result = array();
if($rows>0) {
    $result[] = array(
        'success'=>true,
        'i' => $i,
        'd' =>$d
        );
}
else { // don't forget these brackets here!
   $result[] = array(
        'success'=>false
        );
} // and here

最后:您不需要在脚本末尾关闭 mysqli 连接,因为无论如何它都会在末尾终止。

【讨论】:

    猜你喜欢
    • 2016-02-21
    • 2018-04-22
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    相关资源
    最近更新 更多