Initial commit
This commit is contained in:
51
app/src/main/java/com/janasroboter/MainActivity.java
Normal file
51
app/src/main/java/com/janasroboter/MainActivity.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package com.janasroboter;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.os.StrictMode;
|
||||
import android.view.View;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
|
||||
.detectDiskReads()
|
||||
.detectDiskWrites()
|
||||
.detectNetwork()
|
||||
.penaltyLog()
|
||||
.build());
|
||||
}
|
||||
|
||||
|
||||
public void btnToggle(View view) {
|
||||
try {
|
||||
REST.getString("http://192.168.4.1/toggle");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Toast.makeText(this, "Obacht", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
public void btnLos(View view) {
|
||||
EditText state = view.getRootView().findViewById(R.id.state);
|
||||
String nummer = state.getText().toString();
|
||||
try {
|
||||
REST.getString("http://192.168.4.1/togglearg?state=" + nummer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Toast.makeText(this, "Obacht", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
|
||||
}
|
54
app/src/main/java/com/janasroboter/REST.java
Normal file
54
app/src/main/java/com/janasroboter/REST.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package com.janasroboter;
|
||||
|
||||
import java.io.IOException;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
public class REST {
|
||||
|
||||
// Initialize the http client
|
||||
private static OkHttpClient client = new OkHttpClient();
|
||||
|
||||
// Initialize the JSON mediatype used for post requests
|
||||
private static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
|
||||
private static final MediaType PATCHTYPE = MediaType.parse("application/json-patch+json");
|
||||
private static final MediaType MEDIA_TYPE_MP4 = MediaType.parse("video/mp4");
|
||||
|
||||
|
||||
/**
|
||||
* Make a GET request to a url
|
||||
* @param url the url string
|
||||
* @return content of the site body
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String getString(String url) throws IOException {
|
||||
|
||||
Request request = new Request.Builder().url(url).build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
return response.body().string();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a GET request with an authentication token
|
||||
* @param url the url string
|
||||
* @param authtoken string containing the apikey/authentication token
|
||||
* @return content of the site body
|
||||
* @throws IOException
|
||||
*/
|
||||
public static String getString(String url, String authtoken) throws IOException {
|
||||
// Create a request with an additional authorization header
|
||||
Request request = new Request.Builder().header("Authorization", authtoken)
|
||||
.url(url).build();
|
||||
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
return response.body().string();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user