1
According to the comments from some users of this forum, I read some articles talking about more or less what I wanted to do.
- Read article:
From of this I I tried to create my own code, however I’m with serious problems, which are:
1) I don’t know how to "join" the Jsonrestfull class with the Shopping Class, so I can reuse the Json code for others classes.
2) Using the question (1), do not know how to take the data found in JSON and make a comparison. 3) I don’t know if the code is correct.
Jsonrestfull.java
package com.vuforia.samples.Books.app.Neoris;
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* Created by th on 30/06/17.
*/
public class JSONRestFull {
// Define o URL do Servidor para obter os dados do Book
private static final String LOGTAG = "Json";
private String mServerJsonURL = "http://www.teste.com/shopping.json";
private String mBookDataJSONFullUrl;
// Indica se o aplicativo está atualmente carregando os dados do Book
private boolean mIsLoadingBookData = false;
// Dados do Shopping ativo
private Shopping mShoppingData;
/**
* Obtém os dados do Book de um objeto JSON
*/
private class GetBookDataTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
mIsLoadingBookData = true;
// Inicialize o url completo do BOOK atual para pesquisar
// para os dados do JSON
StringBuilder sBuilder = new StringBuilder();
sBuilder.append(mServerJsonURL);
mBookDataJSONFullUrl = sBuilder.toString();
}
protected Void doInBackground(Void... params) {
HttpURLConnection connection = null;
try {
// Conecta ao Servidor para obter os dados do Book no JSON
URL url = new URL(mBookDataJSONFullUrl);
conectarServidorJson(url);
int status = connection.getResponseCode();
// Verifica se o URL do JSON existe e a conexão
// foi bem sucedida
if (status != HttpURLConnection.HTTP_OK) {
// Limpa as variáveis de dados da Classe
mShoppingData = null;
}
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
// Limpa qualquer referência antiga dos mData
limpaReferenciaAntiga(mShoppingData);
JSONObject jsonObject = new JSONObject(builder.toString());
// Gera um novo objeto do JSON Shopping
mShoppingData = new Shopping();
geraNovoObjetoJson(jsonObject, mShoppingData);
} catch (Exception e) {
Log.d(LOGTAG, "Couldn't get Json's. e: " + e);
} finally {
connection.disconnect();
}
return null;
}
}
public void setmServerJsonURL(String mServerJsonURL) {
this.mServerJsonURL = mServerJsonURL;
}
private void conectarServidorJson(URL url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.connect();
}
private void limpaReferenciaAntiga (Shopping mShoppingData){
if(mShoppingData != null){
mShoppingData = null;
}
}
private void geraNovoObjetoJson (JSONObject jsonObject, Shopping mShoppingData) throws JSONException {
// Gera os Objetos Json do Shopping
mShoppingData.setNome(jsonObject.getString("nome"));
mShoppingData.setLicenseKey(jsonObject.getString("licenseKey"));
mShoppingData.setAccessKey(jsonObject.getString("acessKey"));
mShoppingData.setSecretKey(jsonObject.getString("secretKey"));
mShoppingData.setLAT_1(jsonObject.getDouble("LAT_1"));
mShoppingData.setLAT_2(jsonObject.getDouble("LAT_2"));
mShoppingData.setLONG_1(jsonObject.getDouble("LONGI_1"));
mShoppingData.setLONG_2(jsonObject.getDouble("LONGI_2"));
}
}
Shopping.json
{
"shoppingsObj": {
"shoppings": [{
"nome": "Bangu Shopping",
"licenseKey": "Ad84Z0z/////AAAAGSgcOhPVvkoniWypHW2Dfsw+iX69si/",
"acessKey": "f854427",
"secretKey": "16208b",
"LAT_1": ["-22.879832"],
"LAT_2": ["-22.877738"],
"LONGI_1:": ["-43.468601"],
"LONGI_2:": ["-43.465978"]
},
{
"nome": "Boulevard Shopping Campos",
"licenseKey": "AULCxLD/////AAAAGa6JoRhAAk70lshljOUpGeN7XUgbJ/",
"acessKey": "6ad29c",
"secretKey": "370db",
"LAT_1": ["-21.755484"],
"LAT_2": ["-21.753139"],
"LONGI_1:": ["-41.350870"],
"LONGI_2:": ["-41.346417"]
}
]
}
}
Do you want to read the right Shopping.json file?! What is the problem with Jsonrestfull.java? You then want to use this data fetched from json in another class?!
– viana
Are you using parcelable or serializable?
– viana
@acklay , 1) I want to read shopping.json and use shoppingObj.lat and shopping.Long comparing and traversing all of its vectors, to check if the user is within the perimeter. If he is inside, he will walk through the mall.nome and shopping.Keys of this mall and will make the "set". 2) I don’t know if this Jsonrestfull code is correct. 3) I can’t tell you, I only read the topic I left in the question.
– Thiago Saad