Bivin February 2016

Can not send string to php file using namevaluepair

I am trying to send a string to php script using namevaluepair. But i couldn't receive it on the other side. Here is my code.

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;
}

Here i want to pass the value in the string code to my php script. my php script is

$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],
          

Answers


Kanudo February 2016

<?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);


?>

Post Status

Asked in February 2016
Viewed 1,596 times
Voted 7
Answered 1 times

Search




Leave an answer