本文介绍了Retrofit 2在Postman和Localhost中工作,但不在Amazon AWS Live Server上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的改造实例在本地主机(Android代码和邮递员)上都可以正常工作,并且在邮递员上也可以正常工作.但是当我转到Amazon AWS实例(也称为在线)时,android部分无法正常工作.请在下面查看我的代码.来自android的错误日志是D/response: {"error":true,"message":"Required parameters are not available"}

My retrofit instance is working fine with localhost (both android code and postman), and working fine online with postman. But when I go to Amazon AWS instance (a.k.a online) the android part is not working. Please see my code below. The error log from android is D/response: {"error":true,"message":"Required parameters are not available"}

有趣的是,大文件大小为2M,小文件大小为24KB,可以很好地上传.我已经检查了php.ini文件,最大大小为25MB

Interestingly the problem is with large file sizes 2M, small file sizes 24KB are getting uploaded fine. I have already checked the php.ini file the max sizes there are 25MB

我正在关注本教程: https://www.simplifiedcoding.net/retrofit-upload-file-tutorial/

public interface Api {

    //the base URL for our API
    //make sure you are not using localhost
    //find the ip usinc ipconfig command
    String BASE_URL = "http://creative-thinker.com/files/images/secret_crushes/";

    //this is our multipart request
    //we have two parameters on is name and other one is description
    @Multipart
    @POST("Api.php?apicall=upload")
    Call<MyResponse> uploadImage(@Part("image\"; filename=\"myfile.jpg\" ")
                                         RequestBody file, @Part("desc") RequestBody desc);

}

我的对象类

public class MyResponse {
    public boolean error;
    String message;
}

我的片段

ivImageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        // Open The File Choose And Send To Activity For Result
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, 100);

    }
});

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && data != null) {
            //the image URI
            Uri selectedImage = data.getData();

            //calling the upload file method after choosing the file
            uploadFile(selectedImage, "My Image");
        }
    }

    private void uploadFile(Uri fileUri, String desc) {

        //creating a file
        File file = new File(getRealPathFromURI(fileUri));

        //creating request body for file
        RequestBody requestFile = RequestBody.create(MediaType.parse(getContext().getContentResolver().getType(fileUri)), file);
        RequestBody descBody = RequestBody.create(MediaType.parse("text/plain"), desc);

        //The gson builder
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        //creating retrofit object
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();


        //creating our api
        Api api = retrofit.create(Api.class);

        //creating a call and calling the upload image method
        Call<MyResponse> call = api.uploadImage(requestFile, descBody);

        //finally performing the call
        call.enqueue(new Callback<MyResponse>() {
            @Override
            public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
                if (!response.body().error) {
                    Toast.makeText(getContext().getApplicationContext(), "File Uploaded Successfully...", Toast.LENGTH_LONG).show();
                } else {

                    MyResponse res= response.body();
                    Log.d("response", new Gson().toJson(res));
                    Toast.makeText(getContext().getApplicationContext(), "Some error occurred...", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<MyResponse> call, Throwable t) {
                Toast.makeText(getContext().getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });
    }

    /*
     * This method is fetching the absolute path of the image file
     * if you want to upload other kind of files like .pdf, .docx
     * you need to make changes on this method only
     * Rest part will be the same
     * */
    private String getRealPathFromURI(Uri contentUri) {
        String[] proj = {MediaStore.Images.Media.DATA};
        CursorLoader loader = new CursorLoader(getContext(), contentUri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String result = cursor.getString(column_index);
        cursor.close();
        return result;
    }

FileHandler.php

FileHandler.php

<?php


class FileHandler
{

    private $con;

    public function __construct()
    {
        require_once dirname(__FILE__) . '/DbConnect.php';

        $db = new DbConnect();
        $this->con = $db->connect();
    }

    public function saveFile($file, $extension, $desc)
    {
        $name = round(microtime(true) * 1000) . '.' . $extension;
        $filedest = dirname(__FILE__) . UPLOAD_PATH . $name;
        move_uploaded_file($file, $filedest);

        $url = $server_ip = gethostbyname(gethostname());

        $stmt = $this->con->prepare("INSERT INTO images (description, image) VALUES (?, ?)");
        $stmt->bind_param("ss", $desc, $name);
        if ($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function getAllFiles()
    {
        $stmt = $this->con->prepare("SELECT id, description, image FROM images ORDER BY id DESC");
        $stmt->execute();
        $stmt->bind_result($id, $desc, $url);

        $images = array();

        while ($stmt->fetch()) {

            $temp = array();
            $absurl = 'http://' . gethostbyname(gethostname()) . '/files/images/secret_crushes' . UPLOAD_PATH . $url;
            $temp['id'] = $id;
            $temp['desc'] = $desc;
            $temp['url'] = $absurl;
            array_push($images, $temp);
        }

        return $images;
    }

}

Api.php

<?php


require_once dirname(__FILE__) . '/FileHandler.php';

$response = array();

if (isset($_GET['apicall'])) {
    switch ($_GET['apicall']) {
        case 'upload':

            if (isset($_POST['desc']) && strlen($_POST['desc']) > 0 && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
                $upload = new FileHandler();

                $file = $_FILES['image']['tmp_name'];

                $desc = $_POST['desc'];

                if ($upload->saveFile($file, getFileExtension($_FILES['image']['name']), $desc)) {
                    $response['error'] = false;
                    $response['message'] = 'File Uploaded Successfullly';
                }

            } else {
                $response['error'] = true;
                $response['message'] = 'Required parameters are not available';
            }

            break;

        case 'getallimages':

            $upload = new FileHandler();
            $response['error'] = false;
            $response['images'] = $upload->getAllFiles();

            break;
    }
}

echo json_encode($response);

function getFileExtension($file)
{
    $path_parts = pathinfo($file);
    return $path_parts['extension'];
}

我正在尝试使用

$response['message'] = $string;就在$response['message'] = 'Required parameters are not available';

并获得以下结果

    $string = $_POST['desc'];
    D/response: {"error":true,"message":"My Image"}

    $string = $_POST['desc'];
    $string = strlen($string);
    D/response: {"error":true,"message":"8"}

    $string = $_FILES['image']['error'];
    D/response: {"error":true}

    $string = UPLOAD_ERR_OK;
    D/response: {"error":true,"message":"0"}

并删除&& $_FILES['image']['error'] === UPLOAD_ERR_OK会将数据输入数据库,但仍然没有图像上传

And removing && $_FILES['image']['error'] === UPLOAD_ERR_OK enters the data into database but no image still uploaded

推荐答案

在网上搜索后终于找到了解决方案.问题是文件大小

Finally found the solution after searching the net... The issue was file size

我使用的是php 7和Apache.因此需要在两个地方更改配置

I am on php 7 and Apache. So the configuration needs to changed at two places

配置

upload_max_filesize = 25M
post_max_size = 25M
max_execution_time = 300
max_input_time = 300
memory_limit = 128M

位置

sudo nano /etc/php/7.0/fpm/php.ini
sudo nano /etc/php/7.0/apache2/php.ini

显然,需要更改Amazon AWS上PHP和Apache的设置.希望对别人有帮助.

Apparently the settings needs to be changed for PHP AND Apache on Amazon AWS. Hope it helps others.

这篇关于Retrofit 2在Postman和Localhost中工作,但不在Amazon AWS Live Server上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:30