Advertisement
Guest User

Untitled

a guest
Jan 10th, 2013
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. package ant.junior.webapi;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.ClientProtocolException;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.methods.HttpGet;
  11. import org.apache.http.client.methods.HttpPost;
  12. import org.apache.http.impl.client.DefaultHttpClient;
  13. import org.apache.http.protocol.BasicHttpContext;
  14. import org.apache.http.protocol.HttpContext;
  15.  
  16. import android.util.Base64;
  17.  
  18. public class Api {
  19.  
  20.     HttpClient httpClient = new DefaultHttpClient();
  21.     String baseApiUrl;
  22.     HttpPost httpPost;
  23.     String result;
  24.     HttpContext localContext = new BasicHttpContext();
  25.     HttpGet httpGet;
  26.     HttpResponse response;
  27.    
  28.     String headerRequest;
  29.    
  30.     public Api(String baseUrl){
  31.         this.baseApiUrl = baseUrl;
  32.         this.result = "";
  33.         this.headerRequest = "";
  34.     }
  35.    
  36.     public void updateUrl(String url){
  37.         //Update the get
  38.         httpGet = new HttpGet(this.baseApiUrl+url);
  39.        
  40.         //Update the post
  41.         httpPost = new HttpPost(this.baseApiUrl+url);
  42.     }
  43.    
  44.     public void updateHeader(String user, String password){
  45.         httpGet.setHeader("Authorization", "Basic " + Base64.encodeToString((user+":"+password).getBytes(), Base64.NO_WRAP));
  46.        
  47.         httpPost.setHeader("Authorization", "Basic " + Base64.encodeToString((user+":"+password).getBytes(), Base64.NO_WRAP));
  48.     }
  49.    
  50.     public String get(){
  51.         System.out.println();
  52.         try {
  53.             this.response = httpClient.execute(httpGet, localContext);
  54.         } catch (ClientProtocolException e1) {
  55.             // TODO Auto-generated catch block
  56.             e1.printStackTrace();
  57.         } catch (IOException e1) {
  58.             // TODO Auto-generated catch block
  59.             e1.printStackTrace();
  60.         }
  61.         BufferedReader reader = null;
  62.         try {
  63.             reader = new BufferedReader(
  64.                     new InputStreamReader(
  65.                       response.getEntity().getContent()
  66.                     )
  67.                   );
  68.         } catch (IllegalStateException e) {
  69.             // TODO Auto-generated catch block
  70.             e.printStackTrace();
  71.         } catch (IOException e) {
  72.             // TODO Auto-generated catch block
  73.             e.printStackTrace();
  74.         }
  75.              
  76.             String line = "";
  77.             try {
  78.                 while ((line = reader.readLine()) != null){
  79.                   result += line + "\n";
  80.                 }
  81.             } catch (IOException e) {
  82.                 // TODO Auto-generated catch block
  83.                 e.printStackTrace();
  84.             }
  85.             System.out.println(result);
  86.             return result;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement