我是JSON Parsing的新手,需要一些帮助才能从JSON URL中检索数据... 我已经阅读了很多关于JSON解析的教程,他们都帮助了我很多,但不是我被卡住了.. 我正在尝试从
中检索数据URL = "http://mygogolfteetime.com/iphone/topfive/155"
但我的Json object
会在日志中返回 null 值。
但是我可以从另一个
中检索相同的数据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;
}
}
我还在清单文件中添加了Internet权限...
<uses-permission android:name="android.permission.INTERNET"/>
提前致谢...
抱歉我的英语不好。答案 0 :(得分:1)
您可以使用以下函数来获取JSONArray / JSONObject
的json响应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;
}
}
答案 1 :(得分: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"}]}