added custom toast for the connection errors

This commit is contained in:
paul-loedige 2021-01-02 20:33:30 +01:00
parent 27f8312f51
commit eeebec4d32
3 changed files with 73 additions and 19 deletions

4
APED/.idea/gradle.xml generated
View File

@ -10,8 +10,8 @@
<option name="gradleJvm" value="1.8" /> <option name="gradleJvm" value="1.8" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$USER_HOME$/Storage/Programs/TH/MO_Mobile_Systeme/aped_app/APED" />
<option value="$PROJECT_DIR$/app" /> <option value="$USER_HOME$/Storage/Programs/TH/MO_Mobile_Systeme/aped_app/APED/app" />
</set> </set>
</option> </option>
<option name="resolveModulePerSourceSet" value="false" /> <option name="resolveModulePerSourceSet" value="false" />

View File

@ -1,13 +1,24 @@
package com.example.aped.communication; package com.example.aped.communication;
import android.content.Context; import android.content.Context;
import android.graphics.Color;
import android.util.Log; import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.NoConnectionError;
import com.android.volley.Request; import com.android.volley.Request;
import com.android.volley.RequestQueue; import com.android.volley.RequestQueue;
import com.android.volley.Response; import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest; import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley; import com.android.volley.toolbox.Volley;
import com.example.aped.MainActivity;
import com.example.aped.R;
import com.example.aped.utils.ExternalStorageHandler; import com.example.aped.utils.ExternalStorageHandler;
import org.json.JSONObject; import org.json.JSONObject;
@ -29,21 +40,21 @@ public class Communicator {
/** Request queue used for asynchronous requests. **/ /** Request queue used for asynchronous requests. **/
private final RequestQueue requestQueue; private final RequestQueue requestQueue;
/**The context of the application.**/ /**The context of the application.**/
private final Context context; private final MainActivity mainActivity;
/** /**
* Constructor of the Communicator. * Constructor of the Communicator.
* @param pAddress the ipv4 address of the raspberry * @param pAddress the ipv4 address of the raspberry
* @param pPort the port of the raspberry * @param pPort the port of the raspberry
* @param pContext the context of the application * @param pMainActivity the main activity
*/ */
public Communicator(final String pAddress, public Communicator(final String pAddress,
final int pPort, final int pPort,
final Context pContext) { final MainActivity pMainActivity) {
this.address = pAddress; this.address = pAddress;
this.port = pPort; this.port = pPort;
requestQueue = Volley.newRequestQueue(pContext); requestQueue = Volley.newRequestQueue(pMainActivity);
this.context = pContext; this.mainActivity = pMainActivity;
} }
/** /**
@ -62,8 +73,7 @@ public class Communicator {
url, url,
null, null,
responseListener, responseListener,
error -> Log.e("Communicator", error -> handleError(error, "READ")
"Error during READ: " + error.getMessage())
); );
requestQueue.add(request); requestQueue.add(request);
} }
@ -86,8 +96,7 @@ public class Communicator {
url, url,
message, message,
responseListener, responseListener,
error -> Log.e("Communicator", error -> handleError(error, "WRITE")
"Error during WRITE: " + error.getMessage())
); );
requestQueue.add(request); requestQueue.add(request);
} }
@ -97,15 +106,14 @@ public class Communicator {
*/ */
public void uploadXML() throws FileNotFoundException { public void uploadXML() throws FileNotFoundException {
File xmlFile = new File(ExternalStorageHandler File xmlFile = new File(ExternalStorageHandler
.getExternalPrivateStorageDir(context), "config.xml"); .getExternalPrivateStorageDir(mainActivity), "config.xml");
String url = "http://" + address + ":" + port + "/XML/"; String url = "http://" + address + ":" + port + "/XML/";
FileOutputVolleyRequest request = new FileOutputVolleyRequest( FileOutputVolleyRequest request = new FileOutputVolleyRequest(
Request.Method.POST, Request.Method.POST,
url, url,
xmlFile, xmlFile,
response -> System.out.println(response.toString()), response -> System.out.println(response.toString()),
error -> Log.e("Communicator", error -> handleError(error, "UPLOAD XML")
"Error during XML UPLOAD" + error.getMessage())
); );
requestQueue.add(request); requestQueue.add(request);
} }
@ -122,10 +130,8 @@ public class Communicator {
try { try {
if (response != null) { if (response != null) {
String xmlPath = ExternalStorageHandler String xmlPath = ExternalStorageHandler
.getExternalPrivateStorageDir(context); .getExternalPrivateStorageDir(mainActivity);
File xmlFile = new File(xmlPath, "config.xml"); File xmlFile = new File(xmlPath, "config.xml");
/*create the default XML config by using the
default.xml from the assets folder*/
if (xmlFile.exists()) { if (xmlFile.exists()) {
xmlFile.delete(); xmlFile.delete();
} }
@ -141,9 +147,30 @@ public class Communicator {
+ e.getMessage()); + e.getMessage());
} }
}, },
error -> Log.e("Communicator", error -> handleError(error,"XML DOWNLOAD")
"Error during XML DOWNLOAD: " + error.getMessage())
); );
requestQueue.add(request); requestQueue.add(request);
} }
private void handleError(VolleyError error, String method){
if (error instanceof ServerError){
Log.e("Communicator","server error during " + method + ": " + error.getMessage());
} else if (error instanceof NoConnectionError) {
makeToast("NO CONNECTION");
} else if (error instanceof TimeoutError) {
makeToast("CONNECTION TIMEOUT");
} else {
Log.e("Communicator","error during " + method + ": " + error.getMessage());
}
}
private void makeToast(String message){
Toast toast = new Toast(mainActivity);
View toastView = mainActivity.getLayoutInflater().inflate(R.layout.volley_toast, (ViewGroup) mainActivity.findViewById(R.id.volleyToastContainer));
TextView textView = toastView.findViewById(R.id.text);
textView.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastView);
toast.show();
}
} }

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/volleyToastContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/red"
app:cardCornerRadius="10dp">
<TextView
android:id="@+id/text"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:padding="10dp"
android:textStyle="bold"
android:textSize="20dp"/>
</androidx.cardview.widget.CardView>
</RelativeLayout>