我在解析json字符串之后在我的散列映射中排序修剪值时遇到问题。如何对散列值中的修剪值进行排序
//Read data
String line;
while ((line = bufferedReader.readLine()) != null) {
result.append(line);
}
JSONObject jsonObjectResult = new JSONObject(result.toString().trim());
JSONArray jsonArrayApps = jsonObjectResult.getJSONArray("apps");
try
{
for (int i=0; i<jsonArrayApps.length(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = jsonArrayApps.getJSONObject(i);
map.put("Name", e.getString("package").trim());
map.put("cate", e.getString("category").trim());
mylist.add(map); //mylist is a variable for a listview
}
}
catch (JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
}
以下是结果
list of installed apps and their categories (Click to view)
我需要整理的修整值帮助,使排序的数据可以在自己的类别。 click here
这里是我的JSON代码
public class now extends AsyncTask <HttpURLConnection, Void,Void>
{
private void getAppCategories() throws IOException, JSONException
{
BufferedReader bufferedReader = null;
HttpURLConnection urlConnection = null;
BufferedWriter bufferedWriter = null;
StringBuilder result = new StringBuilder();
//Create JSON object to send to webservice
JSONObject jsonObjectSend = new JSONObject();
JSONArray jsonArrayPakages = new JSONArray();
PackageManager packageManager;
List<ResolveInfo> listApps; //this list store all app in device
try
{
packageManager = getActivity().getPackageManager();
Intent filterApp = new Intent(Intent.ACTION_MAIN);
filterApp.addCategory(Intent.CATEGORY_LAUNCHER);
listApps = packageManager.queryIntentActivities(filterApp, PackageManager.GET_META_DATA);
for (ResolveInfo app : listApps)
{
jsonArrayPakages.put(app.activityInfo.packageName.trim());
}
jsonObjectSend.put("packages", jsonArrayPakages);
Log.d("json", jsonObjectSend.toString());
URL url = new URL("http://getdatafor.appspot.com/data?key=53972606b926d38191a5446fdff89e377873d767fabedf6d");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(30000); /* milliseconds */
urlConnection.setReadTimeout(30000); /* milliseconds */
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application-json");
urlConnection.setDoOutput(true); /* allow output to send data */
urlConnection.connect();
OutputStream outputStream = urlConnection.getOutputStream();
bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write(jsonObjectSend.toString());
bufferedWriter.flush();
InputStream inputStream = urlConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//continues from (//read line)
}
}
}
@Override
protected Void doInBackground(HttpURLConnection... params) {
try {
getAppCategories();
} catch (IOException e) {
Log.d("tag", "Net work error: " + e.getMessage(), e);
} catch (JSONException e) {
Log.d("tag", "JSON is not valid: " + e.getMessage(), e);
}
return null;
}
你有固定数量的分类清单吗? –
不,我没有@patel ..它提取在线类别... – vitus
@vitus可以请你分享你的json格式,你想只显示每个应用程序在它的类别下,而不是在未分类列表下吗? –