从Android发送值到PHP没有名称对值 [英] sending values from android to php without namepair value

查看:87
本文介绍了从Android发送值到PHP没有名称对值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用android api 23和从android的开发人员站点将数据从android发送到php.Am,我知道从api 22及更高版本不推荐使用namepairvalue.我在stackoverflow中提到了其中一篇文章,并获得了以下代码.但这仍然行不通.

I want to send data from android to php.Am using android api 23 and from android's developers site a got to know that namepairvalue is deprecated from api 22 and above. I referred to one of the post here in stackoverflow and got the below code. but still it doesn't work.

我的问题是正在向PHP代码发送一个value(ID),并基于该值从数据库中获取记录.现在,如果我在不使用此值的情况下获取数据,则效果很好,但是我无法将id值发送到php代码.

My problem is that am sending a value(ID) to php code and based on that value fetching the records from database. Now if i fetch data without using this value it works fine, but i am unable to send the id value to php code.

我无法找到此代码中的错误之处,或者可能是我的错误正在编辑代码.如果有人帮助我清楚地理解它,那就太好了.

i am unable to find whats wrong in this code or may be my fault is editing the code. It would be great if someone helps me to understand it clearly .

谢谢.

这是我尝试过的代码段.

Here is the code snippet that i have tried.

Android代码:

Android Code:

String sid = staffid.toString(); // this sid is passed as intent from another activity and passing it to below link.
Log.d("SID :", "" + sid);
 try {
            URL url = null;
            url = new URL("http://usplhubli.96.lt/hrm/hrm_app/individual_view_staff.php");
            Log.d("url to display :", "" + url.toString());
            HttpURLConnection conn = null;
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(CONNECTION_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);

            DataOutputStream os = new DataOutputStream(conn.getOutputStream());
            // Log.d("os to display :", "" + os.toString());

            Uri.Builder builder = new Uri.Builder().appendQueryParameter("sid", sid);
            Log.d("builder to display :", "" + builder.toString());


            String query = builder.build().getEncodedQuery();

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            Log.d("writer display :", "" + writer.toString());
            writer.write(query);
            writer.flush();
            writer.close();
            os.close();

        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (ProtocolException e2) {
            e2.printStackTrace();
        } catch (IOException e3) {
            e3.printStackTrace();
        }
        return null;
    }

我在 AsyncTask doInBackground(String ... params) 中有此代码 基于这个 sid 值,我必须从数据库中获取数据并将其显示在android活动中.

I have this code in doInBackground(String... params) of AsyncTask based on this sid value i have to get the data from database and display it in android activity.

这是我尝试过的PHP代码

Here is the PHP code that i have tried

PHP代码:

$response=array();
$sid=$_POST["sid"];   // this is the POST data that am send from android
//$sid="4";     // this is the static value that i have tried and its working
print_r($sid);
include 'dbconfig.php';

$imagePath='http://www.usplhubli.96.lt/hrm';
$query=mysqli_query($conn,"select * from hrm_staff where sid='$sid'");
if(mysqli_num_rows($query) >0) {
 $response["hrm_staff"] = array();
 while ($row = mysqli_fetch_array($query)) {
 $recp = array();
 $recp["sid"]=$row["sid"];
 $fullName=$row['fname']."".$row['mname']."".$row['lname'];
 $recp["name"]= $fullName;
 $recp["address"]=$row['address'];
 $recp['city']=$row['city'];
 $recp["design"]=$row['did'];
 $recp["contact"]=$row['mobile'];
 $recp["email"]=$row['email'];
 $recp['qualification']=$row['quali'];
 $recp["dateofjoining"]=$row['doj'];
 $ppic1=$row['ppic'];
 $ppic1;
 $ppic21=$imagePath."/".trim($ppic1);
 $recp["ppic"]= $ppic21;
 array_push($response["hrm_staff"], $recp);

 }
  $response["success"] = 1;
 $response["message"]="display records";
 echo json_encode($response);   
 }
else
 {
    $response["success"] = 0;
    $response["message"] = "No products found";
    echo json_encode($response);
}

请提出建议.谢谢.

推荐答案

使用以下代码行使用 HttpURLConnection

用于发送请求参数的方式为:-

For sending the request parameter are as:-

  Uri.Builder builder = new Uri.Builder()
                    .appendQueryParameter("phone", number)
                    .appendQueryParameter("password", password)
                    .appendQueryParameter("device_login",value);

对于在connectionUrl的post方法中获取request参数,如下所示:-

And for the getting the request parameter in the post method of connectionUrl are as follow:-

URL url = new URL(myurl);
        HttpURLConnection urlConnection = (HttpURLConnection)     url.openConnection();
        urlConnection.setReadTimeout(30000);
        urlConnection.setConnectTimeout(30000); ;
        String query = builder.build().getEncodedQuery();


                System.out.println("REQUEST PARAMETERS ARE===" + query);
                urlConnection.setRequestMethod("POST");
                urlConnection.setDoInput(true);
                urlConnection.setDoOutput(true);

                urlConnection.connect();


                //Send request
                DataOutputStream wr = new DataOutputStream (
                        urlConnection.getOutputStream ());
                wr.writeBytes (query);
                wr.flush ();
                wr.close ();

                //Get Response
                InputStream isNew = urlConnection.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(isNew));


                String line;
                response = new StringBuffer();
                while((line = rd.readLine()) != null) {
                    response.append(line);

                }
                rd.close();


                System.out.println("Final response===="+response);

这篇关于从Android发送值到PHP没有名称对值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆