私はこれに多くの問題を抱えています。私は解析したjson配列を持っていますが、これはうまく機能します。解析しようとしている単一のユーザー用の別の Web ページがあります。ここに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;
}}
ここでは、値の 1 つを取得しようとしています。
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();
}}}}