Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xxxx.xxxxxxx.xxxxxxxx;
- import android.util.Log;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * Created by mrgre on 08/06/2015.
- */
- public class pastebin {
- private String devkey = "xxxxxxxxxxxxxxxxxxxxx";
- private String pasteURL = "http://www.pastebin.com/api/api_post.php";
- public String submitPaste(String code, String name) throws UnsupportedEncodingException {
- String output = makePaste(code, name);
- String result = "error";
- try {
- if (output.equals("")) {
- result = "error response was blank";
- } else if (!output.substring(0, 4).equals("http")) {
- result = "unknown error "+output;
- } else {
- String[] sUrl = output.split("[/]");
- String key = sUrl[sUrl.length - 1].replace("%0D", "");
- result = "http://www.pastebin.com/" + key;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return result;
- }
- //api bellow here
- public String checkResponse(String response) {
- if (response.substring(0, 15) == "Bad API request")
- return response.substring(17);
- return "";
- }
- private String makePaste(String code,String name)
- throws UnsupportedEncodingException {
- String content = URLEncoder.encode(code, "UTF-8");
- String title = URLEncoder.encode(name+"- #RpgHelper3", "UTF-8");
- String data = "api_paste_name=" + title
- + "&api_paste_expire_date=N&api_option=paste"
- + "&api_dev_key=" + devkey + "&api_paste_code=" + content;
- Log.d("data = ",data);
- String response = page(pasteURL, data);
- /*String check = this.checkResponse(response);
- if (!check.equals(""))
- return check;*/
- return response;
- }
- private String page(String uri, String urlParameters) {
- URL url;
- HttpURLConnection connection = null;
- try {
- // Create connection
- url = new URL(uri);
- connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod("POST");
- connection.setRequestProperty("Content-Type",
- "application/x-www-form-urlencoded");
- connection.setRequestProperty("Content-Length",
- "" + Integer.toString(urlParameters.getBytes().length));
- connection.setRequestProperty("Content-Language", "en-US");
- connection.setUseCaches(false);
- connection.setDoInput(true);
- connection.setDoOutput(true);
- // Send request
- DataOutputStream wr = new DataOutputStream(
- connection.getOutputStream());
- wr.writeBytes(urlParameters);
- wr.flush();
- wr.close();
- // Get Response
- InputStream is = connection.getInputStream();
- BufferedReader rd = new BufferedReader(new InputStreamReader(is));
- String line;
- StringBuffer response = new StringBuffer();
- while ((line = rd.readLine()) != null) {
- response.append(line);
- }
- rd.close();
- Log.d("reaperLog",response.toString());
- return response.toString();
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- } finally {
- if (connection != null) {
- connection.disconnect();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement