我想從Android發送數據到php.Am使用android api 23和從android的開發者網站得知namepairvalue從api 22和以上版本被棄用。我在這裏提到了一個在stackoverflow中的帖子,並得到了下面的代碼。但仍然無效。發送值從android到php沒有名字對值
我的問題是,我正在向php代碼發送一個值(ID),並根據該值從數據庫中獲取記錄。現在,如果我沒有使用這個值獲取數據,它工作正常,但我無法將ID值發送到PHP代碼。
我無法找到這個代碼有什麼問題,或者可能是我的錯在編輯代碼。如果有人幫助我清楚地理解它,那將會很棒。
預先感謝您。
這是我試過的代碼片段。
的Android代碼:
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;
}
我在doInBackground代碼(字符串... PARAMS)的AsyncTask 基於此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);
}
請建議。謝謝。
yes yes checking ..我會回覆你。謝謝。 –
您是否遇到任何問題或對您有用? –
你的工作..非常感謝你。現在我想要在不同的文本框中顯示每個值或將這些值存儲到不同的變量中。怎麼做。你能提出一些建議嗎? –