繁体   English   中英

服务器从android应用接收空的JSON字符串数组

[英]Server Receive empty JSON string array From android app

我正在将图像数据从我的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字符串数组中的问题。 我不知道为什么它不起作用。如果有人知道,请告诉我

当我将此字符串数组发送到server.it在服务器上打印空白值

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

当我在上面添加String数组时

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

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

Array([Pics] => {“ galleryDesc”:“我的第一画廊”,“图像”:[{“ base64code”:“ Base64Image”,“ type”:“ jpg”,“ imagename”:“ 101.jpg”} ],“ User_ID”:“ 18”,“ galleryTitle”:“ 101.jpg”})

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

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

暂无
暂无

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

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