Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- try {
- URL url = new URL(baseURL+ "/sortbyname");
- HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
- //sortbyname: 2-name
- JSONObject jsonParam = new JSONObject();
- jsonParam.put("username", params[1]);
- urlConnection.setDoOutput(true);
- urlConnection.setRequestProperty("Content-Type", "application/json");
- urlConnection.setRequestMethod("POST");
- urlConnection.connect();
- DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
- wr.writeBytes(jsonParam.toString());
- wr.flush();
- wr.close();
- int response_code = urlConnection.getResponseCode();
- // Check if successful connection made
- if (response_code == HttpURLConnection.HTTP_OK) {
- // Read data sent from server
- InputStream input = urlConnection.getInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(input));
- StringBuilder result = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null) {
- result.append(line);
- }
- // Parse JSON object and return it
- JSONObject obj = new JSONObject(result.toString());
- if(obj.getString("success").equals("false")){
- return (obj.getString("errormsg").toString());
- }
- else {
- JSONArray arr = obj.getJSONArray("users");
- int length = arr.length();
- if(length == 0){
- return "empty";
- }
- String usernames_list = "";
- for(int i = 0; i<length; i++){
- String username = arr.getJSONObject(i).getString("name");
- usernames_list = usernames_list + username + ",";
- }
- return usernames_list;
- }
- } else {
- return ("unsuccessful");
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (JSONException e) {
- e.printStackTrace();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement