Example usage for org.json JSONArray toString

List of usage examples for org.json JSONArray toString

Introduction

In this page you can find the example usage for org.json JSONArray toString.

Prototype

public String toString() 

Source Link

Document

Make a JSON text of this JSONArray.

Usage

From source file:abelymiguel.miralaprima.GetHistory.java

/**
 * Processes requests for both HTTP// w  ww .  j  a  v  a2s .c  o m
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    try {
        _con = Utils.getConnection();
        _stmt = _con.createStatement();
    } catch (URISyntaxException ex) {
        Logger.getLogger(GetMoza.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(GetMoza.class.getName()).log(Level.SEVERE, null, ex);
    }

    try {
        out = response.getWriter();
    } catch (IOException ex) {
        Logger.getLogger(GetMoza.class.getName()).log(Level.SEVERE, null, ex);
    }
    response.setContentType("text/javascript;charset=UTF-8");

    String country_code;
    int limit;
    String limitStr;
    country_code = request.getParameter("country_code");
    limitStr = request.getParameter("limit");

    if (limitStr != null) {
        limit = Integer.parseInt(limitStr);
    } else {
        limit = -1;
    }

    String json_str;
    JSONArray jsonArray = new JSONArray(this.getHistory(country_code, limit));
    json_str = jsonArray.toString();

    String jsonpCallback = request.getParameter("callback");
    if (jsonpCallback != null) {
        out.write(jsonpCallback + "(" + json_str + ")");
    } else {
        out.println(json_str);
    }

    try {
        _con.close();
        _stmt.close();
        _rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(GetPrima.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.close();
}

From source file:abelymiguel.miralaprima.GetPrima.java

/**
 * Processes requests for both HTTP/*from   w  ww  . ja v a  2 s.c  om*/
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        _con = Utils.getConnection();
        _stmt = _con.createStatement();

    } catch (URISyntaxException ex) {
        Logger.getLogger(GetPrima.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(GetPrima.class.getName()).log(Level.SEVERE, null, ex);
    }
    PrintWriter out = response.getWriter();
    response.setContentType("text/javascript;charset=UTF-8");

    String country_code;
    country_code = request.getParameter("country_code");

    JSONObject jsonObject;
    JSONArray jsonArray;
    String json_str;
    if (country_code != null) {
        jsonObject = new JSONObject(this.getCountry(country_code));
        json_str = jsonObject.toString();
    } else {
        jsonArray = new JSONArray(this.getAllCountries());
        json_str = jsonArray.toString();
    }

    String jsonpCallback = request.getParameter("callback");
    if (jsonpCallback != null) {
        out.write(jsonpCallback + "(" + json_str + ")");
    } else {
        out.println(json_str);
    }
    try {
        _con.close();
        _stmt.close();
        _rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(GetPrima.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.close();
}

From source file:abelymiguel.miralaprima.GetMoza.java

/**
 * Processes requests for both HTTP//from w w w .  j a v  a  2 s  . co m
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) {
    response.setContentType("text/html;charset=UTF-8");
    try {
        _con = Utils.getConnection();
        _stmt = _con.createStatement();
    } catch (URISyntaxException ex) {
        Logger.getLogger(GetMoza.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(GetMoza.class.getName()).log(Level.SEVERE, null, ex);
    }

    PrintWriter out = null;
    try {
        out = response.getWriter();
    } catch (IOException ex) {
        Logger.getLogger(GetMoza.class.getName()).log(Level.SEVERE, null, ex);
    }
    response.setContentType("text/javascript;charset=UTF-8");

    String country_code;
    int limit;
    String limitStr;
    country_code = request.getParameter("country_code");
    limitStr = request.getParameter("limit");

    if (limitStr != null) {
        limit = Integer.parseInt(limitStr);
    } else {
        limit = -1;
    }

    String json_str;
    JSONArray jsonArray = new JSONArray(this.searchInDB(country_code, limit));
    json_str = jsonArray.toString();

    String jsonpCallback = request.getParameter("callback");
    if (jsonpCallback != null) {
        out.write(jsonpCallback + "(" + json_str + ")");
    } else {
        out.println(json_str);
    }

    try {
        _con.close();
        _stmt.close();
        _rs.close();
    } catch (SQLException ex) {
        Logger.getLogger(GetPrima.class.getName()).log(Level.SEVERE, null, ex);
    }
    out.close();
}

From source file:i5.las2peer.services.analytics.AnalyticsClass.java

@GET
@Path("")/*from w w  w  .  j a  v a2  s  .  c  o  m*/
public HttpResponse getAnalytics(@QueryParam(name = "key", defaultValue = "*") String key,
        @QueryParam(name = "value", defaultValue = "0") int value) {

    dbm = new DatabaseManager();
    dbm.init(driverName, databaseServer, port, database, this.username, password, hostName);
    JSONArray analytics = null;
    HttpResponse r = null;

    if (key.equals("edge"))
        analytics = dbm.getAnalyticsByEdge(value);

    else if (key.equals("user"))
        analytics = dbm.getAnalyticsByUser(value);

    else {
        r.setStatus(404);
        return r;

    }
    r = new HttpResponse(analytics.toString());
    r.setStatus(200);

    return r;
}

From source file:com.googlecode.CallerLookup.Main.java

public void saveUserLookupEntries() {
    try {//from  w  ww  . j a va  2  s.co  m
        FileOutputStream file = getApplicationContext().openFileOutput(SAVED_FILE, MODE_PRIVATE);
        JSONArray userLookupEntries = new JSONArray();
        for (String lookupEntryName : mUserLookupEntries.keySet()) {
            try {
                userLookupEntries.put(mUserLookupEntries.get(lookupEntryName).toJSONObject());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        OutputStreamWriter content = new OutputStreamWriter(file);
        content.write(userLookupEntries.toString());
        content.flush();
        content.close();
        file.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.layer.atlas.messenger.AtlasIdentityProvider.java

private boolean save() {
    Collection<Participant> participants;
    synchronized (participantsMap) {
        participants = participantsMap.values();
    }/*w  ww.  j a v a2  s  .  c  o  m*/

    JSONArray contactsJson;
    try {
        contactsJson = new JSONArray();
        for (Participant participant : participants) {
            JSONObject contactJson = new JSONObject();
            contactJson.put("id", participant.userId);
            contactJson.put("first_name", participant.firstName);
            contactJson.put("last_name", participant.lastName);
            contactsJson.put(contactJson);
        }
    } catch (JSONException e) {
        Log.e(TAG, "Error while saving", e);
        return false;
    }

    SharedPreferences.Editor editor = context.getSharedPreferences("contacts", Context.MODE_PRIVATE).edit();
    editor.putString("json", contactsJson.toString());
    editor.commit();

    return true;
}

From source file:com.snappy.couchdb.ConnectionHandler.java

public static JSONArray getJsonArray(String url, String userId, String token) throws ClientProtocolException,
        IOException, JSONException, SpikaException, IllegalStateException, SpikaForbiddenException {

    JSONArray retVal = null;

    InputStream is = httpGetRequest(url, userId);
    String result = getString(is);

    is.close();/*  w  ww . ja va2 s . c o m*/

    retVal = jArrayFromString(result);

    Logger.debug("Response: ", retVal.toString());
    return retVal;
}

From source file:com.openAtlas.bundleInfo.maker.BundleMakeBooter.java

public static void main(String[] args) throws JSONException, IOException {
    //if(args.length!=2){
    //   throw new  IOException(" args to less , usage plugin_dir out_put_json_path");
    //}/*from w  ww. j  a  v a 2 s. co  m*/

    args = new String[2];
    args[0] = "C:\\Users\\kltz\\Desktop\\AtlasDemo\\plugin";
    args[1] = "C:\\Users\\kltz\\Desktop\\AtlasDemo\\plugin\\bundle-info.json";

    String path = args[0];
    ApkPreProcess.preProcess(path);
    String targetFile = args[1];
    File dirFile = new File(path);
    JSONArray jsonArray = new JSONArray();
    File[] files = dirFile.listFiles();
    for (File file : files) {
        if (file.getAbsolutePath().contains("libcom")) {
            PackageLite packageLit = PackageLite.parse(file.getAbsolutePath());
            jsonArray.put(packageLit.getBundleInfo());
            //            try {
            //                packageLit.getBundleInfo().toString();
            //            } catch (JSONException e) {
            //               // TODO Auto-generated catch block
            //               e.printStackTrace();
            //            }
        }

    }
    org.apache.commons.io.FileUtils.writeStringToFile(new File(targetFile), jsonArray.toString());
    System.out.println(jsonArray.toString());
}

From source file:org.seadpdt.impl.PeopleServicesImpl.java

@GET
@Path("/list/")
@Produces(MediaType.APPLICATION_JSON)/*from  w  w w .java 2 s. co  m*/
public Response getPeopleListAsArray() {
    FindIterable<Document> iter = peopleCollection.find();
    iter.projection(getBasicPersonProjection());

    MongoCursor<Document> cursor = iter.iterator();
    JSONArray array = new JSONArray();
    while (cursor.hasNext()) {
        Document next = cursor.next();
        next.put("@context", getPersonContext());
        array.put(next);
    }
    return Response.ok(array.toString()).cacheControl(control).build();
}

From source file:net.dv8tion.jda.core.requests.RestAction.java

protected RequestBody getRequestBody(JSONArray array) {
    this.rawData = array;

    return array == null ? null : RequestBody.create(Requester.MEDIA_TYPE_JSON, array.toString());
}