Añadida funcionalidad para enviar datos a Gotify
This commit is contained in:
+5
-1
@@ -6,7 +6,7 @@ android {
|
||||
defaultConfig {
|
||||
minSdkVersion 19
|
||||
targetSdkVersion 24
|
||||
applicationId "com.journaldev.gpslocationtracking"
|
||||
applicationId "com.redp.geotrack"
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
@@ -17,6 +17,10 @@ android {
|
||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
|
||||
}
|
||||
}
|
||||
compileOptions {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
package com.journaldev.gpslocationtracking;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
public class HttpPostForm {
|
||||
private HttpURLConnection httpConn;
|
||||
private Map<String, Object> queryParams;
|
||||
private String charset;
|
||||
|
||||
/**
|
||||
* This constructor initializes a new HTTP POST request with content type
|
||||
* is set to multipart/form-data
|
||||
*
|
||||
* @param requestURL
|
||||
* @param charset
|
||||
* @param headers
|
||||
* @param queryParams
|
||||
* @throws IOException
|
||||
*/
|
||||
public HttpPostForm(String requestURL, String charset, Map<String, String> headers, Map<String, Object> queryParams) throws IOException {
|
||||
this.charset = charset;
|
||||
if (queryParams == null) {
|
||||
this.queryParams = new HashMap<>();
|
||||
} else {
|
||||
this.queryParams = queryParams;
|
||||
}
|
||||
URL url = new URL(requestURL);
|
||||
httpConn = (HttpURLConnection) url.openConnection();
|
||||
httpConn.setUseCaches(false);
|
||||
httpConn.setDoOutput(true); // indicates POST method
|
||||
httpConn.setDoInput(true);
|
||||
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||
if (headers != null && headers.size() > 0) {
|
||||
Iterator<String> it = headers.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
String key = it.next();
|
||||
String value = headers.get(key);
|
||||
httpConn.setRequestProperty(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public HttpPostForm(String requestURL, String charset, Map<String, String> headers) throws IOException {
|
||||
this(requestURL, charset, headers, null);
|
||||
}
|
||||
|
||||
public HttpPostForm(String requestURL, String charset) throws IOException {
|
||||
this(requestURL, charset, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a form field to the request
|
||||
*
|
||||
* @param name field name
|
||||
* @param value field value
|
||||
*/
|
||||
public void addFormField(String name, Object value) {
|
||||
queryParams.put(name, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a header to the request
|
||||
*
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
public void addHeader(String key, String value) {
|
||||
httpConn.setRequestProperty(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the request fields to a byte array
|
||||
*
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
private byte[] getParamsByte(Map<String, Object> params) {
|
||||
byte[] result = null;
|
||||
StringBuilder postData = new StringBuilder();
|
||||
for (Map.Entry<String, Object> param : params.entrySet()) {
|
||||
if (postData.length() != 0) {
|
||||
postData.append('&');
|
||||
}
|
||||
postData.append(this.encodeParam(param.getKey()));
|
||||
postData.append('=');
|
||||
postData.append(this.encodeParam(String.valueOf(param.getValue())));
|
||||
}
|
||||
try {
|
||||
result = postData.toString().getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* URL-encoding keys and values
|
||||
*
|
||||
* @param data
|
||||
* @return
|
||||
*/
|
||||
private String encodeParam(String data) {
|
||||
String result = "";
|
||||
try {
|
||||
result = URLEncoder.encode(data, "UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Completes the request and receives response from the server.
|
||||
*
|
||||
* @return String as response in case the server returned
|
||||
* status OK, otherwise an exception is thrown.
|
||||
* @throws IOException
|
||||
*/
|
||||
public String finish() throws IOException {
|
||||
String response = "";
|
||||
byte[] postDataBytes = this.getParamsByte(queryParams);
|
||||
httpConn.getOutputStream().write(postDataBytes);
|
||||
// Check the http status
|
||||
int status = httpConn.getResponseCode();
|
||||
if (status == HttpURLConnection.HTTP_OK) {
|
||||
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int length;
|
||||
while ((length = httpConn.getInputStream().read(buffer)) != -1) {
|
||||
result.write(buffer, 0, length);
|
||||
}
|
||||
response = result.toString(this.charset);
|
||||
httpConn.disconnect();
|
||||
} else {
|
||||
throw new IOException("Server returned non-OK status: " + status);
|
||||
}
|
||||
return response;
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import android.annotation.TargetApi;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.StrictMode;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
@@ -13,6 +14,9 @@ import android.widget.Button;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
|
||||
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
|
||||
@@ -26,6 +30,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private final static int ALL_PERMISSIONS_RESULT = 101;
|
||||
LocationTrack locationTrack;
|
||||
HashMap<String, String> listadoCoordenadas = new HashMap<String, String>();
|
||||
|
||||
SendMessage sendMessage = new SendMessage();
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -56,6 +63,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
public void onClick(View view) {
|
||||
|
||||
locationTrack = new LocationTrack(MainActivity.this);
|
||||
FIleManager fileManager = new FIleManager();
|
||||
|
||||
|
||||
if (locationTrack.canGetLocation()) {
|
||||
@@ -64,7 +72,12 @@ public class MainActivity extends AppCompatActivity {
|
||||
double longitude = locationTrack.getLongitude();
|
||||
double latitude = locationTrack.getLatitude();
|
||||
|
||||
Toast.makeText(getApplicationContext(), "Longitude:" + Double.toString(longitude) + "\nLatitude:" + Double.toString(latitude), Toast.LENGTH_SHORT).show();
|
||||
// Toast.makeText(getApplicationContext(), "Longitude:" + Double.toString(longitude) + "\nLatitude:" + Double.toString(latitude), Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(getApplicationContext(), "Latitud:" + Double.toString(latitude) + "\nLongitud:" + Double.toString(longitude), Toast.LENGTH_SHORT).show();
|
||||
fileManager.writeToFile("Latitud: "+Double.toString(latitude) + "\nLongitud:" + Double.toString(longitude), MainActivity.this);
|
||||
|
||||
listadoCoordenadas.put(Double.toString(latitude), Double.toString(longitude));
|
||||
|
||||
} else {
|
||||
|
||||
locationTrack.showSettingsAlert();
|
||||
@@ -73,9 +86,42 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
});
|
||||
|
||||
Button btn2 = (Button) findViewById(R.id.btn2);
|
||||
btn2.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
locationTrack = new LocationTrack(MainActivity.this);
|
||||
double longitude = locationTrack.getLongitude();
|
||||
double latitude = locationTrack.getLatitude();
|
||||
|
||||
Toast.makeText(getApplicationContext(), "Guardando datos", Toast.LENGTH_SHORT).show();
|
||||
//fileManager.writeToFile("Latitud: "+Double.toString(latitude) + "\nLongitud:" + Double.toString(longitude), MainActivity.this);
|
||||
|
||||
if (android.os.Build.VERSION.SDK_INT > 9)
|
||||
{
|
||||
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
|
||||
StrictMode.setThreadPolicy(policy);
|
||||
}
|
||||
|
||||
|
||||
sendMessage.sendMessage(Double.toString(latitude), Double.toString(longitude));
|
||||
Iterator it = listadoCoordenadas.entrySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Map.Entry pair = (Map.Entry)it.next();
|
||||
System.out.println("control"+pair.getKey() + " = " + pair.getValue());
|
||||
it.remove(); // avoids a ConcurrentModificationException
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private ArrayList<String> findUnAskedPermissions(ArrayList<String> wanted) {
|
||||
ArrayList<String> result = new ArrayList<String>();
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.journaldev.gpslocationtracking;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SendMessage {
|
||||
|
||||
public void sendMessage(String longitude, String latitude) {
|
||||
try {
|
||||
// Headers
|
||||
Map<String, String> headers = new HashMap<>();
|
||||
headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36");
|
||||
HttpPostForm httpPostForm = new HttpPostForm("https://msg.redp.icu/message?token=A9K-dBtytwSmJ-b", "utf-8", headers);
|
||||
// Add form field
|
||||
httpPostForm.addFormField("title", "Control");
|
||||
httpPostForm.addFormField("message", "longitud: " +longitude+" . Latitud: "+latitude);
|
||||
httpPostForm.addFormField("priority", "5");
|
||||
// Result
|
||||
String response = httpPostForm.finish();
|
||||
System.out.println(response);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user