在Android

时间:2016-01-13 05:33:05

标签: android file-upload android-volley android-async-http

我通常使用排球库进行网络服务,现在我想将文件(即图像,视频等)作为参数发送,就像姓名,国家,年龄或其他参数一样。哪个是最适合实现该功能的简单方法?我已尝试使用' android-async-http'但是我在Header中获得了弃用警告,其中包括#on; public void onSuccess(int statusCode,Header [] headers,JSONArray response)"如果我使用" useLibrary' org.apache.http.legacy'"然后错误转移到" super.onSuccess(statusCode,headers,response);"线。以下是使用的代码。任何帮助将受到高度赞赏。

代码:

private void sendRegisterData(String path) {

    String baseUrl = AppGlobal.BaseURL+"register";
    final ProgressDialog pd = new ProgressDialog(this);
    pd.setCancelable(false);
    pd.show();
    RequestParams params = new RequestParams();
    params.put("name", name_text);
    params.put("country", country_text);
    params.put("userType", userType_text);
    params.put("email", email_text);
    params.put("password", password_text);
    try {
        File file = bmpToFile(profileImage_bmp); //   Convert bmp to File to pass as parameter
        params.put("avatar", file);

    } catch (FileNotFoundException e1) {
        Toast.makeText(this,
                "Problem updating profile! Try later.", Toast.LENGTH_SHORT)
                .show();
        // new SnackBar(this,
        // "Problem updating profile! try later.").show();
        this.finish();
    } catch (IOException e) {
        e.printStackTrace();
    }

    AsyncHttpClient client = new AsyncHttpClient();

    client.post(this, baseUrl, params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONArray response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);

        }

        @Override
        public void onProgress(long bytesWritten, long totalSize) {
            // TODO Auto-generated method stub
            super.onProgress(bytesWritten, totalSize);
            int progressPercentage = (int) ((int) 100 * bytesWritten / totalSize);

        }

        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            Log.e("RESULT FRIEND LIST JO", response.toString() + "");

        }

        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              String responseString) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, responseString);
            Log.e("RESULT FRIEND LIST", responseString.toString() + "");

        }

        @Override
        public void onFailure(int statusCode, Header[] headers,
                              Throwable throwable, JSONObject errorResponse) {
            // TODO Auto-generated method stub
            super.onFailure(statusCode, headers, throwable, errorResponse);

            // Log.e("Upload Exeption", errorResponse.toString());
        }

        @Override
        public void onFailure(int statusCode, Header[] headers,
                              String responseString, Throwable throwable) {
            // TODO Auto-generated method stub
            super.onFailure(statusCode, headers, responseString, throwable);

        }

        @Override
        public void onFinish() {
            // TODO Auto-generated method stub
            super.onFinish();
            pd.dismiss();
        }
    });

0 个答案:

没有答案