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