如何从android中的url获取json对象

时间:2016-07-14 07:09:05

标签: android json

我想从json对象中获取json数据。 我的json数据是:{"avg":2.5},我的android代码是

public class AsyncTaskParseJson extends AsyncTask < String, String, String > {

        final String TAG = "AsyncTaskParseJson.java";

        // set your json string url here
        String yourJsonStringUrl = "http://www.bsservicess.com/photoUpload/star_avg.php?bookName=" + book_name;

        // contacts JSONArray

        @Override
        protected void onPreExecute() {}

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

            try {
                JSONParser jParser = new JSONParser();

                // get json string from url
                JSONObject json = jParser.getJSONFromUrl(yourJsonStringUrl);

                // get the array of users
                JSONObject dataJsonArr = json.getJSONObject(str);

                String c = dataJsonArr.getString("avg");
                na = c;
                starts = Float.parseFloat(c);


            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String strFromDoInBg) {
            super.onPostExecute(strFromDoInBg);

            netRate.setRating(starts);
            Toast.makeText(mybookview.this, Float.toString(starts), Toast.LENGTH_SHORT).show();
        }

但不知怎的,它不起作用。我已经完成了每个教程和evrything但没有任何作用.plz帮助我

3 个答案:

答案 0 :(得分:0)

这是来自url示例的简单获取数据:

Parse JSON from HttpURLConnection object

如果你想使用库,那么试试Volley: 教程链接:

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

答案 1 :(得分:0)

你得到的json数据是{&#34; avg&#34;:2.5} 简单删除下面的代码

 JSONObject dataJsonArr = json.getJSONObject(str);

 String c = dataJsonArr.getString("avg");

以下行

String c = json.getString("avg");

答案 2 :(得分:0)

解决问题的一个非常简单的方法

String str = "{ \"avg\" :0 }";
JsonParser parser = new JsonParser();
JsonObject object = (JsonObject) parser.parse(str);
String value = object.get("avg").getAsString();

但首先你必须纠正你后端的警告。

编辑完整的解决方案

public class AsyncTaskParseJson extends AsyncTask < String, String, String > {

    HttpURLConnection urlConnection;

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

        StringBuilder result = new StringBuilder();

        try {
            URL url = new URL("http://www.bsservicess.com/photoUpload/star_avg.php?bookName=" + book_name);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            BufferedReader reader = new BufferedReader(new InputStreamReader( in ));

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

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }

        return result.toString();
    }

    @Override
    protected void onPostExecute(String result) {
        JsonParser parser = new JsonParser();
        JsonObject object = (JsonObject) parser.parse(result);
        String value = object.get("avg").getAsString();
    }

}

但首先要从网络回复中删除警告!