PHP与Android项目的连接


php connection with android project

我是安卓开发的初学者。我想将 php 文件连接到安卓应用程序。 我的 php 代码是

<?php    
    $con = mysqli_connect("localhost", "root", "", "invoice_db");
    if(mysqli_connect_errno($con)) {
        echo "Failed to connect";
    }
    $response["sucess"]=0;
    $invoiceid = $_POST['invc']; 
    $response = array();
    $sql = "SELECT sl_no from invoice_table where invoice_id='$invoiceid'";
    $result = mysqli_query($con,$sql);
    if(!empty($result)) {
        $row = mysqli_fetch_array($result);
        $data = $row[0];
        $response["sucess"] = 1;
    }
    mysqli_close($con);
?>

这里 'invc' 是从 httpRequest 获取的,

JSONObject json = jsonParser.makeHttpRequest(url_check_user, "POST",  params);

我的 JSONParser 页面包含,

if (method == "POST") {
    // request method is POST
    // defaultHttpClient
    System.out.println("Inside json parser POST condition");
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(new UrlEncodedFormEntity(params));
    System.out.println("Inside json parser POST condition" + params);
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity = httpResponse.getEntity();
    Log.d("From httpentity", httpEntity.toString());  
    System.out.println("ppppppppppppphhhhhhhhhhhhhhhhhhhhppppppppppp");
    is = httpEntity.getContent();
}

现在我想检查 ,参数是否已传递到 php 页面。所以我想安慰/记录猫$invoiceid.在Eclipse Ide中怎么可能呢?

如果你想在

PHP代码中打印一个变量,你可以echo $variable。但是请注意,PHP代码将在服务器上执行,而不是在您的Android设备上执行。此外,您的PHP代码容易受到SQL注入攻击

您可以在 php 文件中使用 JSON 编码方法来获得正确的 JSON 响应,如下所示。

$response = array (
                'invoiceid' => $verify_code,    
            );
print json_encode($response);

这将以如下格式将 JSON 字符串返回到您的应用程序

{"invoiceid":"null"}

您可以像这样解码和记录它

InputStreamReader isw = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isw);
            String line = "";
            StringBuffer buffer = new StringBuffer();
            while ((line = br.readLine()) != null){
                buffer.append(line);
            }
String finalresult = buffer.toString();
 JSONObject myobject = new JSONObject(finalresult);
            String flag= myobject.getString("invoiceid");
log.e("mylog",flag)  

这样它就会在您的 logcat 文件中可见。