Return a method value (JSON Array)

Asked

Viewed 275 times

0

Hello, I have a method that fetches the URL through JSON and wants to return this method value to mainActivity, to show.

package com.example.fabio.domoticaa;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

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

public class Divi_Dispo extends AppCompatActivity {
String x;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_divi__dispo);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    final String[] count = new String[1];

    final String[] id = new String[1];

    Intent intent = getIntent();
    String value = intent.getStringExtra("divisao");
    final EditText nomediv = (EditText) findViewById(R.id.editText4);
    Count(value);

    nomediv.setText(x);//want set th result of Count(value)


}
public void Count(String value) {
    final String[] count = new String[1];
    // Send data
    try {

        RequestQueue queue = Volley.newRequestQueue(Divi_Dispo.this);

        String url = "http://brunos.000webhostapp.com/teste/obter_id.php?descricao=" + value ;

        JsonArrayRequest jsonRequest = new JsonArrayRequest
                (Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        try {
                            JSONObject jObj = new JSONObject(String.valueOf(response.get(0)));
                            count[0] = jObj.getString("COUNT(id)");//quero retornar este valor

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                    }
                });
        queue.add(jsonRequest);
    } catch (Exception ex) {
    } finally {
    }
}

public interface VolleyCallback {
    void onSuccess(String result);
}

}

  • You want what!? There’s a way you can put your doubt here?!

  • I have the Count(Wtring value) method and wanted it to return the Count[0] value and receive in Oncreat

No answers

Browser other questions tagged

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