Problems when creating application to consume postal services

Asked

Viewed 87 times

1

I am developing an application that aims to consult prices and deadlines of orders consuming the Post Office service. However I am having difficulties to implement the service in the application. How do I transfer the data of the entries of the first Activity (where I take source and destination cep) of the second (Length, width, height, weight, diameter and recover in the third, to then carry out the service query using the method of the post office. 2

package com.example.diego.precoeprazocorreios;

import java.io.Serializable;

public class Encomenda implements Serializable{

private String cepOrigem;
private String cepDestino;
private int idIcon;
private String codigo;
private String PrazoEntrega;
private String peso;
private String maoPropria;
private String avisoRecibo;
private Double comprimento;
private Double altura;
private Double largura;
private Double valorDeclarado;

public Encomenda(String cepOrigem, String cepDestino, int idIcon, String codigo, String prazoEntrega, String peso, String maoPropria, String avisoRecibo, Double comprimento, Double altura, Double largura, Double valorDeclarado) {
    this.cepOrigem = cepOrigem;
    this.cepDestino = cepDestino;
    this.idIcon = idIcon;
    this.codigo = codigo;
    PrazoEntrega = prazoEntrega;
    this.peso = peso;
    this.maoPropria = maoPropria;
    this.avisoRecibo = avisoRecibo;
    this.comprimento = comprimento;
    this.altura = altura;
    this.largura = largura;
    this.valorDeclarado = valorDeclarado;
}

public String getCodigo() {
    return codigo;
}

public void setCodigo(String codigo) {
    this.codigo = codigo;
}

public String getPrazoEntrega() {
    return PrazoEntrega;
}

public int getIdIcon() {
    return idIcon;
}

public void setIdIcon(int idIcon) {
    this.idIcon = idIcon;
}

public void setPrazoEntrega(String prazoEntrega) {
    PrazoEntrega = prazoEntrega;
}

public String getPeso() {
    return peso;
}

public void setPeso(String peso) {
    this.peso = peso;
}

public String getMaoPropria() {
    return maoPropria;
}

public void setMaoPropria(String maoPropria) {
    this.maoPropria = maoPropria;
}

public String getAvisoRecibo() {
    return avisoRecibo;
}

public void setAvisoRecibo(String avisoRecibo) {
    this.avisoRecibo = avisoRecibo;
}

public Double getComprimento() {
    return comprimento;
}

public void setComprimento(Double comprimento) {
    this.comprimento = comprimento;
}

public Double getAltura() {
    return altura;
}

public void setAltura(Double altura) {
    this.altura = altura;
}

public Double getLargura() {
    return largura;
}

public void setLargura(Double largura) {
    this.largura = largura;
}

public Double getValorDeclarado() {
    return valorDeclarado;
}

public void setValorDeclarado(Double valorDeclarado) {
    this.valorDeclarado = valorDeclarado;
}

public String getCepOrigem() {
    return cepOrigem;
}

public void setCepOrigem(String cepOrigem) {
    this.cepOrigem = cepOrigem;
}

public String getCepDestino() {
    return cepDestino;
}

public void setCepDestino(String cepDestino) {
    this.cepDestino = cepDestino;
}


}

First Activity

package com.example.diego.precoeprazocorreios;

import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


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

    Button btnProximo1 = (Button) findViewById(R.id.proximo1);
    btnProximo1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            validarCampos();
        }
    });
}

public void validarCampos() {

    EditText cepOrigem = (EditText) findViewById(R.id.origem);
    EditText cepDestino = (EditText) findViewById(R.id.destino);

    String cepOri = cepOrigem.getText().toString();
    String cepDes = cepDestino.getText().toString();
    String padrao = "\\d{8}";

    if (!cepOri.matches(padrao)) {
        cepOrigem.requestFocus();
        Toast toast = Toast.makeText(getApplicationContext(), "CEP inválido", Toast.LENGTH_LONG);
        toast.show();
    } else if (!cepDes.matches(padrao)) {
        cepDestino.requestFocus();
        Toast toast = Toast.makeText(getApplicationContext(), "CEP inválido", Toast.LENGTH_LONG);
        toast.show();
    } else {
        Encomenda enc = new Encomenda(cepOrigem.getText().toString(), cepDestino.getText().toString());
        Intent myIntent = new Intent(MainActivity.this, SegundaActivity.class);
        myIntent.putExtras("objeto", enc);
        startActivity(myIntent);
    }
}
}
  • I’m not sure I understand this, but if you need, in the 3rd Activity, the values passed by the 1st Activity to the 2nd Activity, make the 2nd Activity pass them to her.

  • That’s what I’m trying to do right now... the way it’s there it doesn’t capture the entrances.

  • @ramaral now already picking up the values of the inputs, how do I pass this data to the next screen? remembering that I need to take only what the user chooses, example if he chooses the box/package encoding format, we will need to pass the width, height and length inputs. My doubt is just there, how to do it dynamically according to what the user chooses

  • Again I’m not sure what you want. If I understand correctly you walk there close. You should create only one Bundle with new Bundle() then add values to it according to the options chosen by the user. You should also add a value(flag) to indicate the type of values: for example 1 will indicate box/package. At the end add the Bundle to the Intent that will open the next Activity. In this activitie, depending on the flag, get the values passed.

  • I think he wants to pass the data from one Activity to another, as a Wizard, and in the end he will use all the data. I would do different than using Bundle(), passing a model to the activities and then consuming this model in the latter.

  • @ramaral I created only one budle as requested and I passed the flag and the value of each of the entries I want to pass to the third screen, but it arrives null the third screen. Summarizing what I want now, is to take all the input values of the first and second screen and pass them to the third, that is where I will make the requisition of the service of the post office.

  • Voce has some practical example of this way @Grupocdsinformática?

  • @Carlosdiego has a simple answer on the Soen, here

  • @Grupocdsinformatics is a possible approach and there will certainly be others. I just didn’t want to run away from what the AP was doing so I wouldn’t add to the confusion.

  • @ramaral yes yes, it is that I find more practical to pass the model, and it takes away the need to read a lot of the Bundle. But the mass of the OS is the possibility of seeing countless solutions to a problem, that’s what shorter ;)

  • @Grupocdsinformática is exactly what I want to do. I will try to apply the method in my application.

  • @Grupocdsinformática I am with difficulty to implement the method, already created the class and implemented serializable, in my first Activity I instated the object Order, however it complains of the constructor, because they are several parameters, and on the first screen I only catch two that are the origin and destination cep.

  • Creates a default constructor, and sets the information using the class gets and sets. Remembering that you will only instantiate her in the first Activity, and send her from one Activity to another.

  • Encomenda enc = new Encomenda(cepOrigem.getText().toString(), cepDestino.getText().toString());
 Intent myIntent = new Intent(MainActivity.this, SegundaActivity.class);
 myIntent.putExtras("objeto", enc);
 startActivity(myIntent); Thus?

  • @Grupocdsinformática in the example you indicated the guy uses on the same screen, only two parameters, as would be applied in my case, I’m unable to apply. I rephrased the question

Show 10 more comments

1 answer

1


Add your class a builder like this:

public Encomenda() {
}

After you fill in the fields and call the other Activity, fill in the fields using get and set. Put this snippet on Else:

else {
        Encomenda enc = new Encomenda();
        enc.setCepOrigem(cepOrigem.getText().toString());
        enc.setCepDestino(cepDestino.getText().toString());
        Intent myIntent = new Intent(MainActivity.this, SegundaActivity.class);
        myIntent.putExtras("objeto", enc);
        startActivity(myIntent);
    }

Ai, in the other Activity, you fill in the other fields, before calling Activity read this variable of the Bundle, fill in the new fields using the get and set and then move on to the next, as from the first to the second. Example:

Encomenda enc = getIntent().getSerializableExtra("objeto");
enc.setComprimento(); //Ai le do EditText como na primeira Activity.

//Aqui vc vai chamar a activity 3
Intent myIntent = new Intent(MainActivity.this, TerceiraActivity.class);
myIntent.putExtras("objeto", enc);
startActivity(myIntent);

At Terceiraactivity, you will read the same way as on Monday, fill in and pass to the call from the webservice.

  • ready, I was just what I did. Now I have another problem, I can not capture the input values. I will update the question for you to see

  • Instead of updating, create another question with her code correct, because the problem there is another, and will mess keep updating.

  • https://answall.com/questions/253913/conversao-string-para-double-retornando-null

Browser other questions tagged

You are not signed in. Login or sign up in order to post.