com.autburst.picture.server.PictureServer.java Source code

Java tutorial

Introduction

Here is the source code for com.autburst.picture.server.PictureServer.java

Source

/*
ApicAday - Everyday.. is different, your mood, your life. 
Copyright (c) 2010 
   Oliver Selinger <oliver.selinger@autburst.com>,
   Michael Greifeneder <michael.greifeneder@autburst.com> 
     
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.autburst.picture.server;

import java.io.File;
import java.net.URLEncoder;

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

import android.util.Log;

public class PictureServer {

    private static final String TAG = PictureServer.class.getSimpleName();
    private static final String BASE_URL = "http://server.autburst.com:8080/pictureADay/resources/fileupload";

    public String getUniqueId() throws Exception {
        HttpRequest request = new HttpRequest();
        String response = request.get(BASE_URL);
        Log.d(TAG, "getUniqueId - id: " + response);
        return response;
    }

    public String getVideoUrl(String id, String albumName, float frameRate, String format) throws Exception {
        HttpRequest request = new HttpRequest();
        String url = BASE_URL + "/video/" + id + "/" + URLEncoder.encode(albumName) + "/" + frameRate + "/"
                + format;
        String response = request.get(url);

        Log.d(TAG, "getVideoUrl - url: " + url);

        return response;
    }

    public String[] getImageList(String id) throws Exception {
        HttpRequest request = new HttpRequest();
        String url = BASE_URL + "/" + id;

        Log.d(TAG, "getImageList - url: " + url);

        String response = request.get(url);

        if (response != null && !response.equals("null")) {
            //parse JSON
            JSONObject json = new JSONObject(response);

            JSONArray jsonArray = null;
            String singleFileName = null;
            try {
                jsonArray = json.getJSONArray("files");
            } catch (JSONException e) {
                Log.e(TAG, "No JSONArray returned!");

                //get single filename
                singleFileName = json.getString("files");
            }

            if (jsonArray == null) {
                singleFileName = json.getString("files");
                Log.d(TAG, "getImageList - Server returned one file " + singleFileName);
                return new String[] { singleFileName };
            } else {
                String[] resultList = new String[jsonArray.length()];
                for (int i = 0; i < jsonArray.length(); i++) {
                    resultList[i] = jsonArray.getString(i);
                    Log.d(TAG, "getImageList - Server returned " + resultList[i]);
                }
                return resultList;
            }
        } else {
            Log.d(TAG, "getImageList - Server returned null images");
            return new String[0];
        }
    }

    public void deleteImage(String id, String name) throws Exception {
        HttpRequest request = new HttpRequest();
        String url = BASE_URL + "/" + id + "/" + name;
        request.delete(url);
    }

    public void postImages(String id, File file) throws Exception {
        HttpRequest request = new HttpRequest();
        String url = BASE_URL + "/" + id;
        request.post(url, file);

        Log.d(TAG, "postImages - url: " + url);
    }
}