1 |
package com.caddi.android.caddipictureupload; |
2 |
|
3 |
|
4 |
import android.util.Log; |
5 |
|
6 |
import com.caddi.android.caddipictureupload.util.MultipartUtility; |
7 |
|
8 |
import org.simpleframework.xml.Serializer; |
9 |
import org.simpleframework.xml.core.Persister; |
10 |
|
11 |
import java.io.ByteArrayInputStream; |
12 |
import java.net.URL; |
13 |
import java.nio.charset.Charset; |
14 |
import java.util.List; |
15 |
import java.util.Map; |
16 |
import java.util.TreeMap; |
17 |
|
18 |
import dk.thoerup.genericjavautils.HttpUtil; |
19 |
|
20 |
public class WebserviceWrapper { |
21 |
|
22 |
|
23 |
final static String BASE_URL = "http://vps.caddi.com/caddi/mobilexml.php?"; |
24 |
|
25 |
private static String sessionKey; |
26 |
|
27 |
|
28 |
public static boolean login(String caddiID, String username, String password) throws Exception{ |
29 |
/*if (caddiID.equals("test") && username.equals("test") && password.equals("test")) { |
30 |
return true; |
31 |
}*/ |
32 |
|
33 |
Map<String,String> params = new TreeMap<String,String>(); |
34 |
params.put("action", "login"); |
35 |
params.put("organisation", caddiID); |
36 |
params.put("username", username); |
37 |
params.put("password", password); |
38 |
|
39 |
|
40 |
WebserviceResponse res = webserviceHelper(params); |
41 |
|
42 |
if (res.errorCode == 0) { |
43 |
sessionKey = res.sessionKey; |
44 |
return true; |
45 |
} else { |
46 |
throw new Exception( res.errorText ); |
47 |
} |
48 |
} |
49 |
|
50 |
public static List<String> getFolders() throws Exception{ |
51 |
Map<String,String> params = new TreeMap<String,String>(); |
52 |
params.put("action", "imagefolders"); |
53 |
params.put("sessionkey", sessionKey); |
54 |
|
55 |
WebserviceResponse res = webserviceHelper(params); |
56 |
|
57 |
if (res.errorCode == 0) { |
58 |
return res.folders; |
59 |
} else { |
60 |
throw new Exception( res.errorText ); |
61 |
} |
62 |
|
63 |
} |
64 |
|
65 |
public static WebserviceResponse uploadFile(String folder, String title, String description, byte byteArray[]) throws Exception { |
66 |
/*Map<String,String> params = new TreeMap<String,String>(); |
67 |
params.put("action", "putimage"); |
68 |
params.put("sessionkey", sessionKey); |
69 |
params.put("folder", folder); |
70 |
params.put("title", title); |
71 |
params.put("text", description); |
72 |
|
73 |
String paramString = HttpUtil.encodeParams( params ); |
74 |
*/ |
75 |
|
76 |
MultipartUtility multi = new MultipartUtility( new URL(BASE_URL) ); |
77 |
multi.addFormField("action", "putimage"); |
78 |
multi.addFormField("sessionkey", sessionKey); |
79 |
multi.addFormField("folder", folder); |
80 |
multi.addFormField("title", title); |
81 |
multi.addFormField("text", description); |
82 |
|
83 |
multi.addFilePart("image", "image.jpg", new ByteArrayInputStream(byteArray) ); |
84 |
|
85 |
byte res[] = multi.finish(); |
86 |
|
87 |
String responseStr = new String(res, Charset.forName("UTF-8")); |
88 |
Log.i("Caddi", responseStr); |
89 |
|
90 |
Serializer serializer = new Persister(); |
91 |
WebserviceResponse resObj = serializer.read(WebserviceResponse.class, responseStr); |
92 |
|
93 |
return resObj; |
94 |
} |
95 |
|
96 |
private static WebserviceResponse webserviceHelper(Map<String, String> parameters) throws Exception { |
97 |
|
98 |
String paramString = HttpUtil.encodeParams( parameters ); |
99 |
|
100 |
String uri = BASE_URL + paramString; |
101 |
|
102 |
String responseStr = HttpUtil.getContentString(uri, 500); |
103 |
|
104 |
Log.i("Caddi", responseStr); |
105 |
|
106 |
Serializer serializer = new Persister(); |
107 |
WebserviceResponse res = serializer.read(WebserviceResponse.class, responseStr); |
108 |
|
109 |
return res; |
110 |
} |
111 |
} |