0

我是 JSON Parsing 的新手,在从 JSON URL 检索数据时需要一些帮助......我已经阅读了许多关于 JSON Parsing 的教程,它们都对我有很大帮助,但我没有被卡住......我正在尝试从一个

URL = "http://mygogolfteetime.com/iphone/topfive/155"

但我在日志中Json object返回空值。

但是我可以从另一个检索相同的数据

URL = "http://api.mygogolfteetime.com/top_5.php?id=155&apikey=eart3562@#bety7*&6b12zAf*&etrqvbn*&LMQW"

我还附上了我的代码,以便可以帮助我解决我的问题..

我的测试活动的代码:

public class TestActivity extends Activity {

private static final String url = "http://mygogolfteetime.com/iphone/topfive/155";
private static final String TAG = "Top5";


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Log.v(TAG, "URL: "+String.valueOf(url));
    JsonParser jParse = new JsonParser();
    JSONObject json = jParse.getJSONfromUrl(url);
    Log.v(TAG, "Json: "+String.valueOf(json));
    }
}

这是我的 JsonParser 活动的代码:

public class JsonParser {

    static InputStream is;
    static JSONObject jObj;
    static String json = null;
    private static final String TAG = "Top5";

    public JSONObject getJSONfromUrl(String url)
    {
        try
        {
            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, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        try
        {
            jObj = new JSONObject(json);
        }
        catch(JSONException e)
        {
            e.printStackTrace();
        }
        return jObj;
    }
}

我还在 Manifest 文件中添加了 Internet 权限...

<uses-permission android:name="android.permission.INTERNET"/>

提前致谢...

对不起,我的英语不好。

4

2 回答 2

1

您可以使用以下函数来获取 JSON 响应 JSONArray/JSONObject

public Object request(String url) {


            try {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(url);
                HttpResponse response = client.execute(get);

                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    String strRes = EntityUtils.toString(response.getEntity());
                    Object resObj = strRes.startsWith("[") ? new JSONArray(
                            strRes) : new JSONObject(strRes);
                    return resObj;
                }

                return response;
            } catch (Exception e) {
                return null;
            }

    }
于 2012-08-17T05:19:59.000 回答
1

请检查这个...

public class Top5  extends Activity {


    private static final String TAG = "Top5";


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //    Log.v(TAG, "URL: "+String.valueOf(url));
        //    JsonParser jParse = new JsonParser();
        //    JSONObject json = jParse.getJSONfromUrl(url);
        //    Log.v(TAG, "Json: "+String.valueOf(json));

        try {

            System.setProperty("http.keepAlive", "false");
//          URL url = new URL(url .toString());
            URL url = new URL("http://mygogolfteetime.com/iphone/topfive/155");
            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setUseCaches(false); 
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setConnectTimeout(10000);
            connection.connect();
            System.out.println("Connection :"+connection.getResponseCode());

            String rawObjects = readZipStream(connection.getInputStream());

            Log.i("ROW OBJECT::::::::::::","************"+rawObjects);
        } catch (Exception exception) {

            exception.printStackTrace();
        }
    }


    private static String readZipStream(InputStream is) throws IOException,
    ParseException {

        StringBuilder rawObjects = new StringBuilder();

        InputStream zis = new BufferedInputStream(is);
        BufferedReader br = new BufferedReader(new InputStreamReader(zis));
        try {

            String readedLine;
            while ((readedLine = br.readLine()) != null) {
                System.out.println("readedLine :"+readedLine);
                if (readedLine != null && readedLine.trim().length() > 0) {
                    rawObjects.append(readedLine);
                }
            }

        } catch (IOException e) {
            throw new IOException(e.getMessage());
        } finally {
            close(br);
            close(zis);
        }
        return rawObjects.toString();

    }
    private static void close(InputStream inputStream) {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception exception) {

        }
    }



    private static void close(Reader reader) {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (Exception exception) {

        }
    }
}

结果是这样的::

08-17 11:07:20.828: INFO/System.out(455): readedLine :{"top5":[{"course_id":"127","member_id":"155","created_date":"1345098492","status":"1","golfcourse_name":"SilverHorn Golf Club","facilities":"","holes":"0"},{"course_id":"93","member_id":"155","created_date":"1345098472","status":"1","golfcourse_name":"sonu","facilities":"4","holes":"0"},{"course_id":"89","member_id":"155","created_date":"1344838832","status":"1","golfcourse_name":"ranjeet golf course","facilities":"4","holes":"9"}]}
于 2012-08-17T05:41:00.713 回答