How to search STRING inside JSON on Android

Asked

Viewed 38 times

0

I have the following JSON

Create_request.txt

 {"dados":[{
    "Cod_Produto":"25",
    "Produto":"ARROZ",
    "Quantidade":"200KG",
    "Estoque":"300KG",
    "Valor":"1200",
    "Num_pedido":"41045"}
    ]}

I created a button called "Search request" to receive the number as parameter and check whether or not "Num_request" exists in the JSON,as for example type "41045" in TextView and by clicking the button perform the search ,but I’m not getting any return

activity_cadastre

 <EditText
        android:id="@+id/editText7"
        android:layout_width="178dp"
        android:layout_height="37dp"
        android:layout_marginStart="116dp"
        android:layout_marginTop="320dp"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="156dp"
        android:layout_marginTop="420dp"
        android:text="BUSCAR PEDIDO"
        android:onClick="load"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="BUSCAR PEDIDO" />

Class Register

public class cadastro<textView> extends AppCompatActivity {
    private static final String FILE_NAME = "Cria_Pedido.txt";

    TextView mEditText;

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


    }


    public void criar(View view) {
        Intent intent = new Intent(this, criar_pedido.class);
        startActivity(intent);
    }
    public void adicionar(View view) {
        Intent intent = new Intent(this, addproduto.class);
        startActivity(intent);
    }
    public void load(View view) {
        // mEditText = findViewById(R.id.text);

         FileInputStream fis = null;
        //mEditText = findViewById(R.id.text);
     try {

        fis = openFileInput(FILE_NAME);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuilder sb = new StringBuilder();
            String text ="Num_pedido";

            while ((text = br.readLine()) != null) {
                sb.append(text).append("\n");
            }

            mEditText.setText(sb.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }}
  • Try using Gson (google library) to convert your JSON into a list of objects of the requested type (you need to create the Request class first) and then check if the list has an object whose num_request property corresponds to the search.

1 answer

1


Change that part of your code. Remove the text and do so:

while ((text = br.readLine()) != null) {
    sb.append(text);
}
try {
    JSONObject jsonObject = new JSONObject(sb.toString());
    mEditText.setText(jsonObject.getString("Num_pedido"));
} catch (JSONException e) {
    e.printStackTrace();
}

You could play this catch for next to the already existing Ioexception catch as well.

As already commented, it would be interesting for you to take a look at GSON, it could help you in this beginning. There is a nice guide on this link: https://sites.google.com/site/aulasvictormenegusso/programacao-web-2-1-semestre-2017/trabalhando-com-json-com-a-biblioteca-gson

Browser other questions tagged

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