0

Alright so I have the following situation, I got a java app, where users can login, once they do it will generate a random key, I wish to send this key to my website using http requests.

On the web side I will insert it into the database for later use, this is already working.

Now at this moment I am very stuck with the java side. I am using the following code to try the http request:

private void startRegistration(String username, String password) {

    String myRandomString = randomString(6);
    String deviceId = "abc";
    startToast(myRandomString);

    try {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        insertDevice(myRandomString, deviceId);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@TargetApi(Build.VERSION_CODES.KITKAT)
public void insertDevice(String randomString, String deviceId) throws IOException {

//        String urlParameters  = "key=" + randomString + "&device_id="+ deviceId;
//        byte[] postData       = urlParameters.getBytes( StandardCharsets.UTF_8 );
//        int    postDataLength = postData.length;
    String request        = "http://website.com/test";
    URL    url            = new URL( request );

    HttpURLConnection conn= (HttpURLConnection) url.openConnection();
        conn.setDoOutput( true );
        conn.setInstanceFollowRedirects( false );
        conn.setRequestMethod( "GET" );
        conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty( "charset", "utf-8");
//            conn.setRequestProperty( "Content-Length", Integer.toString( postDataLength ));
        conn.setUseCaches( false );

    try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
//            wr.write( postData );
        Context context = getApplicationContext();
        int duration = Toast.LENGTH_SHORT;

        Toast toast = Toast.makeText(context, "test", duration);
        toast.show();
       }
   }

On the web side I have a route set to catch this request and simple throws a log::info so I can check if the request even ever gets there, this looks like follow:

public function create() {

    Log::info('I got here, hurray!');
}

now when I hit the URL in my browser I can see the log appearing, when I use my app, I don't. But there are no errors thrown either.

At the end I'd like to use a POST request and send both the random key, and device ID to the website. any help is much appreciated

3
  • you can use OkHttp Library. It will make your task much easier that way. Commented Sep 29, 2016 at 10:50
  • stackoverflow.com/questions/11766878/… following link will help you Commented Sep 29, 2016 at 11:23
  • you don't seem to be calling conn.connect()... Commented Sep 29, 2016 at 11:44

1 Answer 1

1

You are setting up the connection but you're not triggering the request. You can perform the request by calling conn.connect() or "operations that depend on being connected" (javadoc)

Here's an example of a POST request:

public void insertDevice(String randomString, String deviceId) {

    String request = "http://website.com/test";
    URL url = new URL(request);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setRequestMethod("POST");

    String urlParameters = "key=" + randomString + "&device_id=" + deviceId;
    byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);

    conn.setDoOutput(true);

    try (OutputStream os = conn.getOutputStream()) {
        os.write(postData);
    }

    conn.connect();

    // Get response code, e.g. 200.
    // conn.getResponseCode();

    // Read server response data with conn.getInputStream() or conn.getErrorStream().

    conn.disconnect();
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sadly this is still not popping my logs on the web server, but no errors are thrown either.
@killstreet not even a fixed version o GET hit the server?
never mind, it was working just the server log had a little delay, you are amazing :) thank you so much!

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.