无法将图像文件上传到API服务器

时间:2016-05-22 00:42:33

标签: java android web-services api file-upload

我正在尝试将文件图像上传到我的api服务器,但它会返回:

{"Version":"1.0.0","StatusCode":415,"ErrorMessage":null,"Result":null}

我应该使用File而不是base64来上传它,我的api可以和iphone用户一起使用但是在android中这不是一个问题 这是我的代码:

  protected String doInBackground(Void... params) {
        String URL="http://api.nokhbatalatbaq.com/api/MemberPosts?PackageID=2&Subject=Subject&Body=Body";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(URL);
            httppost.setHeader("Content-Type", "form-data");
            ObscuredSharedPreferences prefs = ObscuredSharedPreferences.getPrefs(mActivity, "nokhbatalatbaq", Context.MODE_PRIVATE);
            httppost.setHeader("Authorization ", "bearer " + prefs.getString("accessToken", null));
            httppost.setHeader("Accept", "application/json");
            MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            if (selectedImagePath !=null) {
                File file = new File(selectedImagePath);
                FormBodyPart part2 = new FormBodyPart("file", new FileBody(file,  getMimeType(selectedImagePath))); // selectedImagePath =/storage/sdcard0/DCIM/image1463877649974.png
                mpEntity.addPart(part2);
                httppost.setEntity(mpEntity);
            }
            HttpResponse response = httpclient.execute(httppost);
            String st = EntityUtils.toString(response.getEntity());
            Log.v("log_tag", "In the try Loop" + st);

        } catch (Exception e) {
            Log.v("log_tag", "Error in http connection " + e.toString());
        }
        return "Success";

    }

0 个答案:

没有答案