Advertisement
mrgreaper2004

Untitled

Jun 8th, 2015
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.79 KB | None | 0 0
  1. package com.xxxx.xxxxxxx.xxxxxxxx;
  2.  
  3. import android.util.Log;
  4.  
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.URLEncoder;
  7. import java.io.BufferedReader;
  8. import java.io.DataOutputStream;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.net.HttpURLConnection;
  12. import java.net.URL;
  13. /**
  14.  * Created by mrgre on 08/06/2015.
  15.  */
  16. public class pastebin {
  17.  
  18.     private String devkey = "xxxxxxxxxxxxxxxxxxxxx";
  19.     private String pasteURL = "http://www.pastebin.com/api/api_post.php";
  20.  
  21.     public String submitPaste(String code, String name) throws UnsupportedEncodingException {
  22.         String output = makePaste(code, name);
  23.         String result = "error";
  24.         try {
  25.             if (output.equals("")) {
  26.                 result = "error response was blank";
  27.             } else if (!output.substring(0, 4).equals("http")) {
  28.                 result = "unknown error "+output;
  29.             } else {
  30.                 String[] sUrl = output.split("[/]");
  31.                 String key = sUrl[sUrl.length - 1].replace("%0D", "");
  32.                 result = "http://www.pastebin.com/" + key;
  33.             }
  34.         } catch (Exception e) {
  35.             e.printStackTrace();
  36.         }
  37.  
  38.         return result;
  39.     }
  40.  
  41.  
  42.     //api bellow here
  43.     public String checkResponse(String response) {
  44.         if (response.substring(0, 15) == "Bad API request")
  45.             return response.substring(17);
  46.         return "";
  47.     }
  48.  
  49.     private String makePaste(String code,String name)
  50.             throws UnsupportedEncodingException {
  51.         String content = URLEncoder.encode(code, "UTF-8");
  52.         String title = URLEncoder.encode(name+"- #RpgHelper3", "UTF-8");
  53.         String data = "api_paste_name=" + title
  54.                 + "&api_paste_expire_date=N&api_option=paste"
  55.                 + "&api_dev_key=" + devkey + "&api_paste_code=" + content;
  56.         Log.d("data = ",data);
  57.         String response = page(pasteURL, data);
  58.         /*String check = this.checkResponse(response);
  59.         if (!check.equals(""))
  60.             return check;*/
  61.         return response;
  62.     }
  63.  
  64.     private String page(String uri, String urlParameters) {
  65.         URL url;
  66.         HttpURLConnection connection = null;
  67.         try {
  68.             // Create connection
  69.             url = new URL(uri);
  70.             connection = (HttpURLConnection) url.openConnection();
  71.             connection.setRequestMethod("POST");
  72.             connection.setRequestProperty("Content-Type",
  73.                     "application/x-www-form-urlencoded");
  74.  
  75.             connection.setRequestProperty("Content-Length",
  76.                     "" + Integer.toString(urlParameters.getBytes().length));
  77.             connection.setRequestProperty("Content-Language", "en-US");
  78.  
  79.             connection.setUseCaches(false);
  80.             connection.setDoInput(true);
  81.             connection.setDoOutput(true);
  82.  
  83.             // Send request
  84.             DataOutputStream wr = new DataOutputStream(
  85.                     connection.getOutputStream());
  86.             wr.writeBytes(urlParameters);
  87.             wr.flush();
  88.             wr.close();
  89.  
  90.             // Get Response
  91.             InputStream is = connection.getInputStream();
  92.             BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  93.             String line;
  94.             StringBuffer response = new StringBuffer();
  95.             while ((line = rd.readLine()) != null) {
  96.                 response.append(line);
  97.             }
  98.             rd.close();
  99.             Log.d("reaperLog",response.toString());
  100.             return response.toString();
  101.  
  102.         } catch (Exception e) {
  103.  
  104.             e.printStackTrace();
  105.             return null;
  106.  
  107.         } finally {
  108.  
  109.             if (connection != null) {
  110.                 connection.disconnect();
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement