简体   繁体   English

如何正确将参数传递给PHP文件

[英]How can I pass parameters to PHP files correctly

I have problem sending a string parameter to a PHP file to download a song inserting the song's name from a edit text. 我在将字符串参数发送到PHP文件以从编辑文本中下载插入歌曲名称的歌曲时遇到问题。 I don't understand the error I'm receiving. 我不明白我收到的错误。 Thanks in advance for the help! 先谢谢您的帮助!

LOGCAT: logcat的:

Response from url: {"error":false,"message":"Musics fetched successfully.","musics":[]}

i don't know why the array is empty. 我不知道为什么数组为空。

The PHP file works if i use a rest client passing the song's name but not in the URL. 如果我使用其他客户端传递歌曲名称而不是URL,则PHP文件有效。 This is my code: 这是我的代码:

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 CODE: 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);
}

You are sending your canzone parameter with get request( inAndroid) but trying to get it by POST global variable(in php) so i suggest changing your php from $canzone= $_POST['canzone']; 您正在使用get request(inAndroid)发送canzone参数,但是尝试通过POST全局变量(在php中)获取它,因此我建议将您的php从$canzone= $_POST['canzone']; to $canzone= $_GET['canzone']; $canzone= $_GET['canzone'];

EDIT also change the if statement here 编辑也在这里更改if语句

if($_SERVER['REQUEST_METHOD']=='POST'){

to

if($_SERVER['REQUEST_METHOD']=='GET'){

You send song name as GET not like post. 您将歌曲名称发送为GET,而不是发布。

Also you need to urlencode name of a song, if it has more then one word in name. 另外,如果歌曲名称中包含多个单词,则还需要对歌曲名称进行urlencode。

Cheers :) 干杯:)

Try with replacing this line 尝试替换此行

 $canzone = $_POST['canzone'];

with

 $canzone = $_REQUEST['canzone'];

As I understood you post the request like this from Android App 据我了解,您从Android App中发布了这样的请求

String url = "http://blabla.org/AndroidMusicDownload/downloads/getMusic.php?canzone=" + canzone;

But there is a problem that you send 'canzone' in URL, so this is GET parameter, and in the PHP you grab this variable from $_POST, just change $_POST to $_GET, should work 但是有一个问题,您在URL中发送了“ canzone”,所以这是GET参数,在PHP中,您可以从$ _POST抓取此变量,只需将$ _POST更改为$ _GET,就可以了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM