Good evening. I'm having a hard time using the value of a variable in a local class and passing it to another class that will be loaded in the intent. The string value is leaving null and gives error in the application. I'm a beginner, I've researched a lot and still can not figure out a way to pass this value. I would like you to help me. Thanks in advance.
public class Carro extends AppCompatActivity {
ListView lvCarrinhoItems;
Button bFinalizar;
String url2 = "";
String parametros2 = "";
String insertUrl = "http://localhost/registrar.php";
RequestQueue requestQueue;
String result = "";
public static String idOrder = "", dta = "";
public static MeuPedido meuPedido;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shopping_carrinho);
lvCarrinhoItems = (ListView) findViewById(R.id.tela5lv);
LayoutInflater layoutInflater = getLayoutInflater();
final Carrinhos carrinhos = CarrinhoHelper.getCarrinhos();
final TextView tvTotalPrice = (TextView) findViewById(R.id.tvsoma);
final TextView tvObspedido = (TextView) findViewById(R.id.etobscar);
tvTotalPrice.setText((Bandeja.MOEDA+String.valueOf(carrinhos.getTotalPrice().setScale(2, BigDecimal.ROUND_HALF_UP))).replace(".",","));
final TCartItemAdapter tcartItemAdapter = new TCartItemAdapter(this);
tcartItemAdapter.updateCartItems(getCarrinhoItems(carrinhos));
lvCarrinhoItems.setAdapter(tcartItemAdapter);
bFinalizar = (Button) findViewById(R.id.button4);
bFinalizar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder alerta = new AlertDialog.Builder(Carro.this);
alerta.setTitle("Confirmação de pedido");
alerta
.setIcon(R.drawable.service)
.setMessage("Deseja confirmar o seu pedido?")
.setCancelable(false)
.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestQueue = Volley.newRequestQueue(getApplicationContext());
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
pref = getSharedPreferences("login.conf", Context.MODE_PRIVATE);
final String idcliente = pref.getString("idusuario", null);
final String idrestaurante = CarrinhoHelper.getIdResta();
final String valorpedido = String.valueOf(carrinhos.getTotalPrice());
final String obspedido = tvObspedido.getText().toString();
if (idcliente == null){
Intent intent = new Intent(Carro.this, A2_1semlogin.class);
startActivity(intent);
}else
if (networkInfo != null && networkInfo.isConnected()) {
url2 = "http://localhost/registrarpedido.php";
parametros2 = "idcliente=" + idcliente + "&idrestaurante=" + idrestaurante + "&valorpedido=" + valorpedido + "&obspedido=" + obspedido;
new SolicitaDados2().execute(url2);
} else {
Toast.makeText(getApplicationContext(), "Por favor, verifique sua conexão", Toast.LENGTH_LONG).show();
}
Log.d("DadosRet2", parametros2);
Log.d("DadosRet2", idcliente + ", " + idrestaurante + ", " + valorpedido + ", " + obspedido);
class MyAsyncTaskresources extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
InputStream isr = null;
try{
String URL=params[0];
java.net.URL url = new URL( URL);
URLConnection urlConnection = url.openConnection();
isr = new BufferedInputStream(urlConnection.getInputStream());
}
catch(Exception e){
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
String s = "";
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
final JSONObject json = jArray.getJSONObject(i);
s = s + "login info : " + json.getString("idOrder") + " " + json.getString("idcliente") + " " + json.getString("idrestaurante") + " " + json.getString("data");
**STRING QUE DESEJO ACESSAR = idOrder**
idOrder = json.getString("idOrder");
dta = json.getString("data");
List<CarrinhoItem> carrinhoItems = new ArrayList<CarrinhoItem>();
Log.d(TAG, "Current shopping cart: " + carrinhos);
Map<TSaleable, Integer> itemMap = carrinhos.getItemWithQuantity();
for (Map.Entry<TSaleable, Integer> entry : itemMap.entrySet()) {
final CarrinhoItem carrinhoItem = new CarrinhoItem();
carrinhoItem.setBandeja((Bandeja) entry.getKey());
carrinhoItem.setQuantidade(entry.getValue());
carrinhoItems.add(carrinhoItem);
final String idcomida = carrinhoItem.getBandeja().getIdcomida();
final String quantidade = carrinhoItem.getBandeja().getQuantidade();
final BigDecimal valorunit = carrinhoItem.getBandeja().getPreco();
final String adicional = carrinhoItem.getBandeja().getAdicional();
final String obsproduto = carrinhoItem.getBandeja().getObsproduto();
StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
System.out.println(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("idOrder",idOrder);
parameters.put("idcomida", idcomida);
parameters.put("quantidade", quantidade);
parameters.put("valorunit", valorunit.toString());
parameters.put("adicional", adicional);
parameters.put("obsproduto", obsproduto);
final MeuPedido meupedido = new MeuPedido();
String novaId = idOrder;
meupedido.setmPedido(novaId);
return parameters;
}
};
requestQueue.add(request);
}
break;
}
}
catch(Exception e){
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
}
new MyAsyncTaskresources().execute("http://localhost/sel.php?idcliente="+idcliente+"&idrestaurante=" +idrestaurante);
}
});
AlertDialog alertaDialog = alerta.create();
alertaDialog.show();
}
});
}
public static final List<CarrinhoItem> getCarrinhoItems(Carrinhos carrinhos) {
List<CarrinhoItem> carrinhoItems = new ArrayList<CarrinhoItem>();
Log.d(TAG, "Current shopping cart: " + carrinhos);
Map<TSaleable, Integer> itemMap = carrinhos.getItemWithQuantity();
for (Map.Entry<TSaleable, Integer> entry : itemMap.entrySet()) {
final CarrinhoItem carrinhoItem = new CarrinhoItem();
carrinhoItem.setBandeja((Bandeja) entry.getKey());
carrinhoItem.setQuantidade(entry.getValue());
carrinhoItems.add(carrinhoItem);
final String comida = carrinhoItem.getBandeja().getComida();
final String adicional = carrinhoItem.getBandeja().getAdicional();
final String quantidade = carrinhoItem.getBandeja().getQuantidade();
final BigDecimal valor = carrinhoItem.getBandeja().getPreco();
}
return carrinhoItems;
}
private class SolicitaDados2 extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return Conexao.postDados(urls[0], parametros2);
}
@Override
protected void onPostExecute(String resultado) {
if (resultado.contains("pedido_ok")) {
Toast.makeText(getApplicationContext(), "Registro efetuado com sucesso", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Carro.this, Anotafiscal.class);
** LOCAL ONDE DESEJO INSERIR A STRING**
String hjhj = idOrder ;
intent.putExtra("iddopedido",hjhj);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "Ocorreu um erro no cadastro", Toast.LENGTH_LONG).show();
}
}
}
}