我在向PHP文件发送字符串参数时遇到问题,以便从编辑文本中下载插入歌曲名称的歌曲。我不明白我收到的错误。 在此先感谢您的帮助!
logcat的:
Response from url: {"error":false,"message":"Musics fetched successfully.","musics":[]}
我不知道为什么数组是空的。
如果我使用传入歌曲名称但不在URL中的其他客户端,则PHP文件可以正常工作。 这是我的代码:
ANDROID SIDE:
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);
}
答案 0 :(得分:3)
您正在使用get request(inAndroid)发送您的canzone参数,但尝试通过POST全局变量(在php中)获取它
所以我建议将你的PHP从$canzone= $_POST['canzone'];
更改为$canzone= $_GET['canzone'];
修改强> 也改变了if语句
if($_SERVER['REQUEST_METHOD']=='POST'){
到
if($_SERVER['REQUEST_METHOD']=='GET'){
答案 1 :(得分:1)
你发送的歌曲名称为GET而不是帖子。
如果歌曲名称中包含多个单词,则需要对该歌曲的名称进行urlencode。
干杯:)
答案 2 :(得分:0)
尝试替换此行
$canzone = $_REQUEST['canzone'];
与
let sb:UIStoryboard = UIStoryboard(name: "Second", bundle: nil)
let vc : SecondViewController = sb.instantiateInitialViewController() as! SecondViewController
// change to desired number of seconds
let when = DispatchTime.now() + 0.5
DispatchQueue.main.asyncAfter(deadline: when) {
SVProgressHUD.dismiss()
// Your code with delay
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let window:UIWindow = appDelegate.window!
UIView.transition(with: window, duration: 0.2, options: [UIViewAnimationOptions.transitionCrossDissolve], animations:{
}, completion: { (finished: Bool) -> () in
window.rootViewController = nil
window.rootViewController = vc
window.makeKeyAndVisible()
})
}
答案 3 :(得分:0)
据我所知,你从Android App发布了这样的请求
{{1}}
但是有一个问题是你在URL中发送'canzone',所以这是GET参数,在PHP中你从$ _POST中获取这个变量,只需将$ _POST更改为$ _GET,就可以了