1

The JSON data URL: http://fgo6.emscloud.net/json/solarReportData.go?callback=json&reportType=W_w&reportDate=2013-4-15

I am a beginner of Android. I solve this question as follows.

public class JSONParser {

    static InputStream is = null;
    //static JSONObject jObj = null;
    static JSONArray jAry = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONArray getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf8"), 9999999);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            //handle strings
            json = json.substring(json.indexOf("n")+2, json.length()-1);


        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON array
        try {
            jAry = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jAry;

    }

}

I am confused about this question, if you can describe it the more detailed the better.

3
  • There is documentation for gson, there are tutorials and you can search the internet for information.
    – zapl
    Dec 7, 2013 at 17:30
  • You probably don't want "callback=json" in the URL. This wraps the JSON in a function, which is pretty much a web browser thing. Try this URL: fgo6.emscloud.net/json/…
    – Jordan P
    Dec 7, 2013 at 17:37
  • Thanks for your answer.It really helpful. By the way,what's the key word should be used in learning URL function. Dec 9, 2013 at 11:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.