Advertisement
Guest User

Untitled

a guest
Oct 8th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.92 KB | None | 0 0
  1. package org.sen.aarbs;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.util.ArrayList;
  8.  
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.auth.AuthScope;
  12. import org.apache.http.auth.UsernamePasswordCredentials;
  13. import org.apache.http.client.ClientProtocolException;
  14. import org.apache.http.client.HttpClient;
  15. import org.apache.http.client.entity.UrlEncodedFormEntity;
  16. import org.apache.http.client.methods.HttpDelete;
  17. import org.apache.http.client.methods.HttpGet;
  18. import org.apache.http.client.methods.HttpPost;
  19. import org.apache.http.client.methods.HttpPut;
  20. import org.apache.http.impl.client.DefaultHttpClient;
  21. import org.apache.http.params.BasicHttpParams;
  22. import org.apache.http.params.HttpConnectionParams;
  23. import org.apache.http.params.HttpParams;
  24.  
  25. import android.util.Log;
  26.  
  27. public class Service {
  28.  
  29. private String user;
  30. private String password;
  31. private String root_url;
  32. private String device_id;
  33. public Service(String u, String p, String ru, String did)
  34. {
  35. this.user = u;
  36. this.password = p;
  37. this.root_url = ru;
  38. this.device_id = did;
  39. }
  40.  
  41. public static void handle_http_status( HttpResponse status) throws Exception{
  42. int status_code = status.getStatusLine().getStatusCode();
  43. switch (status_code)
  44. {
  45. case 400:
  46. throw new Exception("record not found");
  47. case 422:
  48. throw new Exception("unprocessable entity");
  49. case 200:
  50. return;
  51. case 201:
  52. return;
  53. default: throw new Exception("unknown error /n" + status.getStatusLine().toString());
  54.  
  55. }
  56. }
  57.  
  58. public static DefaultHttpClient getDefaultHttpClient(int timeout,String user, String password)
  59. {
  60.  
  61. HttpParams httpParams = new BasicHttpParams();
  62. HttpConnectionParams.setConnectionTimeout(httpParams, timeout );
  63. HttpConnectionParams.setSoTimeout(httpParams, timeout);
  64. DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
  65. httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, -1),
  66. new UsernamePasswordCredentials(user,password));
  67. return httpClient;
  68. }
  69.  
  70. public static String inputStreamToString(InputStream is) throws IOException
  71. {
  72. String line = "";
  73. StringBuilder total = new StringBuilder();
  74. BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  75. while ((line = rd.readLine()) != null) {
  76. total.append(line);
  77. }
  78. return total.toString();
  79. }
  80.  
  81. public InputStream getData (String path) throws Exception
  82. {
  83. DefaultHttpClient httpClient = getDefaultHttpClient(20000,user,password);
  84. InputStream content = null;
  85. HttpGet httpget = new HttpGet(this.root_url+path+".json");
  86. HttpResponse httpresponse = httpClient.execute(httpget);
  87. content = httpresponse.getEntity().getContent();
  88. handle_http_status(httpresponse);
  89. return content;
  90. }
  91.  
  92. public InputStream getLogentries() throws Exception
  93. {
  94. return getData("/devices/"+this.device_id+"/logentries");
  95. }
  96.  
  97. public InputStream getSolutions(String problem_id) throws Exception
  98. {
  99. return getData("/problems/"+problem_id+"/solutions");
  100. }
  101.  
  102. public InputStream getProblems() throws Exception
  103. {
  104. return getData("/devices/"+this.device_id+"/problems");
  105. }
  106.  
  107. public InputStream getInstructions() throws Exception
  108. {
  109. return getData("/devices/"+this.device_id+"/instructions");
  110. }
  111.  
  112. public String postLogentry(ArrayList<NameValuePair> log) throws Exception
  113. {
  114. String Uri = postData("/logentries");
  115. putdata_abs(Uri,log);
  116. return Uri;
  117. }
  118.  
  119. public String postProblem(ArrayList<NameValuePair> problem) throws Exception
  120. {
  121. String Uri = postData("/problems");
  122. putdata_abs(Uri,problem);
  123. return Uri;
  124. }
  125.  
  126. public String postSolution(ArrayList<NameValuePair> solution, String problem_id) throws Exception
  127. {
  128. String Uri = postData("/solutions");
  129. Log.i("location",Uri);
  130. Uri = putdata(Uri,solution);
  131. Log.i("location",Uri);
  132. putdata("/problems/"+problem_id+Uri);
  133. return Uri;
  134. }
  135.  
  136. public void deleteData(String path) throws Exception{
  137. DefaultHttpClient httpClient = getDefaultHttpClient(20000,user,password);
  138. HttpDelete httpdelete = new HttpDelete(this.root_url+path+".json");
  139. HttpResponse r = httpClient.execute(httpdelete);
  140. httpClient.getConnectionManager().closeExpiredConnections();
  141. handle_http_status(r);
  142. }
  143.  
  144. public String postData(String path, ArrayList<NameValuePair> nameValuePairs) throws ClientProtocolException, Exception
  145. {
  146. HttpPost httppost = new HttpPost(this.root_url+path+".json");
  147. HttpClient httpclient = getDefaultHttpClient(20000, user, password);
  148. httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  149. HttpResponse r = httpclient.execute(httppost);
  150. handle_http_status(r);
  151. return r.getLastHeader("location").getValue();
  152. }
  153.  
  154. public String postData(String path) throws Exception
  155. {
  156. HttpPost httppost = new HttpPost(this.root_url+path+".json");
  157. HttpClient httpclient = getDefaultHttpClient(20000, user, password);
  158. HttpResponse r = httpclient.execute(httppost);
  159. handle_http_status(r);
  160. httpclient.getConnectionManager().closeExpiredConnections();
  161. return r.getLastHeader("location").getValue();
  162. }
  163.  
  164. public String putdata(String path, ArrayList<NameValuePair> nameValuePairs) throws Exception
  165. {
  166. HttpPut httpput = new HttpPut(this.root_url+path+".json");
  167. HttpClient httpclient = getDefaultHttpClient(20000, user, password);
  168. httpput.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  169. HttpResponse r = httpclient.execute(httpput);
  170. httpclient.getConnectionManager().closeExpiredConnections();
  171. handle_http_status(r);
  172. return r.getLastHeader("location").getValue();
  173. }
  174.  
  175. public void putdata(String path) throws Exception
  176. {
  177. HttpPut httpput = new HttpPut(this.root_url+path+".json");
  178. HttpClient httpclient = getDefaultHttpClient(20000, user, password);
  179. HttpResponse r = httpclient.execute(httpput);
  180. httpclient.getConnectionManager().closeExpiredConnections();
  181. handle_http_status(r);
  182. }
  183. public String putdata_abs(String uri, ArrayList<NameValuePair> nameValuePairs ) throws Exception
  184. {
  185. HttpPut httpput = new HttpPut(uri+".json");
  186. HttpClient httpclient = getDefaultHttpClient(20000, user, password);
  187. httpput.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  188. HttpResponse r = httpclient.execute(httpput);
  189. httpclient.getConnectionManager().closeExpiredConnections();
  190. handle_http_status(r);
  191. return r.getLastHeader("location").getValue();
  192.  
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement