Ich möchte JSON nach unten sortieren, ich kann JSON von oben nach unten analysieren, aber ich brauche von unten nach oben.Wie JSON nach unten sortieren
Ist es möglich? Wie kann ich das machen?
kann ich json mit diesen Codes analysieren:
MainActivity Datei:
public class MainActivity extends AppCompatActivity {
private ListView lv;
private static String url = "https://api.androidhive.info/contacts/";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(url);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray contacts = jsonObj.getJSONArray("contacts");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String name = c.getString("name");
String email = c.getString("email");
HashMap<String, String> contact = new HashMap<>();
contact.put("name", name);
contact.put("email", email);
contactList.add(contact);
}
} catch (final JSONException e) {
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.list_item, new String[]{"name", "email"}, new int[]{R.id.name,
R.id.email});
lv.setAdapter(adapter);
}
}
}
Httphandler-Datei:
public class HttpHandler {
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (Exception e) {
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
JSON-Datei:
{
"contacts": [
{
"name": "leonardo",
"email": "[email protected]"
}
},
{
"name": "Johnny",
"email": "[email protected]"
}
},
{
"name": "jack",
"email": "[email protected]"
}
}
{
"name": "paul",
"email": "[email protected]"
}
}
]
}
Ausgabe:
leonardo - [email protected]
-----------------------------
johnny - [email protected]
-----------------------------
jack - [email protected]
-----------------------------
paul - [email protected]
aber zunächst möchte ich so das letzte Element zeigen:
paul - [email protected]
-----------------------------
jack - [email protected]
-----------------------------
johnny - [email protected]
-----------------------------
leonardo - [email protected]
Hallo, du bist perfekt. Es läuft gut. Danke mein Freund. – joohnd