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.
– Lennoard Silva