【发布时间】:2015-11-17 05:10:08
【问题描述】:
我想将数据从 android 发送到 php.Am 使用 android api 23 并且从 android 的开发人员站点知道 namepairvalue 从 api 22 及更高版本中被弃用。我在stackoverflow中提到了一篇文章并得到了下面的代码。但还是不行。
我的问题是我正在向 php 代码发送一个值(ID)并基于该值从数据库中获取记录。现在,如果我在不使用此值的情况下获取数据,它可以正常工作,但我无法将 id 值发送到 php 代码。
我无法找到此代码中的错误,或者可能是我的错在编辑代码。如果有人能帮助我清楚地理解它,那就太好了。
提前谢谢你。
这是我尝试过的代码sn-p。
安卓代码:
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 活动中。
这是我尝试过的 PHP 代码
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);
}
请提出建议。谢谢。
【问题讨论】:
-
是的,我正在检查.. 我会回复你的。谢谢。
-
您有什么问题吗?或者它对您有用吗??
-
ya 它的工作.. 非常感谢你。现在我想将每个值放在不同的文本框中或将这些值存储到不同的变量中。怎么做。你能推荐点什么吗?
-
对于存储值,您可以使用 sqlite 或 sharedpreference...注意这些东西仅在您想要存储数据以供将来使用时使用,例如保存登录名和密码。
-
好的,那我现在该怎么办..?我想在另一个活动中将这些值显示到它们的文本框中。