我如何从我的android上载文件到Kloudless?

问题描述 投票:0回答:1

我想将文件从我的android内部存储器上传到任何云存储(例如Google驱动器,一个驱动器等),因为kloudless提供了一个API,可以使用accesstoken将文件上传到任何云存储,我想使用相同的api进行上传文件(https://api.kloudless.com/v1/accounts/accountid+/storage/files/)。我通过邮递员尝试过,我可以上传文件enter image description here

现在我尝试通过android volley,我能够在云中创建文件,但是其中没有数据。这是我的代码

public class MainActivity extends AppCompatActivity {
Button b;
TextView TV;
File myFile;
String responseString;
String path;
public String BASE_URL = "https://api.kloudless.com";
private static final int FILE_SELECT_CODE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TV = findViewById(R.id.textView);
    b = findViewById(R.id.button);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showFileChooser();
        }
    });
}

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    try {
        startActivityForResult(
                Intent.createChooser(intent, "Select a File to Upload"),
                FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
        Toast.makeText(this, "Please install a File Manager.",
                Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == FILE_SELECT_CODE) {
        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            Log.d("TAG", "File Uri: " + uri.toString());
            // Get the path
            String path = null;
            try {
                path = FileUtils.getPath(this, uri);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            Log.d("TAG", "File Path: " + path);
            try {
                data();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}
public void data() throws FileNotFoundException, UnsupportedEncodingException {
    final String url = BASE_URL + "/v1/accounts/" + "accountid" + "/storage/files/";

    final RequestQueue queue = Volley.newRequestQueue(this);
    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put( "file","somedata");

    JSONObject Body=new JSONObject(params);

    final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,Body, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            // response
            Log.d("Response", response.toString());
        }
    },
            new ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    // error
                    Log.d("Error.Response", error.toString());
                }
            }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("Authorization", "Bearer Bearerkey");
              params.put("Content-Type", "form-data");
            params.put("X-Kloudless-Metadata", "{\"parent_id\":\"root\",\"name\":\"testdone.txt\"}");
         //   params.put("Content-Length", Space);
            return params;
        }



    };
    queue.add(request);
}

请帮助我如何在请求正文中发送文件

android android-volley google-drive-android-api kloudless
1个回答
0
投票
我在Kloudless工作,虽然我对Android Volley不太熟悉,但是这里的问题似乎是您正在使用错误的内容类型设置JSON正文。为了复制Postman请求,您将需要使用分段文件上传,如下所示:How to upload file using Volley library in android?该文件需要添加到名为file的字段中,例如entity.addPart("file", new FileBody(new File("....")));

为了使服务器端(对于较大的文件)有更有效的处理,执行的请求应在请求正文中包含二进制文件内容,并具有标头Content-Type: application/octet-stream。基于一些粗略的搜索,Volley库似乎并不那么简单,因此最好尝试使用其他库。

© www.soinside.com 2019 - 2024. All rights reserved.