簡體   English   中英

無法使用namevaluepair將字符串發送到php文件

[英]Can not send string to php file using namevaluepair

我正在嘗試使用namevaluepair將字符串發送到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);


?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM