我在这方面遇到了很多麻烦。我有一个解析过的 json 数组,效果很好。我有另一个网页供我尝试解析的单个用户使用。这是php。它的有效json。
{
"userURL": "http://forum.example.com.php?u=426561",
"userId": "426561",
"forumName": "example name",
"totalPosts": "19787",
"postsPerDay": "8.11",
"totalThanks": "40585",
"joinDate": "2007-02-20",
"yearsJoined": "6",
"referrals": "17",
"friendCount": "61",
"recognizedDeveloper": "1",
"recognizedContributor": "0",
"recognizedThemer": "0",
"moderator": "0",
"recognizedEliteDeveloper": "0",
"romCount": "100",
"kernelCount": "1",
"tutorialCount": "0",
"modCount": "21",
"themeCount": "0",
"originalAppCount": "0",
"toolkitCount": "0",
"scriptCount": "0",
"sOffCount": "0",
"recoveryCount": "0",
"score": "687",
"rank": "1 out of 122"
}
这是我的解析器类。
public class JSONParser1 {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser1() {
}
public JSONObject 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, "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) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}}
在这里,我只是想抓住其中一个价值。
public class GetScore extends Activity {
private static String url = "http://example.com"
private static String TAG_NAME = "forumName";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getscore);
JSONParser1 jParser1 = new JSONParser1();
JSONObject json = jParser1.getJSONFromUrl(url);
try {
json = new JSONObject(TAG_NAME);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//get nickname
try {
json.getString("forumName");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
///.... get other value for object
}
}
我如何将它与资源 ID 联系起来?任何帮助将不胜感激。
public class GetScore extends Activity {
private static String url = "http://example.com"
private static String TAG_NAME = "forumName";
private JSONParser1 jParser1 ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getscore);
jParser1 = new JSONParser1();
new GetJSONTask().execute(url);
}
class GetJSONTask extends AsyncTask<String, Void, JSONObject> {
private Exception exception;
protected JSONObject doInBackground(String... urls) {
try {
JSONParser1 jParser = new JSONParser1();
return jParser.getJSONFromUrl(urls[0]);
} catch (Exception e) {
return null;
}
}
protected void onPostExecute(JSONObject json) {
// do all the parsing here:
//get nickname
try {
// You then reset the same variable here
// the previous object is now lost, or if it throws an exception,
// the previously set value remains (whether null or not)
json = new JSONObject(TAG_NAME);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// This object could easily be null, or could be valid if the previous
// call failed
try {
json.getString("forumName");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}}}