218 lines
8.2 KiB
Java
218 lines
8.2 KiB
Java
package monnethic.mobile.restApi;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.DataOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.util.Map;
|
|
|
|
import monnethic.mobile.database.User;
|
|
|
|
public class HttpCallHandler {
|
|
|
|
public String getHttp(URL url) throws Exception {
|
|
InputStream inputStream;
|
|
BufferedReader bufferedReader = null;
|
|
HttpURLConnection urlConnection = null;
|
|
|
|
urlConnection = (HttpURLConnection) url.openConnection();
|
|
urlConnection.setRequestMethod("GET");
|
|
urlConnection.connect();
|
|
|
|
int responseCode = urlConnection.getResponseCode();
|
|
if(responseCode != 200){
|
|
throw new Exception("ERROR != 200");
|
|
}
|
|
inputStream = urlConnection.getInputStream();
|
|
if(inputStream == null){
|
|
throw new Exception("ERROR inputStream");
|
|
}
|
|
|
|
bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
String line;
|
|
|
|
while ((line = bufferedReader.readLine())!=null){
|
|
stringBuilder.append(line);
|
|
}
|
|
|
|
return stringBuilder.toString();
|
|
}
|
|
|
|
|
|
public String postHttp(URL url, Map<String,String> params) throws Exception {
|
|
String returnValue=null;
|
|
HttpURLConnection urlConnection = null;
|
|
urlConnection = (HttpURLConnection) url.openConnection();
|
|
urlConnection.setRequestMethod("POST");
|
|
urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
urlConnection.setRequestProperty("Accept","application/json");
|
|
urlConnection.setDoOutput(true);
|
|
urlConnection.setDoInput(true);
|
|
|
|
JSONObject jsonParam = new JSONObject();
|
|
for(Map.Entry<String,String> pair : params.entrySet()){
|
|
jsonParam.put(pair.getKey(),pair.getValue());
|
|
}
|
|
|
|
DataOutputStream os = new DataOutputStream(urlConnection.getOutputStream());
|
|
os.writeBytes(jsonParam.toString());
|
|
|
|
int statusCode = urlConnection.getResponseCode();
|
|
if(statusCode != 200){
|
|
JSONObject jsonReturn = new JSONObject();
|
|
if(statusCode == 404){
|
|
jsonReturn.put("status",404);
|
|
jsonReturn.put("response","Not Found");
|
|
returnValue = jsonReturn.toString();
|
|
}else if(statusCode == 403){
|
|
jsonReturn.put("status",403);
|
|
jsonReturn.put("response","Forbidden");
|
|
returnValue = jsonReturn.toString();
|
|
}
|
|
} else {
|
|
BufferedReader bufferedReader = null;
|
|
bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
String line;
|
|
while ((line = bufferedReader.readLine())!=null){
|
|
stringBuilder.append(line);
|
|
}
|
|
returnValue = stringBuilder.toString();
|
|
}
|
|
return returnValue;
|
|
}
|
|
|
|
|
|
public String executeLoginHttp(String urlParam, String[] params){
|
|
BufferedReader bufferedReader = null;
|
|
HttpURLConnection connection = null;
|
|
String res = null;
|
|
try{
|
|
URL url = new URL(urlParam);
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("POST");
|
|
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
connection.setRequestProperty("Accept","application/json");
|
|
connection.setDoOutput(true);
|
|
connection.setDoInput(true);
|
|
JSONObject jsonParam = new JSONObject();
|
|
jsonParam.put("email",params[0]);
|
|
jsonParam.put("password",params[1]);
|
|
DataOutputStream os = new DataOutputStream(connection.getOutputStream());
|
|
os.writeBytes(jsonParam.toString());
|
|
|
|
int statusCode = connection.getResponseCode();
|
|
if(statusCode != 200){
|
|
JSONObject jsonReturn = new JSONObject();
|
|
System.out.println("Response code is : "+statusCode);
|
|
if(statusCode == 404){
|
|
jsonReturn.put("status",404);
|
|
jsonReturn.put("response","Not Found");
|
|
return jsonReturn.toString();
|
|
}else if(statusCode == 403){
|
|
jsonReturn.put("status",403);
|
|
jsonReturn.put("response","Wrong Password");
|
|
return jsonReturn.toString();
|
|
}
|
|
}else{
|
|
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
String line;
|
|
while ((line = bufferedReader.readLine())!=null){
|
|
stringBuilder.append(line);
|
|
}
|
|
JSONObject jsonSuccess = new JSONObject(stringBuilder.toString());
|
|
jsonSuccess.put("status",200);
|
|
res = jsonSuccess.toString();
|
|
os.flush();
|
|
os.close();
|
|
}
|
|
return res;
|
|
}catch (Exception e){
|
|
e.printStackTrace();
|
|
return null;
|
|
} finally {
|
|
if(connection != null){
|
|
connection.disconnect();
|
|
}
|
|
if(bufferedReader != null){
|
|
try {
|
|
bufferedReader.close();
|
|
}catch (IOException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public String executeRegisterHttp(String urlParam, User user){
|
|
BufferedReader bufferedReader = null;
|
|
HttpURLConnection connection = null;
|
|
String res = null;
|
|
try{
|
|
URL url = new URL(urlParam);
|
|
connection = (HttpURLConnection) url.openConnection();
|
|
connection.setRequestMethod("POST");
|
|
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
connection.setRequestProperty("Accept","application/json");
|
|
connection.setDoOutput(true);
|
|
connection.setDoInput(true);
|
|
JSONObject jsonParam = new JSONObject();
|
|
jsonParam.put("name",user.getName());
|
|
jsonParam.put("firstname",user.getFirstname());
|
|
jsonParam.put("email",user.getEmail());
|
|
jsonParam.put("password",user.getPassword());
|
|
|
|
DataOutputStream os = new DataOutputStream(connection.getOutputStream());
|
|
os.writeBytes(jsonParam.toString());
|
|
|
|
int statusCode = connection.getResponseCode();
|
|
if(statusCode!=200){
|
|
JSONObject jsonReturn = new JSONObject();
|
|
System.out.println("Response code is : "+statusCode);
|
|
if(statusCode == 302){
|
|
jsonReturn.put("status",302);
|
|
jsonReturn.put("response","User Already Exist !");
|
|
return jsonReturn.toString();
|
|
}else {
|
|
return null;
|
|
}
|
|
}else {
|
|
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
String line;
|
|
while ((line = bufferedReader.readLine())!=null){
|
|
stringBuilder.append(line);
|
|
}
|
|
JSONObject jsonSuccess = new JSONObject(stringBuilder.toString());
|
|
jsonSuccess.put("status",200);
|
|
res = jsonSuccess.toString();
|
|
os.flush();
|
|
os.close();
|
|
}
|
|
return res;
|
|
}catch (Exception e){
|
|
e.printStackTrace();
|
|
return null;
|
|
} finally {
|
|
if(connection != null){
|
|
connection.disconnect();
|
|
}
|
|
if(bufferedReader != null){
|
|
try {
|
|
bufferedReader.close();
|
|
}catch (IOException e){
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|