0

This question has been asked numerous times and my apologies for asking again. I've tried following numerous examples (Java - sending HTTP parameters via POST method easily) and blogs all without success.

I have this method

//POST
public static void MakeRequest(String uri, String data, boolean isError) {
    try {
        Log.i(TAG, data);
        Log.i(TAG, uri);

        byte[] bytes =data.getBytes();
        // 2. make Connection
        HttpURLConnection urlConnection = (HttpURLConnection) ((new URL(uri).openConnection()));
        urlConnection.setRequestMethod("POST" );
        urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("charset", "utf-8");
        urlConnection.setRequestProperty("Content-Length", "" + Integer.toString(bytes.length));
        urlConnection.setInstanceFollowRedirects(false);
        urlConnection.setDoOutput(true);
        urlConnection.setUseCaches (false);

        DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream ());
        wr.write(bytes);

        Log.i(TAG, "after send?");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

For making a call to my service. The service works fine for posting as I've been testing with Postman all with success. With my limited knowledge of Android/Java I am having a difficult time in figuring out why the above isn't working. I've walked through the debugger and no errors are thrown, the correct URL is used. What I cna't figure out is why the service is never being reaching.

Community
  • 1
  • 1
Micah Montoya
  • 757
  • 1
  • 8
  • 24

1 Answers1

0
 URL loginUrl = new URL(Activity.getString(R.string.base_url) + "/users");
        HttpURLConnection loginConnection = (HttpURLConnection) loginUrl.openConnection();

        loginConnection.setRequestProperty("X-HTTP-Method-Override", "POST");
        loginConnection.setRequestMethod("POST");
        loginConnection.setRequestProperty("Content-Type", "application/json");
        loginConnection.setUseCaches(false);
        loginConnection.setDoInput(true);
        loginConnection.setDoOutput(true);
        loginConnection.connect();

        JSONObject requestJson = new JSONObject();
        requestJson.put("username", username);
        requestJson.put("email", email);
        requestJson.put("password", password);

        DataOutputStream printout = new DataOutputStream(loginConnection.getOutputStream());
        printout.writeBytes(requestJson.toString());
        printout.flush();
        printout.close();

I use this to post JSON object, you can tailor it to your requirements. Chaining the headers and the URL, and posting a different Object.

Rohan Sood
  • 178
  • 1
  • 14
  • Copied this and adjusted but it still didn't work. No errors or anything. Weird thing is that the "get" operations do just fine. I'm so stumped. – Micah Montoya Jul 08 '16 at 23:45