Skip to content

Commit

Permalink
add httpStatus support.
Browse files Browse the repository at this point in the history
  • Loading branch information
NeelPatel21 committed Sep 16, 2017
1 parent ba35fc2 commit 3ac6b62
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.util.Log;

import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -81,6 +82,7 @@ public class AddRequestTask<T,R> extends AsyncTask<String,Void,T> {
private Map<String,String> headers=new HashMap<>();
private HttpMethod meth;
private R requestObj;
private HttpStatus httpStatus;

public AddRequestTask(Class<T> type, R requestObj, HttpMethod meth,
Entry<String,String> ... header) {
Expand All @@ -99,7 +101,10 @@ public AddRequestTask(Class<T> type, R requestObj, Entry<String,String> ... head
@Override
protected T doInBackground(String... params) {
try {
return new RequestHandler().getResource(type, params[0], meth, requestObj, headers);
RequestHandler rh = new RequestHandler();
T obj=rh.getResource(type, params[0], meth, requestObj, headers);
this.httpStatus = rh.getHttpStatus();
return obj;
}catch(Exception ex){
// System.err.println("do in back");
// ex.printStackTrace();
Expand All @@ -126,4 +131,9 @@ public T getObj(){
}
return null;
}

public HttpStatus getHttpStatus() {
return httpStatus;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
Expand Down Expand Up @@ -57,6 +59,7 @@ methods of this class as they contains network access permission which is not

public class RequestHandler{

private HttpStatus httpStatus;
/**
* this method will make http request using parameters provided.
* request object will be parsed to json format & Http response will be parsed
Expand Down Expand Up @@ -91,8 +94,9 @@ public <T,R> T getResource(Class<T> type, String url, HttpMethod meth,R requestO


// Log.i("test2",request.toString());
HttpEntity<T> response;
ResponseEntity<T> response;
response = restTemplate.exchange(url,meth,request,type);
this.httpStatus = response.getStatusCode();
return response.hasBody()?response.getBody():null;
// return restTemplate.getForObject(url,type,header);
} catch (Exception e) {
Expand Down Expand Up @@ -152,4 +156,8 @@ public <T> T getResource(Class<T> type, String url){
return getResource(type, url, HttpMethod.GET);
}

public HttpStatus getHttpStatus() {
return httpStatus;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.util.Log;

import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -86,6 +87,7 @@ public class RequestTask<T> extends AsyncTask<String,Void,T> {
private Class<T> type;
private Map<String,String> headers=new HashMap<>();
private HttpMethod meth;
private HttpStatus httpStatus;

/**make a object with specified parameters.
* @param type class object of expected return type.
Expand All @@ -111,7 +113,10 @@ public RequestTask(Class<T> type, Entry<String,String> ... header) {
@Override
protected T doInBackground(String... params) {
try {
return new RequestHandler().getResource(type, params[0], meth, headers);
RequestHandler rh = new RequestHandler();
T obj = rh.getResource(type, params[0], meth, headers);
this.httpStatus=rh.getHttpStatus();
return obj;
}catch(Exception ex){
// System.err.println("do in back");
// ex.printStackTrace();
Expand Down Expand Up @@ -144,4 +149,9 @@ public T getObj(){
}
return null;
}

public HttpStatus getHttpStatus() {
return httpStatus;
}

}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.android.tools.build:gradle:2.3.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 3ac6b62

Please sign in to comment.