responses can be caught. Will take MAJOR refactoring to implement into the base application

This commit is contained in:
paul-loedige 2020-12-29 01:11:31 +01:00
parent 7186e84e13
commit 658d83c695
4 changed files with 54 additions and 3 deletions

View File

@ -37,6 +37,7 @@ dependencies {
implementation 'androidx.navigation:navigation-ui:2.2.2'
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
implementation 'com.android.volley:volley:1.1.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

View File

@ -4,6 +4,8 @@
<!-- permission to read and write the XML file -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- permission to access send http requests -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
@ -11,7 +13,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.APED">
android:theme="@style/Theme.APED"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:label="@string/app_name"

View File

@ -11,6 +11,7 @@ import android.view.MenuItem;
import android.view.Menu;
import android.widget.Toast;
import com.example.aped.communication.Communicator;
import com.example.aped.communication.IIO;
import com.example.aped.utils.ExternalStorageHandler;
import com.example.aped.communication.TestIO;
@ -63,6 +64,13 @@ public class MainActivity extends AppCompatActivity {
//checks that the permission to read and write the xml is granted
ensurePermissions();
setXML();
/** THIS IS A TEST **/
IIO io = new Communicator("192.168.2.246",8080,this);
String returnString = io.read("sensorarray");
System.out.println(returnString);
/** THIS IS A TEST **/
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

View File

@ -1,21 +1,60 @@
package com.example.aped.communication;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import kotlin.NotImplementedError;
public class Communicator implements IIO{
private String address;
private int port;
private RequestQueue requestQueue;
public Communicator(String address, int port){
public Communicator(String address, int port, Context context){
this.address = address;
this.port = port;
requestQueue = Volley.newRequestQueue(context);
}
@Override
public String read(String deviceName) {
throw new NotImplementedError();
String requestString = "http://" + address + ":" + port + "/device/" + deviceName;
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.GET,
requestString,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error.getMessage());
}
}
);
requestQueue.add(request);
return "";
}
@Override