如何“样式化”JSON API返回?

时间:2015-08-04 21:40:27

标签: java android json

所以我从JSON API获得了4个字符串,并想知道如何设置它们的样式?例如:我拥有的4个字符串是最大玩家,当前玩家,如果服务器在线或离线,则布尔说真或假,以及当前ping。现在它只是显示如下:

35053

26690

true

205

但我希望它显示为

26690/35053 players online

ping: 205

server online: true

那我该怎么办呢?

MainActivity.java:

package in.untie.hypixelmobile;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends ListActivity {

    private Context context;
    private static String url = "http://api.razex.de/server/status/mc.hypixel.net:25565";

    private static final String PMAX = "Max";
    private static final Boolean ONLINE = false;
    private static final String PING = "Ping";
    private static final String PCURRENT = "Current";

    ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new ProgressTask(MainActivity.this).execute();
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {

        private ProgressDialog dialog;
        private ListActivity activity;
        private Context context;

        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage("Loading");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }

            ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.list_item, new String[]{PCURRENT, PMAX, ONLINE.toString(), PING}, new int[]{R.id.ping, R.id.max, R.id.online, R.id.current});

            setListAdapter(adapter);

            lv = getListView();

        }

        @Override
        protected Boolean doInBackground(String... params) {

            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromURL(url);

            try {

                String ping = json.getString("ping");
                Boolean isonline = json.getBoolean("online");

                JSONObject playersObject = json.getJSONObject("players");
                String pmax = playersObject.getString("max");
                String pcurrent = playersObject.getString("online");

                HashMap<String, String> map = new HashMap<String, String>();

                if (isonline) {

                    map.put(PING, ping);
                    map.put(ONLINE.toString(), isonline.toString());
                    map.put(PMAX, pmax);
                    map.put(PCURRENT, pcurrent);
                    jsonlist.add(map);

                } else {

                    map.put(ONLINE.toString(), isonline.toString());
                    jsonlist.add(map);

                }

            } catch (JSONException e) {

                e.printStackTrace();

            }
            return false;
        }
    }

}

2 个答案:

答案 0 :(得分:1)

String.format()通常用于此类任务,例如

String.format("%d/%d players online\n\nping: %s\n\n server online: %s", PCURRENT, PMAX, PING, ONLINE.toString())

阅读here

答案 1 :(得分:0)

我想这就是你想要的,结果将在jsonResults

package in.untie.hypixelmobile;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class MainActivity extends ListActivity {

    private Context context;
    private static String url = "http://api.razex.de/server/status/mc.hypixel.net:25565";

    private static final String PMAX = "Max";
    private static final Boolean ONLINE = false;
    private static final String PING = "Ping";
    private static final String PCURRENT = "Current";

    ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new ProgressTask(MainActivity.this).execute();
    }

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        StringBuilder jsonResults = new StringBuilder();
        private ProgressDialog dialog;
        private ListActivity activity;
        private Context context;

        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }

        @Override
        protected void onPreExecute() {
            this.dialog.setMessage("Loading");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }

            ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.list_item, new String[]{PCURRENT, PMAX, ONLINE.toString(), PING}, new int[]{R.id.ping, R.id.max, R.id.online, R.id.current});

            setListAdapter(adapter);

            lv = getListView();

        }

        @Override
        protected Boolean doInBackground(String... params) {

            JSONParser jParser = new JSONParser();
            JSONObject json = jParser.getJSONFromURL(url);

            try {

                String ping = json.getString("ping");
                Boolean isonline = json.getBoolean("online");

                JSONObject playersObject = json.getJSONObject("players");
                String pmax = playersObject.getString("max");
                String pcurrent = playersObject.getString("online");

                if (isonline) {
                    jsonResults.append(pcurrent+"/"+pmax+" players online");
                    jsonResults.append("ping: "+ping);
                    jsonResults.append("server online: "+ isonline.toString());


                } else {

                    jsonResults.append("server online: "+  isonline.toString());

                }

            } catch (JSONException e) {

                e.printStackTrace();

            }
            return false;
        }
    }

}