0

I have an activity containing one edit-text, button and Textview. Now I want if someone enters Java in edit-text then its info to be filled in Text View. Here is my JSON. I have also included my .java file along with it.

{
  "compscience": [
    {
      "bookname": "java",
      "row": "1",
      "column": "1"
    },
    {
      "bookname": "cns",
      "row": "2",
      "column": "2"
    },
    {
      "bookname": "rdbms",
      "row": "3",
      "column": "3"
    },
    {
      "bookname": "daa",
      "row": "4",
      "column": "4"
    }
  ]
}

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

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

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class act1 extends AppCompatActivity {
    EditText subname;
    Button fetch;
    static String ab,single="";
    public static   String c;
   static TextView data;
fetchdata process;
    StringBuffer response ;

void  init()
{
    subname = findViewById(R.id.subname);
    ab = subname.toString();
    fetch = findViewById(R.id.button);
    data=findViewById(R.id.result);      
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_act1);
    init();
}
public  void fetch(View view)
{
   process = new fetchdata();
    process.execute();//it will start do in background
}

 class fetchdata extends AsyncTask {

    @Override
    protected Object doInBackground(Object[] objects) {
        response = new StringBuffer();
        try {
            URL url = new URL("https://api.myjson.com/bins/c1054");

            URLConnection connection =url.openConnection();

            InputStream inputStream = connection.getInputStream();
            InputStreamReader reader = new InputStreamReader(inputStream);
            BufferedReader buffer = new BufferedReader(reader);
            String line = "";

            while((line = buffer.readLine()) != null) {
                response.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Object o) {
        super.onPostExecute(o);

        try {
            JSONArray array=new JSONArray("respnse");

            for(int i=0;i<array.length();i++)
            {
                JSONObject obj=array.getJSONObject(i);

                if(ab.equals(obj.getString("booknmae")))
                {
                    single = single +obj.getString("row");
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        act1.data.setText(single);
    }
}
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
anurag
  • 11
  • 4
  • 3
    Welcome to Stack Overflow! Please review [writing the perfect question](https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question) to help you to ask a good question, and thus get a good answer. – Jeroen Heier Aug 26 '18 at 06:03
  • 1
    Possible duplicate of [How do I parse JSON in Android?](https://stackoverflow.com/questions/9605913/how-do-i-parse-json-in-android) – OneCricketeer Aug 26 '18 at 09:01

1 Answers1

1

Fist of all, you need to understand how JSONObject and JSONArray work, your json response is JSONObject and inside it there is key with name compscience have JSONArray as value.

try this:

try {
        JSONObject respJson = new JSONObject(response.toString());
        JSONArray array = respJson.getJSONArray("compscience");
        for(int i=0;i<array.length();i++)
        {
            JSONObject obj=array.getJSONObject(i);

            if(ab.equals(obj.getString("booknmae")))
            {

                single = single +obj.getString("row");

            }
        }


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Mohamd Ali
  • 2,146
  • 4
  • 23
  • 30