gpt4 book ai didi

php - 无法使用 namevaluepair 将字符串发送到 php 文件

转载 作者:行者123 更新时间:2023-11-29 21:30:57 25 4
gpt4 key购买 nike

我正在尝试使用名称值对将字符串发送到 php 脚本。但我在另一边却收不到。这是我的代码。

protected String doInBackground(String... args) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("Username",code ));
Log.v("username", code);

DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://192.168.42.21:8080/sellapp/menuitem.php");

// Depends on your web service
httppost.setHeader("Content-type", "application/json");

InputStream inputStream = null;
String result = null;


try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();

inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();

String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();


} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish {}
}
return result;
}

这里我想将字符串代码中的值传递给我的 php 脚本。我的 php 脚本是

$con = mysqli_connect(HOST,USER,PASS,DB);
$cst_id=$_REQUEST['Username'];
// $cst_id= 'cus02';
$sql = "
select
cust_code, segment_type, cust_name, cust_address, cust_payment_type, cust_credit_limit, cust_cr_balance
from customer where cust_code='".$cst_id."'
";

$res = mysqli_query($con,$sql);

$result = array();
while($row = mysqli_fetch_array($res)){
array_push(
$result,
[
'cust_id'=>$row[0],
'cust_seg'=>$row[1],
'cust_name'=>$row[2],
'cust_type'=>$row[3],
'cust_ad'=>$row[4],
'cust_cr'=>$row[5],
'cust_bl'=>$row[6]
]
);
}

echo json_encode(array("result"=>$result));

mysqli_close($con);

当我直接将值赋予 php 时,它就可以工作了。但通过名称/值对,它返回一个空数组作为结果。

请帮我得到答案。我尝试了与之相关的问题。但没有成功。

最佳答案

<?php


$con = mysqli_connect(HOST,USER,PASS,DB);

$cst_id = $_POST['Username']; // --------- not $_REQUEST['Username'];

// $cst_id= 'cus02';

$sql = "select cust_code,segment_type,cust_name,cust_address,cust_payment_type,cust_credit_limit,cust_cr_balance from customer where cust_code='".$cst_id."' ";

$res = mysqli_query($con,$sql);


$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
['cust_id'=>$row[0],
'cust_seg'=>$row[1],
'cust_name'=>$row[2],
'cust_type'=>$row[3],
'cust_ad'=>$row[4],
'cust_cr'=>$row[5],
'cust_bl'=>$row[6]
]);
}

//echo json_encode(array("result"=>$result));
echo json_encode($result);

mysqli_close($con);


?>

关于php - 无法使用 namevaluepair 将字符串发送到 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35264377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com