Anindya Roy
Anindya Roy

Reputation: 7

App not able to extract json data

I am trying to extract Json data, but somehow the code is not working. Can somebody please help me ? Can't seem to understand what i am doing wrong.

(I dont really hav any more details to add)

public class MainActivity extends AppCompatActivity{
    TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t=(TextView)findViewById(R.id.exchangeRate);
        Anindya task = new Anindya();
        task.execute("xyz");
    }
    public class Anindya extends AsyncTask<String,Void,String>
    {
        @Override
        protected String doInBackground(String... urls) {
            String result = "";
            URL url;
            HttpURLConnection urlConnection=null;

            try {
                url = new URL(urls[0]);
                urlConnection = (HttpURLConnection)url.openConnection();
                InputStream in = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(System.in);
                int data = reader.read();

                while (data!= 0)
                {
                    char current = (char)data;
                    result += current;
                    data = reader.read();
                }
                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.i("website content",result);
        }
    }
}

Upvotes: 0

Views: 39

Answers (1)

Swr7der
Swr7der

Reputation: 859

Try replacing InputStreamReader reader = new InputStreamReader(System.in) with InputStreamReader reader = new InputStreamReader(in)

If it still doesn't solve your problem then try to check the value of data and post details about returned value.

Upvotes: 1

Related Questions