Search code examples
androidparsinghttpconnection

Parsing Data from url using HttpURLConnection android


I am working on Url parsing where I am getting a response by using HttpURLConnection as {"USD_PKR":115.599998}. From my response I want to get value as 115.599998 by parsing above extracted data. My code is given below, kindly guide me to get my extracted String value.

private class MakeNetworkCall extends AsyncTask<String, Void, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
//            DisplayMessage("Please Wait ...");
        }

        @Override
        protected String doInBackground(String... arg) {

            InputStream is = null;
            String URL = arg[0];
            Log.d("", "URL: " + URL);
            String res = "";

            is = ByGetMethod(URL);

            if (is != null) {
                res = ConvertStreamToString(is);
            } else {
                res = "Something went wrong";
            }
            return res;
        }

        protected void onPostExecute(String result) {
            super.onPostExecute(result);

//            DisplayMessage(result);
            convert_result.setVisibility(View.VISIBLE);
            convert_result.setText(result);
// Here I am getting a response as {"USD_PKR":115.599998}, I want to extract the value as 115.599998

            Log.d("Result: ", "Result: " + result);
        }
    }


    String ConvertStreamToString(InputStream stream) {

        InputStreamReader isr = new InputStreamReader(stream);
        BufferedReader reader = new BufferedReader(isr);
        StringBuilder response = new StringBuilder();

        String line = null;
        try {

            while ((line = reader.readLine()) != null) {
                response.append(line);
            }

        } catch (IOException e) {
            Log.e("Error", "Error in ConvertStreamToString", e);
        } catch (Exception e) {
            Log.e("Error", "Error in ConvertStreamToString", e);
        } finally {

            try {
                stream.close();

            } catch (IOException e) {
                Log.e("Error", "Error in ConvertStreamToString", e);

            } catch (Exception e) {
                Log.e("Error", "Error in ConvertStreamToString", e);
            }
        }
        return response.toString();


    }

    InputStream ByGetMethod(String ServerURL) {

        InputStream DataInputStream = null;
        try {

            URL url = new URL(ServerURL);
            HttpURLConnection cc = (HttpURLConnection)
                    url.openConnection();
            //set timeout for reading InputStream
            cc.setReadTimeout(5000);
            // set timeout for connection
            cc.setConnectTimeout(5000);
            //set HTTP method to GET
            cc.setRequestMethod("GET");
            //set it to true as we are connecting for input
            cc.setDoInput(true);

            //reading HTTP response code
            int response = cc.getResponseCode();

            //if response code is 200 / OK then read Inputstream
            if (response == HttpURLConnection.HTTP_OK) {
                DataInputStream = cc.getInputStream();
            }

        } catch (Exception e) {
            Log.e("Error Get Data", "Error in GetData", e);
        }
        return DataInputStream;

    }

Solution

  • Your response is an usual JSON.

    String responseString = "{\"USD_PKR\":115.599998}";
    JSONObject json = new JSONObject(responseString);
    double usdPkr = json.getDouble("USD_PKR"); // 115.599998
    

    Your case:

    protected void onPostExecute(String result) {
        super.onPostExecute(result);
    
        // DisplayMessage(result);
        JSONObject json = new JSONObject(result);
        double usdPkr = json.getDouble("USD_PKR"); // 115.599998
        convert_result.setVisibility(View.VISIBLE);
        convert_result.setText("" + usdPkr);
    
        Log.d("Result: ", "Result: " + result);
    }