0

I am posting data to server as single string. Its not posting. I am getting response.

I tried volley and retrofit. but I want to try in normal HTTP connection. I don't want to use other thing

    String api_url = "http://mywebsite.com/blah_blah";
  String book_now_request = "user_id=" + URLEncoder.encode(user_id, "UTF-8") + "&src=" + URLEncoder.encode(String.valueOf(src), "UTF-8") + "&src_lng=" + URLEncoder.encode(String.valueOf(src_lng) , "UTF-8" );



  JSONObject response_data = call_api(api_url, book_now_request);

  public JSONObject call_api(String api_url, String request_data) {
        try {
            URL url = new URL(api_url);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));

            Log.e("request_data", request_data);

            writer.write(request_data);
            writer.flush();
            writer.close();
            os.close();

            BufferedReader bufferedReader = new BufferedReader(new    InputStreamReader(conn.getInputStream()));
            String line = "";
            String response = "";
            while ((line = bufferedReader.readLine()) != null) {
                response += line;
            }

            Log.d("API response", response);

            JSONObject response_data = new JSONObject(response);
            Log.e("J Response",response_data.toString());
            return response_data;

        } catch (Exception e) {
        //   Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
            Log.e("Exception", e.toString());
        }

        return null;
    }
6
  • what exactly is the issue that you are facing Commented May 21, 2019 at 10:44
  • I'm getting wrong response that mean It's not post properly Commented May 21, 2019 at 10:46
  • Is this format correct? Commented May 21, 2019 at 10:46
  • If you are fetching data from body using POST method, then try to fetch data using raw data from server end. What response you are getting Commented May 21, 2019 at 10:47
  • I have to post this data to server. If this data is correct.. server will return success. or else it will return data not posted. I am getting data not posted. my doubt is this string will converted into json object and post or it will post as raw data Commented May 21, 2019 at 10:52

1 Answer 1

1

I can understand your problem. Your passing it as raw data. You need to format it to JSON data.

String book_now_request = "{\"user_id\":\"key\",\"key\":\"value\",\"key\":\"value\"}"

Just try this. Hope this will work. Have a nice day!

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

1 Comment

I was posting it like url. now it works. thanks again

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.