2

I need to send the json request to server using HttpPost. Here is my current code :

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

But it gives me 400, that means bad request.

Note : when base64 path is small then only it works, otherwise it is not working. Please help me.

4
  • What does the base64 path look like when it does work? And when it doesn't? Commented Aug 26, 2015 at 10:25
  • when the base64 path is small then it works, but when the base64 path is large then not working Commented Aug 26, 2015 at 10:27
  • Can you give an example of the actual path that worked/didn't work for you? Commented Aug 26, 2015 at 10:28
  • Both the path are works, when i check on browser. But is path length is too long then i am getting error like "File not found" Commented Aug 26, 2015 at 10:30

1 Answer 1

3

Please use this code:

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

                    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.