Variable returns me null

0

I have a problem, I need to click on save to send the url of the image that I uploaded to cloudinary, the image if it is uploaded but I do not know why it returns to me, the code itself does not return error, in this log Log.d ("Path in save button", String.valueOf (route)); I realize that it sends null any help would be great thank you very much

public class edit_profile_activity extends AppCompatActivity 
{
    private static int RESULT_LOAD_IMG = 1;
    String imgDecodableString;
    Button btngudar;
    Map uploadResult;
String ruta;
    Uri selectedImage;
    Map config = new HashMap();
    ImageView imgView;

    Button bu=null, guardar;


    EditText editName, editEmail, editLastName, editNick, showDate;
    TextView txtNameTop, txtNickTop;
    RadioButton rbMale,rbFemale;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);

        //bu = (Button) findViewById(R.id.btnLogout);
        guardar = (Button) findViewById(R.id.btnguardar);
        rbMale = (RadioButton) findViewById(R.id.rbMale);
        rbFemale = (RadioButton) findViewById(R.id.rbFemale);

        config.put("cloud_name", "*********");
        config.put("api_key", "1234567890abcde");
        config.put("api_secret","123456789012345678901234567");

        new PlifRequestBase(edit_profile_activity.this) {

            @Override
            public JsonObject onHttpOk(JsonObject response) throws JSONException {
                JsonObject data;
                data = response;

                //En esta parte se declaran los botones, textos y campos de texto con las respectivas variables
                rbMale = (RadioButton) findViewById(R.id.rbMale);
                rbFemale = (RadioButton) findViewById(R.id.rbFemale);

                editLastName = (EditText) findViewById(R.id.editapellido);
                editEmail = (EditText) findViewById(R.id.editcorreo);
                editName = (EditText) findViewById(R.id.editnombre);
                editNick = (EditText) findViewById(R.id.editnick);
                showDate = (EditText) findViewById(R.id.showDate);


                txtNameTop = (TextView) findViewById(R.id.txtNombreA);
                txtNickTop = (TextView) findViewById(R.id.nickname);

                //se toman los valores desde el JsonObject
                final String name = data.get("first_name").getAsString();
                final String lastName = data.get("last_name").getAsString();
                final String emailUser = data.get("email").getAsString();
                //final String nick= data.get("nick").getAsString();
                //final String nick= data.get("nick").toString();
                //final String nick= String.valueOf(data.get("nick").getAsString().equals(""));
                final String nick = data.get("nick") != JsonNull.INSTANCE ? data.get("nick").getAsString() : null;
                final int sex = data.get("sex").getAsInt();

                //se ponen en los respectivos textos y edit text lo consultado
                edit_profile_activity.this.runOnUiThread(new Runnable() {
                    public void run() {
                        txtNameTop.setText(name + " " + lastName);
                        //txtNickTop.setText(nick);
                        editName.setText(name);
                        editEmail.setText(emailUser);
                        editLastName.setText(lastName);
                        if (nick == null)
                        {
                            editNick.setText("");

                        }
                        if(nick != "false") {
                            editNick.setText(nick);
                        }
                        //    showDate.setText(birthDate);
                    }
                });

                Log.d("Nombre", name + " " + lastName);
                Log.d("Correo", emailUser);


                return data;
            }

            @Override
            public void onHttpCreate(JsonObject response) throws JSONException {

            }

            @Override
            public void onHttpUnprocessableEntity(JsonObject response) throws JSONException {
                this.cancel(true);
                final String error = response.get("errors").toString();
                edit_profile_activity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(edit_profile_activity.this.getApplicationContext(), error, Toast.LENGTH_LONG).show();
                    }
                });
            }
        }.execute("user/profile", "GET");

        //------------------------------------------------------------------------------------------------------------------

        //se hacen los cambios en la api a partir de la accion del boton guardar
        guardar.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Thread thread = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        try
                        {
                            Cloudinary cloudinary = new Cloudinary(config);
                            uploadResult =  cloudinary.uploader().upload(imgDecodableString, ObjectUtils.emptyMap());
                            ruta = String.valueOf(uploadResult.get("url"));
                            Log.d("Ruta",String.valueOf(ruta));
                            Log.d("RESULTADOS",String.valueOf(uploadResult));
                            Log.d("URL",String.valueOf(uploadResult.get("url")));

                        } catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                });
                thread.start();

                Log.d("Ruta en boton guardar",String.valueOf(ruta));

                String setName = editName.getText().toString();
                String setEmail= editEmail.getText().toString();
                String setLastName= editLastName.getText().toString();
                String setNick = editNick.getText().toString();
                String setBirthDate= showDate.getText().toString();

                Toast.makeText(edit_profile_activity.this, "Se ha guardado correctamente", Toast.LENGTH_SHORT).show();

                if (setNick == null)
                {
                    editNick.setText("nickname");

                }
                if(setNick != "")
                {
                    editNick.setText("");
                }

                int sex = 0;

                if (rbMale.isChecked())
                {
                    sex=1;
                }
                else if (rbFemale.isChecked())
                {
                    sex=2;
                }else
                {
                    sex=3;
                }

                new PlifRequestBase(edit_profile_activity.this){

                    @Override
                    public JsonObject onHttpOk(JsonObject response) throws JSONException {
                        JsonObject data;
                        data = response;
                        if (response.equals(200)){
                            Toast.makeText(edit_profile_activity.this, "Se ha guardado correctamente", Toast.LENGTH_SHORT).show();
                        }
                        return data;
                    }

                    @Override
                    public void onHttpCreate(JsonObject response) throws JSONException {


                        try {
                            Toast.makeText(edit_profile_activity.this, "Se ha guardado correctamente", Toast.LENGTH_SHORT).show();
                        } catch (IllegalStateException e) {

                        }
                    }

                    @Override
                    public void onHttpUnprocessableEntity(JsonObject response) throws JSONException {
                        this.cancel(true);
                        final String error = response.get("errors").toString();
                        edit_profile_activity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(edit_profile_activity.this.getApplicationContext(), error, Toast.LENGTH_LONG).show();
                            }

                        });

                    }

                }.execute("user/profile","PUT","user",
                        "email", setEmail,
                        "first_name", setName,
                        "last_name", setLastName,
                        "nick", setNick,
                        "birthdate", setBirthDate,
                        "sex", String.valueOf(sex),
                        "current_avatar", String.valueOf(ruta)
                );

                Log.d("Envía:", setBirthDate);
                Log.d("Sexo", String.valueOf(sex));

                    new PlifRequestBase(edit_profile_activity.this) {

                    @Override
                    public JsonObject onHttpOk(JsonObject response) throws JSONException {
                        JsonObject data;
                        data = response;

                        //En esta parte se declaran los botones, textos y campos de texto con las respectivas variables
                        rbMale = (RadioButton) findViewById(R.id.rbMale);
                        rbFemale = (RadioButton) findViewById(R.id.rbFemale);

                        editLastName = (EditText) findViewById(R.id.editapellido);
                        editEmail = (EditText) findViewById(R.id.editcorreo);
                        editName = (EditText) findViewById(R.id.editnombre);
                        editNick = (EditText) findViewById(R.id.editnick);

                        txtNameTop = (TextView) findViewById(R.id.txtNombreA);
                        txtNickTop = (TextView) findViewById(R.id.nickname);

                        //se toman los valores desde el JsonObject
                        final String name = data.get("first_name").getAsString();
                        final String last_name = data.get("last_name").getAsString();
                        final String email_user = data.get("email").getAsString();
                        //final String nick= data.get("nick").toString();
                        //final String nick= String.valueOf(data.get("nick").getAsString().equals(""));
                        final String nick = data.get("nick") != JsonNull.INSTANCE ? data.get("nick").getAsString() : null;
                        final int sex = data.get("sex").getAsInt();

                        //se ponen en los respectivos textos y edit text lo consultado
                        edit_profile_activity.this.runOnUiThread(new Runnable() {
                            public void run() {
                                txtNameTop.setText(name + " " + last_name);
                                //txtNickTop.setText(nick);
                                editName.setText(name);
                                editEmail.setText(email_user);
                                editLastName.setText(last_name);
                                if (nick == null)
                                {
                                    editNick.setText("");

                                }
                                if(nick != "false") {
                                    editNick.setText(nick);
                                }
                            }
                        });

                        Log.d("Nombre", name + " " + last_name);
                        Log.d("Correo", email_user);


                        return data;
                    }

                    @Override
                    public void onHttpCreate(JsonObject response) throws JSONException {

                    }

                    @Override
                    public void onHttpUnprocessableEntity(JsonObject response) throws JSONException {
                        this.cancel(true);
                        final String error = response.get("errors").toString();
                        edit_profile_activity.this.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(edit_profile_activity.this.getApplicationContext(), error, Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                }.execute("user/profile", "GET");


            }
        });

    }


    public void logout(View view){
        SharedPreferences sharedpreferences = getSharedPreferences("PlifMx", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedpreferences.edit();
        editor.clear();
        editor.commit();
        Intent intent = new Intent(edit_profile_activity.this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }


    public void loadImagefromGallery(View view) {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }
    public void subirImg ()
    {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();

                final ImageView imgView = (ImageView) findViewById(R.id.circleImageView);
                // Set the Image in ImageView after decoding the String
                imgView.setImageBitmap(BitmapFactory.decodeFile(imgDecodableString));

            } else {
                Toast.makeText(this, "No seleccionaste una imagen",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {
            Toast.makeText(this, "Ocurrio algo", Toast.LENGTH_LONG)
                    .show();
        }
    }
}
    
asked by Heber Solis 18.03.2017 в 19:12
source

2 answers

0

Check in debug if you have a NetworkOnMainThreadException . I have not used Cloudinary, but I guess that uploader().upload(...) takes access to the internet.

If so, you'll have to put this code in a AsyncTask to work . The reason is that Android since 3.0 does not allow direct access to the network in the main thread.

    
answered by 18.03.2017 в 19:36
0

Remember that you are using a Thread to obtain the value of ruta , if you execute a Thread and immediately start it you want to obtain this value, it may be that you have not finished the request and obtaining the value, it is advisable to do it within the definition of the Thread.

Thread thread = new Thread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        try
                        {
                            Cloudinary cloudinary = new Cloudinary(config);
                            uploadResult =  cloudinary.uploader().upload(imgDecodableString, ObjectUtils.emptyMap());
                            ruta = String.valueOf(uploadResult.get("url"));
                            Log.d("Ruta",String.valueOf(ruta));
                            Log.d("RESULTADOS",String.valueOf(uploadResult));
                            Log.d("URL",String.valueOf(uploadResult.get("url")));


//*** obten aquí en valor.
Log.d("Ruta en boton guardar",String.valueOf(ruta));

                        } catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }
                });
                thread.start();

               // Log.d("Ruta en boton guardar",String.valueOf(ruta));
    
answered by 18.03.2017 в 23:00