Example usage for twitter4j JSONArray getJSONArray

List of usage examples for twitter4j JSONArray getJSONArray

Introduction

In this page you can find the example usage for twitter4j JSONArray getJSONArray.

Prototype

public JSONArray getJSONArray(int index) throws JSONException 

Source Link

Document

Returns the value at index if it exists and is a JSONArray .

Usage

From source file:TwitterGateway.java

@Override
public void onStatus(Status status) {
    user = status.getUser().getScreenName();

    String mentionStatus = status.getText();
    System.out.println("@" + user + " - " + mentionStatus);
    String paramScreenName = screenName.toLowerCase();
    mentionStatus = mentionStatus.toLowerCase().replace(paramScreenName, "").trim();
    String locale = new String();
    if (mentionStatus.contains(" to ")) {
        location = mentionStatus.split(" to ");
        locale = "en";
    } else if (mentionStatus.contains(" ke ")) {
        location = mentionStatus.split(" ke ");
        locale = "id";
    } else {/*from  w ww.  j  a v  a  2  s. c o  m*/
        location = null;
    }
    boolean statusLocation1 = false;
    boolean statusLocation2 = false;

    if (mentionStatus.equals("help") || mentionStatus.equals(("bantuan"))) {
        try {
            if (mentionStatus.equals("help")) {
                Tweet(user,
                        "For using this Twitter bot for searching public transport route, you can mention...");
                Tweet(user, "'First location' to 'second location', example : BIP to PVJ");
            } else {
                Tweet(user, "Format penggunaan Twitter bot untuk mencari jalur transportasi publik adalah...");
                Tweet(user, "'Lokasi awal' ke 'lokasi tujuan', contoh : BIP ke PVJ.");
            }
        } catch (TwitterException ex) {
            System.out.println("Tweet help error");
        }
    } else if (location.length == 2 && !location[0].contains("@") && !location[1].contains("@")) {
        try {
            if (location[0].equals(location[1])) {
                if (locale.equals("id")) {
                    Tweet(user, "Pencarian tidak dapat dilakukan karena lokasi awal dan lokasi tujuan sama");
                } else if (locale.equals("en")) {
                    Tweet(user, "Route can't be found. Starting location and destination are similar");
                }
            } else {
                System.out.println("Lokasi 1 : " + location[0].trim());
                System.out.println("Lokasi 2 : " + location[1].trim());
                //string destination menampung hasil dari JSONObject hasil pencarian apa saja yang ditemukan dari KIRIGateway.getLatLong
                String destination1 = KIRIGateway.GetLatLong(location[0]);
                String destination2 = KIRIGateway.GetLatLong(location[1]);

                //dimasukan ke JSONObject 
                JSONObject objDest1 = new JSONObject(destination1);
                JSONObject objDest2 = new JSONObject(destination2);

                //memasukan hasil pencarian pertama dari JSONObject ke abribut routingResponse
                JSONObject res1 = objDest1.getJSONArray("searchresult").getJSONObject(0);
                String hasilDest1 = res1.getString("placename");
                latlon[0] = res1.getString("location");
                if (hasilDest1 != null) {
                    statusLocation1 = true;
                }

                JSONObject res2 = objDest2.getJSONArray("searchresult").getJSONObject(0);
                String hasilDest2 = res2.getString("placename");
                latlon[1] = res2.getString("location");
                if (hasilDest2 != null) {
                    statusLocation2 = true;
                }

                //Mendapatkan hasil pencarian lalu dimasukan ke JSONArray paramSteps untuk dipisah-pisah lalu dimasukan ke RoutingResponse
                String hasilPencarian = KIRIGateway.GetTrack(latlon[0], latlon[1], locale);
                JSONObject objTrack = new JSONObject(hasilPencarian);
                JSONObject routingresults = objTrack.getJSONArray("routingresults").getJSONObject(0);
                JSONArray paramSteps = routingresults.getJSONArray("steps");
                //buat variable step, steps, dan routing response
                step = new Step[paramSteps.length()];
                for (int i = 0; i < step.length; i++) {
                    step[i] = new Step(paramSteps.getJSONArray(i).getString(3) + "");
                }
                steps = new Steps(step);

                routingResponse = new RoutingResponse(objTrack.getString("status"), steps);
                if (routingResponse.getStatus().equals("ok")) {
                    for (int i = 0; i < routingResponse.getRoutingResult().getSteps().length; i++) {
                        date = new Date();
                        Tweet(user, routingResponse.getRoutingResult().getSteps()[i].getHumanDescription());
                    }
                    if (locale.equals("id")) {
                        Tweet(user,
                                "Untuk lebih lengkapnya dapat dilihat pada http://kiri.travel?start="
                                        + location[0].replace(" ", "%20") + "&finish="
                                        + location[1].replace(" ", "%20") + "&region=bdo");
                    } else if (locale.equals("en")) {
                        Tweet(user,
                                "For futher information you can visit http://kiri.travel?start="
                                        + location[0].replace(" ", "%20") + "&finish="
                                        + location[1].replace(" ", "%20") + "&region=bdo");
                    }

                    for (int i = 0; i < routingResponse.getRoutingResult().getSteps().length; i++) {
                        System.out.println("@" + user + " "
                                + routingResponse.getRoutingResult().getSteps()[i].getHumanDescription());
                    }
                    System.out.println(
                            "@" + user + " Untuk lebih lengkap silahkan lihat di http://kiri.travel?start="
                                    + location[0].replace(" ", "%20") + "&finish="
                                    + location[1].replace(" ", "%20") + "&region=bdo");
                } else {
                    System.out.println("status error");
                }
            }
        } catch (Exception ex) {
            try {
                if (!statusLocation1) {
                    date = new Date();
                    Tweet(user, location[0] + " tidak ditemukan");
                    System.out.println("@" + user + " " + location[0] + " tidak ditemukan");
                } else if (!statusLocation2) {
                    date = new Date();
                    Tweet(user, location[1] + " tidak ditemukan");
                    System.out.println("@" + user + " " + location[1] + " tidak ditemukan");
                } else {
                    date = new Date();
                    Tweet(user, "Gangguan Koneksi");
                    System.out.println("Gangguan Koneksi");
                }
                //java.util.logging.Logger.getLogger(TwitterGateway.class.getName()).log(Level.SEVERE, null, ex);
            } catch (TwitterException ex1) {
                System.out.println("Error2");
                java.util.logging.Logger.getLogger(TwitterGateway.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

}