[英]How can I pass parameters to PHP files correctly
我在将字符串参数发送到PHP文件以从编辑文本中下载插入歌曲名称的歌曲时遇到问题。 我不明白我收到的错误。 先谢谢您的帮助!
logcat的:
Response from url: {"error":false,"message":"Musics fetched successfully.","musics":[]}
我不知道为什么数组为空。
如果我使用其他客户端传递歌曲名称而不是URL,则PHP文件有效。 这是我的代码:
机器人侧面:
class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//Toast.makeText(MainActivity.this, "Json Data is downloading", Toast.LENGTH_LONG).show();
canzone = editText.getText().toString();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "http://blabla.org/AndroidMusicDownload/downloads/getMusic.php?canzone=" + canzone;
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
/* title=jsonObj.getString("title");
link=jsonObj.getString("link");
HashMap<String, String> contact = new HashMap<>();
contact.put("title", title);
contact.put("link", link);
System.out.println("LINK: "+link);
contactList.add(contact);
*/
Toast.makeText(MainActivity.this, jsonObj.getString("message"), Toast.LENGTH_SHORT).show();
JSONArray jsonArray = jsonObj.getJSONArray("musics");
for (int i = 0; i < jsonArray.length(); i++) {
//Declaring a json object corresponding to every pdf object in our json Array
JSONObject jsonObject = jsonArray.getJSONObject(i);
//Declaring a Pdf object to add it to the ArrayList pdfList
// Pdf pdf = new Pdf();
// String pdfName = jsonObject.getString("name");
//String pdfUrl = jsonObject.getString("url");
//pdf.setName(pdfName);
//pdf.setUrl(pdfUrl);
//pdfList.add(pdf);
canzone_cantante = jsonObject.getString("canzone_cantante");
}
/* pdfAdapter=new PdfAdapter(MainActivity.this,R.layout.list_layout, pdfList);
listView.setAdapter(pdfAdapter);
pdfAdapter.notifyDataSetChanged();*/
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("canzone_cantante", canzone_cantante);
//contact.put("email", email);
// contact.put("mobile", mobile);
/* Toast.makeText(getApplicationContext(),
"LINK: "+link ,
Toast.LENGTH_LONG).show();*/
// adding contact to contact list
System.out.println("LINK: " + canzone_cantante);
contactList.add(contact);
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
PHP代码:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$canzone = $_POST['canzone'];
require_once 'dbDetails.php';
$con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die("Unable to connect");
$sql = "SELECT * FROM music where canzone = '$canzone'";
$result = mysqli_query($con,$sql);
//response array
$response = array();
$response['error'] = false;
$response['message'] = "Musics fetched successfully.";
$response['musics'] = array();
//traversing through all the rows
while($row =mysqli_fetch_array($result)){
$temp = array();
$temp['id'] = $row['id'];
$temp['canzone'] = $row['canzone'];
$temp['canzone_cantante'] = $row['canzone_cantante'];
$temp['url'] = $row['url'];
array_push($response['musics'],$temp);
}
echo json_encode($response);
}
您正在使用get request(inAndroid)发送canzone参数,但是尝试通过POST全局变量(在php中)获取它,因此我建议将您的php从$canzone= $_POST['canzone'];
到$canzone= $_GET['canzone'];
编辑也在这里更改if语句
if($_SERVER['REQUEST_METHOD']=='POST'){
至
if($_SERVER['REQUEST_METHOD']=='GET'){
您将歌曲名称发送为GET,而不是发布。
另外,如果歌曲名称中包含多个单词,则还需要对歌曲名称进行urlencode。
干杯:)
尝试替换此行
$canzone = $_POST['canzone'];
同
$canzone = $_REQUEST['canzone'];
据我了解,您从Android App中发布了这样的请求
String url = "http://blabla.org/AndroidMusicDownload/downloads/getMusic.php?canzone=" + canzone;
但是有一个问题,您在URL中发送了“ canzone”,所以这是GET参数,在PHP中,您可以从$ _POST抓取此变量,只需将$ _POST更改为$ _GET,就可以了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.