dsgffz2579 2016-10-08 00:18
浏览 163
已采纳

类型java.lang.string无法转换为jsonarray

I am using volley to connect the php from android, but it's show me error please some help me here a my java & php code .php is connect to database php show -1 'success'=>false result.

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 code start

<?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);
?>                         
  • 写回答

1条回答

  • dousao9569 2016-10-08 02:01
    关注

    According to the "problem" you've described I guess all the syntax errors in your code came from porting the code to here.

    It seems that - if I understood you correctly - your only problem is a simple missing ' in your 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')";
    

    This will cause $rows to be false (because of a mysqli error), so your if sets 'success' to false.

    The corrected sql would be

    $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."',...
    

    NOTES
    You should better switch to prepared statements, because you're open to sql-injection. Also, better check first if your query was successful or if there were any errors.

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

    Also, you don't need to array_push in php. It's much easier to use that syntax:

    $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
    

    And finally: You don't need to close a mysqli-connection at the end of a script, as it will be terminated at the end anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?
  • ¥50 invest生境质量模块
  • ¥15 nhanes加权logistic回归,svyglm函数