Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sen.aarbs;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.auth.AuthScope;
- import org.apache.http.auth.UsernamePasswordCredentials;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpDelete;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.client.methods.HttpPut;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.BasicHttpParams;
- import org.apache.http.params.HttpConnectionParams;
- import org.apache.http.params.HttpParams;
- import android.util.Log;
- public class Service {
- private String user;
- private String password;
- private String root_url;
- private String device_id;
- public Service(String u, String p, String ru, String did)
- {
- this.user = u;
- this.password = p;
- this.root_url = ru;
- this.device_id = did;
- }
- public static void handle_http_status( HttpResponse status) throws Exception{
- int status_code = status.getStatusLine().getStatusCode();
- switch (status_code)
- {
- case 400:
- throw new Exception("record not found");
- case 422:
- throw new Exception("unprocessable entity");
- case 200:
- return;
- case 201:
- return;
- default: throw new Exception("unknown error /n" + status.getStatusLine().toString());
- }
- }
- public static DefaultHttpClient getDefaultHttpClient(int timeout,String user, String password)
- {
- HttpParams httpParams = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpParams, timeout );
- HttpConnectionParams.setSoTimeout(httpParams, timeout);
- DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
- httpClient.getCredentialsProvider().setCredentials(new AuthScope(null, -1),
- new UsernamePasswordCredentials(user,password));
- return httpClient;
- }
- public static String inputStreamToString(InputStream is) throws IOException
- {
- String line = "";
- StringBuilder total = new StringBuilder();
- BufferedReader rd = new BufferedReader(new InputStreamReader(is));
- while ((line = rd.readLine()) != null) {
- total.append(line);
- }
- return total.toString();
- }
- public InputStream getData (String path) throws Exception
- {
- DefaultHttpClient httpClient = getDefaultHttpClient(20000,user,password);
- InputStream content = null;
- HttpGet httpget = new HttpGet(this.root_url+path+".json");
- HttpResponse httpresponse = httpClient.execute(httpget);
- content = httpresponse.getEntity().getContent();
- handle_http_status(httpresponse);
- return content;
- }
- public InputStream getLogentries() throws Exception
- {
- return getData("/devices/"+this.device_id+"/logentries");
- }
- public InputStream getSolutions(String problem_id) throws Exception
- {
- return getData("/problems/"+problem_id+"/solutions");
- }
- public InputStream getProblems() throws Exception
- {
- return getData("/devices/"+this.device_id+"/problems");
- }
- public InputStream getInstructions() throws Exception
- {
- return getData("/devices/"+this.device_id+"/instructions");
- }
- public String postLogentry(ArrayList<NameValuePair> log) throws Exception
- {
- String Uri = postData("/logentries");
- putdata_abs(Uri,log);
- return Uri;
- }
- public String postProblem(ArrayList<NameValuePair> problem) throws Exception
- {
- String Uri = postData("/problems");
- putdata_abs(Uri,problem);
- return Uri;
- }
- public String postSolution(ArrayList<NameValuePair> solution, String problem_id) throws Exception
- {
- String Uri = postData("/solutions");
- Log.i("location",Uri);
- Uri = putdata(Uri,solution);
- Log.i("location",Uri);
- putdata("/problems/"+problem_id+Uri);
- return Uri;
- }
- public void deleteData(String path) throws Exception{
- DefaultHttpClient httpClient = getDefaultHttpClient(20000,user,password);
- HttpDelete httpdelete = new HttpDelete(this.root_url+path+".json");
- HttpResponse r = httpClient.execute(httpdelete);
- httpClient.getConnectionManager().closeExpiredConnections();
- handle_http_status(r);
- }
- public String postData(String path, ArrayList<NameValuePair> nameValuePairs) throws ClientProtocolException, Exception
- {
- HttpPost httppost = new HttpPost(this.root_url+path+".json");
- HttpClient httpclient = getDefaultHttpClient(20000, user, password);
- httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- HttpResponse r = httpclient.execute(httppost);
- handle_http_status(r);
- return r.getLastHeader("location").getValue();
- }
- public String postData(String path) throws Exception
- {
- HttpPost httppost = new HttpPost(this.root_url+path+".json");
- HttpClient httpclient = getDefaultHttpClient(20000, user, password);
- HttpResponse r = httpclient.execute(httppost);
- handle_http_status(r);
- httpclient.getConnectionManager().closeExpiredConnections();
- return r.getLastHeader("location").getValue();
- }
- public String putdata(String path, ArrayList<NameValuePair> nameValuePairs) throws Exception
- {
- HttpPut httpput = new HttpPut(this.root_url+path+".json");
- HttpClient httpclient = getDefaultHttpClient(20000, user, password);
- httpput.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- HttpResponse r = httpclient.execute(httpput);
- httpclient.getConnectionManager().closeExpiredConnections();
- handle_http_status(r);
- return r.getLastHeader("location").getValue();
- }
- public void putdata(String path) throws Exception
- {
- HttpPut httpput = new HttpPut(this.root_url+path+".json");
- HttpClient httpclient = getDefaultHttpClient(20000, user, password);
- HttpResponse r = httpclient.execute(httpput);
- httpclient.getConnectionManager().closeExpiredConnections();
- handle_http_status(r);
- }
- public String putdata_abs(String uri, ArrayList<NameValuePair> nameValuePairs ) throws Exception
- {
- HttpPut httpput = new HttpPut(uri+".json");
- HttpClient httpclient = getDefaultHttpClient(20000, user, password);
- httpput.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- HttpResponse r = httpclient.execute(httpput);
- httpclient.getConnectionManager().closeExpiredConnections();
- handle_http_status(r);
- return r.getLastHeader("location").getValue();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement