在哪里(也许如何)使用JSONObject解析我的JSONstring

时间:2016-12-23 00:20:19

标签: android json api

好的,所以我有一个活动将API流式传输到JsonStr。我已经通过将其发送到TextView来验证它们的全部内容。工作得很好。这是一个示例API,很简单。

    {
   "shapePoints":[
      39.74012,
      -104.9849,
    ],
   "elevationProfile":[
      {
         "distance":0,
         "height":1616
      },
      ],
   "info":{
      "copyright":{
         "text":"© 2016 MapQuest, Inc.",
         "imageUrl":"http://api.mqcdn.com/res/mqlogo.gif",
         "imageAltText":"© 2016 MapQuest, Inc."
      },
      "statuscode":0,
      "messages":[

      ]
   }
}

我无法弄清楚如何解析“身高”。 JSONObject的代码看起来很简单。当我把JSONObject代码放在“doInBackground”中时对我不起作用。也不是“PostExecute”。 JSONObject代码应该在哪里获取“高度”值?这是我的代码。

public class ElevationActivity extends AppCompatActivity {
TextView tv_elevationJson;
TextView tv_elevationOnly;
Button btn_fetchElevation;
// Will contain the raw JSON response as a string.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_elevation);
    tv_elevationJson = (TextView) findViewById(R.id.tv_elevation_json);
    tv_elevationOnly = (TextView) findViewById(R.id.tv_elevationOnly);
    btn_fetchElevation = (Button) findViewById(R.id.btn_fetch_elevation);

    btn_fetchElevation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new FetchElevationData().execute();
        }
    });
}
private class FetchElevationData extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;

        try {
            double lat = 35.111716;
            double lon = -85.245095;
            String endpoint = String.format("http://open.mapquestapi.com/elevation/v1/profile?key=xxxxxxxxxxx&shapeFormat=raw&latLngCollection=%s,%s", lat, lon);
            URL url = new URL(endpoint
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            // Read the input stream into a String
            InputStream inputStream = urlConnection.getInputStream();
            StringBuilder builder = new StringBuilder();
            if (inputStream ==null) {
                // Nothing to do.
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) !=null) {
                builder.append(line + "\n");
            }

            if (builder.length() ==0) {
                // Stream was empty.  No point in parsing.
                return null;
            }
            String elevationJsonStr = builder.toString();
            return elevationJsonStr;
        }catch (IOException e) {
            Log.e("PlaceholderFragment", "Error ", e);
            return null;
        }finally{
            if (urlConnection !=null) {
                urlConnection.disconnect();
            }
            if (reader !=null) {
                try {
                    reader.close();
                } catch (final IOException e) {
                    Log.e("PlaceholderFragment", "Error closing stream", e);
                }
            }
        }
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        tv_elevationJson.setText(s);

    }
}

}

1 个答案:

答案 0 :(得分:0)

傻傻的我。即使只有一个高程,API也会设置为数组。我需要使用JSONArray,而不是JSONObject。