【问题标题】:Server Receive empty JSON string array From android app服务器从 android 应用程序接收空 JSON 字符串数组
【发布时间】:2017-09-11 18:31:30
【问题描述】:

我正在将图像数据从我的 android 应用程序发送到服务器,我可以在日志中看到字符串数组不是空的并且具有正确的数据,但在服务器上它接收到 null。

这是我向服务器发送数据的 AsyncTask

protected Void doInBackground(Void... params) {
        BufferedReader reader;

        try {

            URL url = new URL("http://www.example.com/Data/galleryLog.php");

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(true);

            Log.d(TAG,"String Data "+Images);

            //For POST Only - Begin
            OutputStream os = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            writer.write(Images);
            writer.flush();
            writer.close();
            os.close();
            connection.connect();
            //For POST Only End
            int responseCode = connection.getResponseCode();
            Log.d(TAG, "POST RESPONSE CODE " + responseCode);
            String LoginResult;
            if (responseCode == HttpURLConnection.HTTP_OK) {
                //Success
                Log.d(TAG, "Success connection with database");
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuffer response = new StringBuffer();

                while ((inputLine = reader.readLine()) != null) {
                    response.append(inputLine);
                }
                reader.close();

                Log.d(TAG, "Reader Close - Printing Result ");
                //Print Result
                Log.d(TAG, "Calling Response " + response.toString());
            }


            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

这是我在 logcat 中得到的字符串,它不为空并且具有正确的数据

String Data {"galleryDesc":"My First Gallery","images":[{"base64code":"Base64Image","type":"jpg","imagename":"image-101.jpg"}],"User_ID":"18","galleryTitle":"Image-101.jpg"}

我将接收到的数据打印到文件中的 PHP 代码,但它总是给我空白文件

<?php
$tm = 'ritu_gallery_'.time().'.log';
file_put_contents($tm, print_r($_POST, TRUE));

echo "Response<br>";
print_r($_POST);
?>

它像那样创建文本文件

数组( )

更新:这是 AsyncTask 字符串数组中的一个问题。我不知道为什么它不起作用。如果有人知道,请告诉我

当我将此字符串数组发送到服务器时,它会在服务器上打印空白值

 {"galleryDesc":"My First Gallery","images":[{"base64code":"Base64Image","type":"jpg","imagename":"image-101.jpg"}],"User_ID":"18","galleryTitle":"Image-101.jpg"}

当我将上面的字符串数组添加到这个

String data= URLEncoder.encode("Pic","UTF-8")+ "=" +URLEncoder.encode(Images,"UTF-8");

它像这样在服务器上创建文件

数组 ( [图片] => {"galleryDesc":"我的第一个画廊","images":[{"base64code":"Base64Image","type":"jpg","imagename":"101.jpg"}], "User_ID":"18","galleryTitle":"101.jpg"} )

所有正确的值。如果有人知道为什么会这样。请告诉我

【问题讨论】:

    标签: java php android arrays json


    【解决方案1】:

    您在这里访问了错误的变量,图像是一个文件,它将在 $_FILE 变量中而不是在 $_POST 变量中,并且表单应该是多部分表单数据,我不知道您是这样做还是不是因为我对 android 不太了解,但如果这样做,那么您可以在 $_FILE 变量中看到您的文件

    【讨论】:

    • 你能告诉我如何在 php 中查看文件吗因为我不擅长 php
    • 就像你在帖子里做的一样,很抱歉它是 $_FILES。 $_FILES['图片']
    • 其实我是从某个网站上复制了php代码,所以我不能像你说的那样修改
    • 您只需要从 $_FILES['images'] 访问图像,就可以像之前那样通过 $_POST 访问其余数据。如果您需要更多文档,这里是一个链接w3schools.com/php/php_file_upload.asp
    • 我想在文本文件中打印完整的字符串数组。请告诉我怎么做,因为我在你的建议中做错了
    猜你喜欢
    • 2012-05-05
    • 2019-07-26
    • 1970-01-01
    • 2012-05-03
    • 2017-04-11
    • 1970-01-01
    • 2020-09-25
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多