Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.78 KB | None | 0 0
  1. package com.example.myapplication.restcommunication;
  2.  
  3. import com.example.myapplication.model.ResponseResult;
  4.  
  5. import java.io.IOException;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9.  
  10. public class RestPost extends RestParent {
  11.  
  12.     public RestPost(ResponseDelegate delegate) {
  13.         super(delegate);
  14.     }
  15.  
  16.  
  17.     @Override
  18.     protected ResponseResult doInBackground(String... params) {
  19.         String endpoint = params[0];
  20.         URL endpointURL = null;
  21.         try {
  22.             endpointURL = new URL(ServerUrl + endpoint);
  23.             HttpURLConnection myConnection = createConnection(endpointURL);
  24.             myConnection.setDoInput(true);
  25.             myConnection.setDoOutput(true);
  26.             myConnection.setRequestMethod("POST");
  27.  
  28.             processOutput(myConnection, params);
  29.  
  30.             return processInput(myConnection);
  31.  
  32.         } catch (MalformedURLException e) {
  33.             e.printStackTrace();
  34.         } catch (IOException e) {
  35.             e.printStackTrace();
  36.         }
  37.  
  38.         return new ResponseResult(HttpURLConnection.HTTP_CLIENT_TIMEOUT);
  39.     }
  40.  
  41.     @Override
  42.     protected String resolveParams(String[] params) {
  43.         return params[1];
  44.     }
  45. }
  46.  
  47.  
  48. package com.example.myapplication.restcommunication;
  49.  
  50. import android.os.AsyncTask;
  51. import android.util.Log;
  52.  
  53. import com.example.myapplication.model.ResponseResult;
  54.  
  55. import java.io.BufferedReader;
  56. import java.io.BufferedWriter;
  57. import java.io.IOException;
  58. import java.io.InputStreamReader;
  59. import java.io.OutputStream;
  60. import java.io.OutputStreamWriter;
  61. import java.net.HttpURLConnection;
  62. import java.net.URL;
  63. import java.util.ArrayList;
  64. import java.util.List;
  65.  
  66. public abstract class RestParent extends AsyncTask<String, String, ResponseResult> {
  67.  
  68. //    protected static final String ServerUrl = "http://158.195.218.38:8080";
  69.     protected static final String ServerUrl = "http://147.175.160.46:8080";
  70.     protected ResponseDelegate delegate;
  71.  
  72.     public RestParent(ResponseDelegate delegate) {
  73.         this.delegate = delegate;
  74.     }
  75.  
  76.     protected HttpURLConnection createConnection(URL endpoint) throws IOException {
  77.         HttpURLConnection myConnection =
  78.                 (HttpURLConnection) endpoint.openConnection();
  79.         myConnection.setReadTimeout(5000);
  80.         myConnection.setConnectTimeout(5000);
  81.         myConnection.setRequestProperty("Content-Type", "application/json");
  82.         myConnection.setRequestProperty("Accept", "application/json, text/plain, */*");
  83.  
  84.         return myConnection;
  85.     }
  86.  
  87.     protected ResponseResult processInput(HttpURLConnection myConnection) throws IOException {
  88.  
  89.         try {
  90.             Thread.sleep(3000);
  91.         } catch (InterruptedException e) {
  92.             e.printStackTrace();
  93.         }
  94.         if (myConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
  95.             // Success
  96.             // Further processing here
  97.             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(myConnection.getInputStream()));
  98.             StringBuilder response = new StringBuilder();
  99.             String line;
  100.             while ((line = bufferedReader.readLine()) != null) {
  101.                 response.append(line);
  102.             }
  103.             return new ResponseResult(myConnection.getResponseCode(), response.toString());
  104.         } else {
  105.             // Error handling code goes here
  106.             return new ResponseResult(myConnection.getResponseCode());
  107.         }
  108.     }
  109.  
  110.     protected void processOutput(HttpURLConnection myConnection, String[] params) throws IOException {
  111.         OutputStream os = null;
  112.         os = myConnection.getOutputStream();
  113.         BufferedWriter writer = new BufferedWriter(
  114.                 new OutputStreamWriter(os, "UTF-8"));
  115.         writer.write(resolveParams(params));
  116.         writer.flush();
  117.     }
  118.  
  119.     protected String resolveParams(String[] params) {
  120.         StringBuilder result;
  121.         if (params == null || params.length == 1) {
  122.             return "";
  123.         }
  124.         result = new StringBuilder();
  125.  
  126.         for (int i = 1; i < params.length; i += 2) {
  127.             if (i != 1) {
  128.                 result.append("&");
  129.             }
  130.             result.append(params[i]).append("=").append(params[i + 1]);
  131.  
  132.         }
  133.         Log.d("endpoint", "result = " + result);
  134.         return result.toString();
  135.     }
  136.  
  137.     @Override
  138.     protected void onPostExecute(ResponseResult responseResult) {
  139.         super.onPostExecute(responseResult);
  140.         delegate.processResponse(responseResult);
  141.     }
  142.  
  143. }
  144.  
  145.  
  146. package com.example.myapplication.restcommunication;
  147.  
  148. import com.example.myapplication.model.ResponseResult;
  149.  
  150. public interface ResponseDelegate {
  151.     public void processResponse(ResponseResult result);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement