Receiving Null in GET in a Class on Android

0

I'm trying to pass information from a Class that is Activity and another that is a DialogFragment for an Extended Class to Activity.

I get the JSON information correctly and in SET it passes the value, but in the other Class where the GET & SET says it is NULL.

AboutScreen.java

package com.vuforia.samples.Books.ui.ActivityList;


import com.vuforia.samples.Books.R;
import com.vuforia.samples.Books.app.Neoris.GPSTracker;
import com.vuforia.samples.Books.app.Neoris.ShoppingJsonController;
import com.vuforia.samples.Books.ui.ActivityFragmentList.GPSFragmentActivity;

import android.app.ProgressDialog;
import android.os.Bundle;;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
;


public class AboutScreen extends FragmentActivity {

    // Botões
    private Button btnLocalizar;

    // ProgressDialog JSON SHOPPING
    private ProgressDialog pDialog;

    // Class's
    private GPSTracker gpsTracker;
    private ShoppingJsonController shoppingJsonController;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about_screen);

        gpsTracker = new GPSTracker(AboutScreen.this);
        gpsTracker.permissaoGPS();

        // Utiliza o Botão Localizar
        btnLocalizar();
    }


    /**
    * Utiliza o Botão Localizar
    * @return AlertDialog
    * @return Informações do JSON SHOPPING
    * */
    private void btnLocalizar(){

        // Botão Localizar
        this.btnLocalizar = (Button) findViewById(R.id.btn_Localizar);
        this.btnLocalizar.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                shoppingJsonController = new ShoppingJsonController(AboutScreen.this);
                shoppingJsonController.consumirJsonArrayShopping();

                exibirGPSFragmentDialog();
            }
        });
    }


    /*
    * Método para utilizar o AlertDialog Fragment
    * @return Infla o AlertDialog Fragment
    * */
    public void exibirGPSFragmentDialog(){
        GPSFragmentActivity gpsFragmentDialog = new GPSFragmentActivity();
        gpsFragmentDialog.show(getSupportFragmentManager(), "gpsFragmentDialog");
    }


}

GPSFragmentActivity [DIALOGFRAGMENT] :

package com.vuforia.samples.Books.ui.ActivityFragmentList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.vuforia.samples.Books.R;
import com.vuforia.samples.Books.app.Neoris.GPSTracker;
import com.vuforia.samples.Books.app.Neoris.Shopping;
import com.vuforia.samples.Books.app.Neoris.ShoppingJsonController;
import com.vuforia.samples.Books.app.Neoris.VolleyAppController;
import com.vuforia.samples.Books.ui.ActivityList.AboutScreen;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by th on 05/07/17.
 */

public class GPSFragmentActivity extends DialogFragment {

    // Class's
    private Shopping shop;
    private ShoppingJsonController shoppingJsonController;


    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        shop = new Shopping(GPSFragmentActivity.this);
        shoppingJsonController = new ShoppingJsonController(GPSFragmentActivity.this);

        // Cria o objeto para configurar o AlertDialog
        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        // Define o Layout do AlertDialog
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.gps_fragment, null);
        builder.setView(dialogView);

        // Define o Layout Personalizado do AlertDialog
        builder.setTitle("GEOLOCALIZAÇÃO");
        builder.setMessage("" + shop.getShoppingNome());

        builder.setNeutralButton("RE-LOCALIZAR", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                // Pega as informações na Classe ShoppingJSONController
                // e Insere no DialogFragment
                shoppingJsonController.consumirJsonArrayShopping();
            }
        });

        builder.setPositiveButton("CONFIRMAR", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        // Cria o Objeto AlertDialog
        return builder.create();
    }


}

ShoppingJsonController.java

package com.vuforia.samples.Books.app.Neoris;

import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.vuforia.samples.Books.ui.ActivityFragmentList.GPSFragmentActivity;
import com.vuforia.samples.Books.ui.ActivityList.AboutScreen;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by th on 07/07/17.
 */

public class ShoppingJsonController extends Activity {


    private static final String URL_JSON_ARRAY = "http://jsonvu.esy.es/shoppings.json";

    private JSONObject shoppingObj;
    private JSONObject shoppingInfoObj;

    private Double shoppingJSON_LAT_1;
    private Double shoppingJSON_LAT_2;
    private Double shoppingJSON_LONG_1;
    private Double shoppingJSON_LONG_2;

    private String shoppingJSON_NOME;
    private String shoppingJSON_LICENSE_KEY;
    private String shoppingJSON_ACCESS_KEY;
    private String shoppingJSON_SECRET_KEY;

    private static String TAG = ShoppingJsonController.class.getSimpleName();

    private ProgressDialog pDialog;

    // Class's
    private GPSTracker gpsTracker;
    private Shopping shop;

    public ShoppingJsonController(AboutScreen aboutScreen) {
    }

    public ShoppingJsonController(GPSFragmentActivity gpsFragmentActivity) {
    }

    /**
     * MÉTODOS PARA O JSON DO SHOPPING
     * @return Informações do JSON consumido
     * @set Informações do JSON consumido a Shopping Class
     * **/
    public void consumirJsonArrayShopping(){

        // Referência as Classes GPSTracker & Shopping
        gpsTracker = new GPSTracker(ShoppingJsonController.this);
        shop = new Shopping(ShoppingJsonController.this);

        JsonArrayRequest req = new JsonArrayRequest(this.URL_JSON_ARRAY, new Response.Listener<JSONArray>() {

            @Override
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());

                try {

                    // Analisando a Resposta do JSON ARRAY
                    // Loop através de cada OBJETO JSON
                    for (int i = 0; i < response.length(); i++) {

                        // Pega o OBJETO JSON consumido
                        // Manipula as informações do ARRAY JSON através do OBJETO JSON a ser consumido
                        shoppingObj = (JSONObject) response.get(i);
                        shoppingInfoObj = shoppingObj.getJSONObject("informacoes");

                        shoppingJSON_LAT_1 = shoppingObj.getDouble("LAT_1");
                        shoppingJSON_LAT_2 = shoppingObj.getDouble("LAT_2");
                        shoppingJSON_LONG_1 = shoppingObj.getDouble("LONG_1");
                        shoppingJSON_LONG_2 = shoppingObj.getDouble("LONG_2");

                        shoppingJSON_NOME = shoppingInfoObj.getString("nome");
                        shoppingJSON_LICENSE_KEY = shoppingInfoObj.getString("licenseKey");
                        shoppingJSON_ACCESS_KEY = shoppingInfoObj.getString("accessKey");
                        shoppingJSON_SECRET_KEY = shoppingInfoObj.getString("secretKey");

                        // Se checkPerimetroUsuario == True, INSERE as informações do Shopping
                        // em que o usuário está dentro do Perímetro
                        if((gpsTracker.getLatitude() == shoppingJSON_LAT_1 && gpsTracker.getLatitude() == shoppingJSON_LAT_1)
                                && (gpsTracker.getLongitude() == shoppingJSON_LONG_1 && gpsTracker.getLongitude() == shoppingJSON_LONG_2)){

                            shop.setShoppingNome(shoppingJSON_NOME);
                            shop.setShoppingLicenseKey(shoppingJSON_LICENSE_KEY);
                            shop.setShoppingAccessKey(shoppingJSON_ACCESS_KEY);
                            shop.setShoppingSecretKey(shoppingJSON_SECRET_KEY);

                        } else {
                            shop.setShoppingNome("NÃO ENCONTRADO - FORA DO PERÍMETRO");
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                    // Caso ocorra algum erro
                    // Mostrará um TOAST informando o erro
                    Toast.makeText(getApplicationContext(),
                            "ERRO: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

                // Caso ocorra algum erro na Resposta do VOLLEY.JSON
                // Mostrará um TOAST informando o erro
                VolleyLog.d(TAG, "ERRO: " + error.getMessage());
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                //hidepDialog();
            }
        });

        // Adicionando solicitação para a fila do Consumo do JSON
        // Através da VolleyAppController Class
        VolleyAppController.getInstance().addToRequestQueue(req);
    }

}
  • IMAGES:
  

    

  

    
asked by anonymous 07.07.2017 / 16:55

0 answers