android - 如何在 Android 中使用 HTTP POST 将图像作为 JSON 中的 base64 字符串发送?

标签 android json base64 http-post

我需要使用 HttpPost 将 json 请求发送到服务器。 这是我当前的代码:

public static String makeRequest(String uri, String json) {
        HttpURLConnection urlConnection;

        String data = json;
        String result = null;
        try {
            //Connect 
            urlConnection = (HttpURLConnection) ((new URL(uri).openConnection()));
            urlConnection.setDoOutput(true);
            urlConnection.setRequestProperty("Content-Type", "application/json");
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.setRequestMethod("POST");
            urlConnection.connect();

            //Write
            OutputStream outputStream = urlConnection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            writer.write(data);
            writer.close();
            outputStream.close();

            //Read
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));

            String line = null;
            StringBuilder sb = new StringBuilder();

            while ((line = bufferedReader.readLine()) != null) {
                System.out.println("Uploading............");
                sb.append(line);
            }

            bufferedReader.close();
            result = sb.toString();
            System.out.println("Response : " +result);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

但它给了我 400,这意味着错误的请求。

注意:当base64路径很小时才有效,否则无效。 请帮助我。

最佳答案

请使用此代码:

try {

                        HttpURLConnection urlConnection;


                        String result = "";
                        try {
                            String data ="";
                            data = jsonObj.toString();
                            //          String temp=URLEncoder.encode(uri, "UTF-8");
                            URL url = new URL(WebServiceConstants.getMethodUrl(WebServiceConstants.METHOD_UPDATEVENDER));
                            urlConnection = (HttpURLConnection) ((url.openConnection()));
                            urlConnection.setDoInput(true);
                            urlConnection.setDoOutput(true);
                            urlConnection.setUseCaches(false);
                            urlConnection.setChunkedStreamingMode(1024);

                            urlConnection.setRequestProperty("Content-Type", "application/json");
                            urlConnection.setRequestProperty("Accept", "application/json");
                            urlConnection.setRequestMethod("POST");
                            urlConnection.connect();

                            //Write
                            OutputStream outputStream = urlConnection.getOutputStream();
                            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                            writer.write(data);
                            writer.close();
                            outputStream.close();

                            //Read
                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));

                            String line = null;
                            StringBuilder sb = new StringBuilder();

                            while ((line = bufferedReader.readLine()) != null) {
                                System.out.println("Uploading............");
                                sb.append(line);
                            }

                            bufferedReader.close();
                            _responseMain = sb.toString();
                            System.out.println("Response of Image Upload : " +_responseMain);


                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        //                      makeRequest(WebServiceConstants.getMethodUrl(WebServiceConstants.METHOD_UPDATEVENDER), jsonObj.toString());
                    } 
                    catch (Exception e) {
                        // TODO: handle exception
                        e.printStackTrace();

                        runOnUiThread( new Runnable() {
                            public void run() 
                            {
                                Constant.showAlertDialog("Message",getResources().getString(R.string.communicationError), VendorEditProfile.this, false);
                            }
                        });

                    }

关于android - 如何在 Android 中使用 HTTP POST 将图像作为 JSON 中的 base64 字符串发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32223789/

相关文章:

Android - 创建自定义 SearchView

javascript - 为国家、州/省/地区和城市选择菜单加载数据的 API 或数据库

java - 通过 UUID 生成密码重置 token

firefox - 内联 base64 编码链接在 Firefox 中不起作用

android - TextView文本值刷新onclick

android - 具有不同边距/填充的移动和桌面中的相同 CSS

android - 在销毁时从未调用过服务

xml - 在 RestEasy 中从 ExceptionMapper 返回 JSON 的简单方法?

使用 GSON 的 Java 对象到 JSON

HTTP 内容协商/压缩 : Use Base64 with Accept-Encoding/Content-Encoding?