使用 HttpURLConnection android 从 url 解析数据



我正在研究 Url 解析,我通过使用 HttpURLConnection 作为{"USD_PKR":115.599998}来获得响应。从我的回答中,我想通过解析上面提取的数据来获得115.599998值。我的代码如下,请指导我获取提取的String值。

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;
    }

您的响应是通常的 JSON。

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

您的案例:

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);
}

相关内容

  • 没有找到相关文章

最新更新