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

时间:2015-08-26 10:12:46

标签: 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路径较小时,只有它工作,否则无法正常工作。 请帮帮我。

1 个答案:

答案 0 :(得分:3)

请使用此代码:

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);
                            }
                        });

                    }