首页 > 解决方案 > Uploading a JSON file with the same name to a PHP server takes effect after too much time

问题描述

I have some android users who need a JSON file to be downloaded from a PHP server
Whenever the JSON file needs any modification, I download it, modify the items and upload the file again for further use by the users

If the name of the modified file which is supposed to be uploaded is the same previous name, there is a problem and the problem is the server is sensitive to some file types
You upload some files successfully but when the users download the files, yet the old files are downloaded as if there is no new file uploaded

For example if the file type is JSON type, the problem exists but if it is PHP type there is no problem In fact it will take some time (the time is too much) for JSON type to be ready for download but PHP file is instantly ready for download
I called the server administrator and he said it's natural and HTML files have the same problem too
He said if I pay more money I can by virtual server which does not have that problem

Anyway I decided to rename the file each time I upload it to solve the problem

The process is as following:
1 - An http request is sent to the PHP code to identify the last uploaded file name
2 - PHP code searches the folder which contains the JSON file
3 - The name of the last uploaded file is identified and sent back to the client

Now that the file name is identified the file is downloadable and if need be the new file name for upload is identified too (Cause just 1 number is added to the previous number and then the file is uploaded)

The problem of such an process is speed of upload and download
Since 2 actions is necessary for each upload or download
1 - Identification of last uploaded file name
2 - Upload or download

Any suggestion?
Thanks

edit:
The code for download the JSON:

public class JSONHttpHandler {

    private static final String TAG = JSONHttpHandler.class.getSimpleName();

    public JSONHttpHandler() {
    }

    public String makeServiceCall(String reqUrl) {
        String response = null;
        try {
            URL url = new URL(reqUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setUseCaches(false);
            conn.setReadTimeout(5000);//this is in milliseconds
            conn.setConnectTimeout(5000);//this is in milliseconds
            conn.setRequestProperty("Content-type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestMethod("GET");

            // make GET request to the given URL
            //conn.connect();

            // read the response
            InputStream in = new BufferedInputStream(conn.getInputStream());
            response = convertStreamToString(in);
            Log.e(TAG, "2222222222222222222222222" + response);
        } catch (MalformedURLException e) {
            Log.e(TAG, "MalformedURLException: " + e.getMessage());
        } catch (ProtocolException e) {
            Log.e(TAG, "ProtocolException: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IOException: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
        return response;
    }

    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}

The code requested in comments for intentionally manipulating the JSON file for download test:

<?php
$myfile = fopen("j000000033w.json", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

标签: phpandroid

解决方案


1 - reqUrl 是服务器中 JSON 文件的 URL 好吗?

对于一个可以的测试。

但是对于你真正想要的不是。

那时您将使用一个 php 脚本来回显该文件。


推荐阅读