responses can be caught. Will take MAJOR refactoring to implement into the base application
This commit is contained in:
parent
7186e84e13
commit
658d83c695
@ -37,6 +37,7 @@ dependencies {
|
|||||||
implementation 'androidx.navigation:navigation-ui:2.2.2'
|
implementation 'androidx.navigation:navigation-ui:2.2.2'
|
||||||
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
|
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
|
||||||
implementation 'androidx.lifecycle:lifecycle-viewmodel-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.+'
|
testImplementation 'junit:junit:4.+'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
<!-- permission to read and write the XML file -->
|
<!-- permission to read and write the XML file -->
|
||||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<!-- permission to access send http requests -->
|
||||||
|
<uses-permission android:name="android.permission.INTERNET" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
@ -11,7 +13,8 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
android:theme="@style/Theme.APED">
|
android:theme="@style/Theme.APED"
|
||||||
|
android:usesCleartextTraffic="true">
|
||||||
<activity
|
<activity
|
||||||
android:name=".MainActivity"
|
android:name=".MainActivity"
|
||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
|
@ -11,6 +11,7 @@ import android.view.MenuItem;
|
|||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.example.aped.communication.Communicator;
|
||||||
import com.example.aped.communication.IIO;
|
import com.example.aped.communication.IIO;
|
||||||
import com.example.aped.utils.ExternalStorageHandler;
|
import com.example.aped.utils.ExternalStorageHandler;
|
||||||
import com.example.aped.communication.TestIO;
|
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
|
//checks that the permission to read and write the xml is granted
|
||||||
ensurePermissions();
|
ensurePermissions();
|
||||||
setXML();
|
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);
|
setContentView(R.layout.activity_main);
|
||||||
Toolbar toolbar = findViewById(R.id.toolbar);
|
Toolbar toolbar = findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
|
@ -1,21 +1,60 @@
|
|||||||
package com.example.aped.communication;
|
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.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
import kotlin.NotImplementedError;
|
import kotlin.NotImplementedError;
|
||||||
|
|
||||||
public class Communicator implements IIO{
|
public class Communicator implements IIO{
|
||||||
private String address;
|
private String address;
|
||||||
private int port;
|
private int port;
|
||||||
|
private RequestQueue requestQueue;
|
||||||
|
|
||||||
public Communicator(String address, int port){
|
public Communicator(String address, int port, Context context){
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
requestQueue = Volley.newRequestQueue(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String read(String deviceName) {
|
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
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user