added custom toast for the connection errors
This commit is contained in:
parent
27f8312f51
commit
eeebec4d32
4
APED/.idea/gradle.xml
generated
4
APED/.idea/gradle.xml
generated
@ -10,8 +10,8 @@
|
||||
<option name="gradleJvm" value="1.8" />
|
||||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/app" />
|
||||
<option value="$USER_HOME$/Storage/Programs/TH/MO_Mobile_Systeme/aped_app/APED" />
|
||||
<option value="$USER_HOME$/Storage/Programs/TH/MO_Mobile_Systeme/aped_app/APED/app" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="resolveModulePerSourceSet" value="false" />
|
||||
|
@ -1,13 +1,24 @@
|
||||
package com.example.aped.communication;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
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.RequestQueue;
|
||||
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.Volley;
|
||||
import com.example.aped.MainActivity;
|
||||
import com.example.aped.R;
|
||||
import com.example.aped.utils.ExternalStorageHandler;
|
||||
|
||||
import org.json.JSONObject;
|
||||
@ -29,21 +40,21 @@ public class Communicator {
|
||||
/** Request queue used for asynchronous requests. **/
|
||||
private final RequestQueue requestQueue;
|
||||
/**The context of the application.**/
|
||||
private final Context context;
|
||||
private final MainActivity mainActivity;
|
||||
|
||||
/**
|
||||
* Constructor of the Communicator.
|
||||
* @param pAddress the ipv4 address 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,
|
||||
final int pPort,
|
||||
final Context pContext) {
|
||||
final MainActivity pMainActivity) {
|
||||
this.address = pAddress;
|
||||
this.port = pPort;
|
||||
requestQueue = Volley.newRequestQueue(pContext);
|
||||
this.context = pContext;
|
||||
requestQueue = Volley.newRequestQueue(pMainActivity);
|
||||
this.mainActivity = pMainActivity;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -62,8 +73,7 @@ public class Communicator {
|
||||
url,
|
||||
null,
|
||||
responseListener,
|
||||
error -> Log.e("Communicator",
|
||||
"Error during READ: " + error.getMessage())
|
||||
error -> handleError(error, "READ")
|
||||
);
|
||||
requestQueue.add(request);
|
||||
}
|
||||
@ -86,8 +96,7 @@ public class Communicator {
|
||||
url,
|
||||
message,
|
||||
responseListener,
|
||||
error -> Log.e("Communicator",
|
||||
"Error during WRITE: " + error.getMessage())
|
||||
error -> handleError(error, "WRITE")
|
||||
);
|
||||
requestQueue.add(request);
|
||||
}
|
||||
@ -97,15 +106,14 @@ public class Communicator {
|
||||
*/
|
||||
public void uploadXML() throws FileNotFoundException {
|
||||
File xmlFile = new File(ExternalStorageHandler
|
||||
.getExternalPrivateStorageDir(context), "config.xml");
|
||||
.getExternalPrivateStorageDir(mainActivity), "config.xml");
|
||||
String url = "http://" + address + ":" + port + "/XML/";
|
||||
FileOutputVolleyRequest request = new FileOutputVolleyRequest(
|
||||
Request.Method.POST,
|
||||
url,
|
||||
xmlFile,
|
||||
response -> System.out.println(response.toString()),
|
||||
error -> Log.e("Communicator",
|
||||
"Error during XML UPLOAD" + error.getMessage())
|
||||
error -> handleError(error, "UPLOAD XML")
|
||||
);
|
||||
requestQueue.add(request);
|
||||
}
|
||||
@ -122,10 +130,8 @@ public class Communicator {
|
||||
try {
|
||||
if (response != null) {
|
||||
String xmlPath = ExternalStorageHandler
|
||||
.getExternalPrivateStorageDir(context);
|
||||
.getExternalPrivateStorageDir(mainActivity);
|
||||
File xmlFile = new File(xmlPath, "config.xml");
|
||||
/*create the default XML config by using the
|
||||
default.xml from the assets folder*/
|
||||
if (xmlFile.exists()) {
|
||||
xmlFile.delete();
|
||||
}
|
||||
@ -141,9 +147,30 @@ public class Communicator {
|
||||
+ e.getMessage());
|
||||
}
|
||||
},
|
||||
error -> Log.e("Communicator",
|
||||
"Error during XML DOWNLOAD: " + error.getMessage())
|
||||
error -> handleError(error,"XML DOWNLOAD")
|
||||
);
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
27
APED/app/src/main/res/layout/volley_toast.xml
Normal file
27
APED/app/src/main/res/layout/volley_toast.xml
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user