Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication.restcommunication;
- import com.example.myapplication.model.ResponseResult;
- import java.io.IOException;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- public class RestPost extends RestParent {
- public RestPost(ResponseDelegate delegate) {
- super(delegate);
- }
- @Override
- protected ResponseResult doInBackground(String... params) {
- String endpoint = params[0];
- URL endpointURL = null;
- try {
- endpointURL = new URL(ServerUrl + endpoint);
- HttpURLConnection myConnection = createConnection(endpointURL);
- myConnection.setDoInput(true);
- myConnection.setDoOutput(true);
- myConnection.setRequestMethod("POST");
- processOutput(myConnection, params);
- return processInput(myConnection);
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return new ResponseResult(HttpURLConnection.HTTP_CLIENT_TIMEOUT);
- }
- @Override
- protected String resolveParams(String[] params) {
- return params[1];
- }
- }
- package com.example.myapplication.restcommunication;
- import android.os.AsyncTask;
- import android.util.Log;
- import com.example.myapplication.model.ResponseResult;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.List;
- public abstract class RestParent extends AsyncTask<String, String, ResponseResult> {
- // protected static final String ServerUrl = "http://158.195.218.38:8080";
- protected static final String ServerUrl = "http://147.175.160.46:8080";
- protected ResponseDelegate delegate;
- public RestParent(ResponseDelegate delegate) {
- this.delegate = delegate;
- }
- protected HttpURLConnection createConnection(URL endpoint) throws IOException {
- HttpURLConnection myConnection =
- (HttpURLConnection) endpoint.openConnection();
- myConnection.setReadTimeout(5000);
- myConnection.setConnectTimeout(5000);
- myConnection.setRequestProperty("Content-Type", "application/json");
- myConnection.setRequestProperty("Accept", "application/json, text/plain, */*");
- return myConnection;
- }
- protected ResponseResult processInput(HttpURLConnection myConnection) throws IOException {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- if (myConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
- // Success
- // Further processing here
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(myConnection.getInputStream()));
- StringBuilder response = new StringBuilder();
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- response.append(line);
- }
- return new ResponseResult(myConnection.getResponseCode(), response.toString());
- } else {
- // Error handling code goes here
- return new ResponseResult(myConnection.getResponseCode());
- }
- }
- protected void processOutput(HttpURLConnection myConnection, String[] params) throws IOException {
- OutputStream os = null;
- os = myConnection.getOutputStream();
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(os, "UTF-8"));
- writer.write(resolveParams(params));
- writer.flush();
- }
- protected String resolveParams(String[] params) {
- StringBuilder result;
- if (params == null || params.length == 1) {
- return "";
- }
- result = new StringBuilder();
- for (int i = 1; i < params.length; i += 2) {
- if (i != 1) {
- result.append("&");
- }
- result.append(params[i]).append("=").append(params[i + 1]);
- }
- Log.d("endpoint", "result = " + result);
- return result.toString();
- }
- @Override
- protected void onPostExecute(ResponseResult responseResult) {
- super.onPostExecute(responseResult);
- delegate.processResponse(responseResult);
- }
- }
- package com.example.myapplication.restcommunication;
- import com.example.myapplication.model.ResponseResult;
- public interface ResponseDelegate {
- public void processResponse(ResponseResult result);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement