Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- implementation 'com.squareup.okhttp3:okhttp:4.9.0'
- package com.joythis.android.simplesthttprw;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.util.Log;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import javax.net.ssl.HttpsURLConnection;
- import okhttp3.FormBody;
- import okhttp3.OkHttpClient;
- import okhttp3.Request;
- import okhttp3.RequestBody;
- import okhttp3.Response;
- /**
- * Created by Artur Marques on 2018.
- */
- public class AmIoHttp {
- public static String io_https_ReadAll(
- String pUrl
- ){
- String ret="";
- HttpURLConnection urlConnection = null;
- try {
- URL url = new URL(pUrl);
- urlConnection = (HttpsURLConnection) url.openConnection();
- InputStream in = urlConnection.getInputStream();
- InputStreamReader isr = new InputStreamReader(in);
- BufferedReader br = new BufferedReader(isr);
- String strLine = "";
- while ((strLine = br.readLine())!=null){
- ret+=strLine;
- }
- br.close();
- isr.close();
- in.close();
- } catch (Exception e) {
- Log.e(
- "@AmIoHttp",
- e.toString()
- );
- e.printStackTrace();
- } finally //The finally block always executes when the try block exits
- {
- if (urlConnection != null) {
- urlConnection.disconnect();
- }//if
- }//finally
- return ret;
- }//io_https_ReadAll
- public static String io_http_ReadAll(
- String pUrl
- ){
- String ret="";
- HttpURLConnection urlConnection = null;
- try {
- URL url = new URL(pUrl);
- urlConnection = (HttpURLConnection) url.openConnection();
- InputStream in = urlConnection.getInputStream();
- InputStreamReader isr = new InputStreamReader(in);
- BufferedReader br = new BufferedReader(isr);
- String strLine = "";
- while ((strLine = br.readLine())!=null){
- ret+=strLine;
- }
- br.close();
- isr.close();
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- } finally //The finally block always executes when the try block exits
- {
- if (urlConnection != null) {
- urlConnection.disconnect();
- }//if
- }//finally
- return ret;
- }//io_http_ReadAll
- public final static String TAG = "@AmIoHttp";
- public static Bitmap readBitmapFromUrl(
- String pUrl
- ){
- Bitmap ret = null;
- try{
- URL url = new URL(pUrl);
- InputStream is = url.openStream();
- ret = BitmapFactory.decodeStream(is);
- is.close();
- }
- catch (Exception e){
- Log.e(
- TAG,
- e.getMessage()
- );
- }
- return ret;
- }//readBitmapFromUrl
- public static String postShare(
- String pUrl,
- String pWhen,
- String pWhat
- ){
- OkHttpClient client = new OkHttpClient();
- //build a post request
- RequestBody requestBody = new FormBody.Builder()
- .add("when", pWhen)
- .add("what", pWhat)
- .build();
- Request request = new Request.Builder()
- //.url("https://arturmarques.com/edu/ddm/files/w12/receiver.php")
- .url(pUrl)
- .post(requestBody)
- .build();
- try {
- Response response = client.newCall(request).execute();
- // Do something with the response.
- String strRet = response.body().string();
- return strRet;
- } catch (IOException e) {
- e.printStackTrace();
- }
- return "FAIL";
- }//postShare
- }//AmIoHttp
Advertisement
Add Comment
Please, Sign In to add comment