Created
September 26, 2016 06:26
-
-
Save JaydipZala/712f71a13a51a048f245ebe531218d6c to your computer and use it in GitHub Desktop.
Network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Soap { | |
private static String BASE_URL_API = General.BASE_URL + "api/"; | |
private static final String CHARSET = "UTF-8"; | |
private static final String RESULT_OK = "200"; | |
private static final String TAG = Soap.class.getSimpleName(); | |
public static String getSoapResponsePost(String requestURL, Uri.Builder postDataParams) { | |
URL url; | |
String response = ""; | |
try { | |
url = new URL(BASE_URL_API + requestURL); | |
Log.d(TAG, postDataParams.toString()); | |
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); | |
httpURLConnection.setDoInput(true); | |
httpURLConnection.setDoOutput(true); | |
httpURLConnection.setReadTimeout(15000); | |
httpURLConnection.setRequestMethod("POST"); | |
httpURLConnection.setConnectTimeout(15000); | |
// uri builder | |
String query = postDataParams.build().getEncodedQuery(); | |
OutputStream os = httpURLConnection.getOutputStream(); | |
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, CHARSET)); | |
writer.write(query); | |
writer.flush(); | |
writer.close(); | |
httpURLConnection.connect(); | |
int resCode = httpURLConnection.getResponseCode(); | |
if (resCode == HttpsURLConnection.HTTP_OK) { | |
String line; | |
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); | |
while ((line = br.readLine()) != null) { | |
response = response + line; | |
} | |
} else { | |
response = ""; | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
Log.e(TAG, response); | |
return response; | |
} | |
public static String getSoapResponseByPostFile(String postFixOfUrl, | |
ArrayList<NameValuePair> nameValuePairs, | |
ArrayList<NameValuePair> nameValuePairsFile) | |
throws ClientProtocolException, IOException { | |
HttpClient httpclient = new DefaultHttpClient(); | |
try { | |
HttpPost httppost = new HttpPost(BaseURL + postFixOfUrl); | |
Log.d("SOAP", "URI:" + BaseURL + postFixOfUrl); | |
// httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, | |
// "UTF-8")); | |
MultipartEntity entity = new MultipartEntity(); | |
for (int index = 0; index < nameValuePairsFile.size(); index++) { | |
String filePath = nameValuePairsFile.get(index).getValue(); | |
if (filePath != null && filePath != "") { | |
File myFile = new File(filePath); | |
FileBody fileBody = new FileBody(myFile); | |
entity.addPart(nameValuePairsFile.get(index).getName(), | |
fileBody); | |
} | |
} | |
for (int index = 0; index < nameValuePairs.size(); index++) { | |
entity.addPart(nameValuePairs.get(index).getName(), | |
new StringBody(nameValuePairs.get(index).getValue(), | |
Charset.forName("UTF-8"))); | |
} | |
httppost.setEntity(entity); | |
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient | |
.execute(httppost); | |
HttpEntity r_entity = httpResponse.getEntity(); | |
String xmlString = EntityUtils.toString(r_entity); | |
Log.d("SOAP", "Result:" + xmlString.toString()); | |
return xmlString.toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
httpclient.getConnectionManager().shutdown(); | |
} | |
return null; | |
} | |
public static ParsedResponse apiLogin(String email, String password, String deviceId) | |
throws IOException, JSONException { | |
ParsedResponse p = new ParsedResponse(); | |
if (Utils.isNetworkAvailable(General.appContext)) { | |
Uri.Builder builder = new Uri.Builder() | |
.appendQueryParameter("device", "1") | |
.appendQueryParameter("deviceId", deviceId) | |
.appendQueryParameter("email", email) | |
.appendQueryParameter("password", password); | |
String res = getSoapResponsePost("user/login/", builder); | |
if (!TextUtils.isEmpty(res)) { | |
JSONObject objRes = new JSONObject(res); | |
if (objRes.getString("statuscode").equals(RESULT_OK)) { | |
JSONObject objData = objRes.getJSONObject("data"); | |
Gson gson = new Gson(); | |
Login login = gson.fromJson(objData.toString(), Login.class); | |
p.error = false; | |
p.o = login; | |
} else { | |
p.error = true; | |
p.o = objRes.getString("message"); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_prblm_loading_data); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_no_internet); | |
} | |
return p; | |
} | |
public static ParsedResponse apiNewRequestTask(String user_id, String message) | |
throws IOException, JSONException { | |
ParsedResponse p = new ParsedResponse(); | |
if (Utils.isNetworkAvailable(General.appContext)) { | |
Uri.Builder builder = new Uri.Builder() | |
.appendQueryParameter("user_id", user_id) | |
.appendQueryParameter("message", message); | |
String res = getSoapResponsePost("user/addpatientrequest/", builder); | |
if (!TextUtils.isEmpty(res)) { | |
JSONObject objRes = new JSONObject(res); | |
if (objRes.getString("statuscode").equals(RESULT_OK)) { | |
p.error = false; | |
p.o = objRes.getString("message"); | |
} else { | |
p.error = true; | |
p.o = objRes.getString("message"); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_prblm_loading_data); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_no_internet); | |
} | |
return p; | |
} | |
public static ParsedResponse apiWaitingRequest(String user_id, String type) | |
throws IOException, JSONException { | |
ParsedResponse p = new ParsedResponse(); | |
if (Utils.isNetworkAvailable(General.appContext)) { | |
Uri.Builder builder = new Uri.Builder() | |
.appendQueryParameter("user_id", user_id) | |
.appendQueryParameter("type", type); | |
String res = getSoapResponsePost("user/getpatientrequest/", builder); | |
if (!TextUtils.isEmpty(res)) { | |
JSONObject objRes = new JSONObject(res); | |
if (objRes.getString("statuscode").equals(RESULT_OK)) { | |
JSONArray arrData = objRes.getJSONArray("data"); | |
ArrayList<Request> arrRequest = new ArrayList<>(); | |
Gson gson = new Gson(); | |
for (int i = 0; i < arrData.length(); i++) { | |
JSONObject c = arrData.getJSONObject(i); | |
Request request = new Request(); | |
request = gson.fromJson(c.toString(), Request.class); | |
JSONArray arrJSONComments = c.getJSONArray("commentData"); | |
ArrayList<Comment> arrComments = new ArrayList<>(); | |
for (int j = 0; j < arrJSONComments.length(); j++) { | |
JSONObject cmnt = arrJSONComments.getJSONObject(j); | |
Comment comment = new Comment(); | |
comment = gson.fromJson(cmnt.toString(), Comment.class); | |
arrComments.add(comment); | |
} | |
request.arrComments = arrComments; | |
arrRequest.add(request); | |
} | |
p.error = false; | |
p.o = arrRequest; | |
} else { | |
p.error = true; | |
p.o = objRes.getString("message"); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_prblm_loading_data); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_no_internet); | |
} | |
return p; | |
} | |
public static ParsedResponse apiSendComment(String user_id, String request_id, String comment) | |
throws IOException, JSONException { | |
ParsedResponse p = new ParsedResponse(); | |
if (Utils.isNetworkAvailable(General.appContext)) { | |
Uri.Builder builder = new Uri.Builder() | |
.appendQueryParameter("user_id", user_id) | |
.appendQueryParameter("request_id", request_id) | |
.appendQueryParameter("comment", comment); | |
String res = getSoapResponsePost("user/addpatientrequestcomment/", builder); | |
if (!TextUtils.isEmpty(res)) { | |
JSONObject objRes = new JSONObject(res); | |
if (objRes.getString("statuscode").equals(RESULT_OK)) { | |
p.error = false; | |
p.o = objRes.getString("message"); | |
} else { | |
p.error = true; | |
p.o = objRes.getString("message"); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_prblm_loading_data); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_no_internet); | |
} | |
return p; | |
} | |
public static ParsedResponse apiGetRequest(String request_id) throws JSONException { | |
ParsedResponse p = new ParsedResponse(); | |
if (Utils.isNetworkAvailable(General.appContext)) { | |
Uri.Builder builder = new Uri.Builder() | |
.appendQueryParameter("request_id", request_id); | |
String res = getSoapResponsePost("user/getcommentByreqId/", builder); | |
if (!TextUtils.isEmpty(res)) { | |
JSONObject objRes = new JSONObject(res); | |
if (objRes.getString("statuscode").equals(RESULT_OK)) { | |
JSONArray arrData = objRes.getJSONArray("data"); | |
Gson gson = new Gson(); | |
Type listType = new TypeToken<List<Comment>>() { | |
}.getType(); | |
List<Comment> posts = gson.fromJson(arrData.toString(), listType); | |
p.error = false; | |
p.o = posts; | |
} else { | |
p.error = true; | |
p.o = objRes.getString("message"); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_prblm_loading_data); | |
} | |
} else { | |
p.error = true; | |
p.o = General.appContext.getString(R.string.err_no_internet); | |
} | |
return p; | |
} | |
public static String apiRegistration(String user_id, String first_name, | |
String last_name, String user_image, String email, String password, | |
String middle_name, String telephone_no, String country, | |
String device_type, String device_id, String login_type, | |
String fb_id, String twitter_id, String googleplus_id) | |
throws Exception { | |
ArrayList<NameValuePair> alNameValuePairs = new ArrayList<NameValuePair>(); | |
ArrayList<NameValuePair> alNameValuePairsFile = new ArrayList<NameValuePair>(); | |
NameValuePair nameValuePairs = new BasicNameValuePair("user_id", | |
user_id); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("first_name", first_name); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("last_name", last_name); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("user_image", user_image); | |
alNameValuePairsFile.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("email", email); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("password", password); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("middle_name", middle_name); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("telephone_no", telephone_no); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("country", country); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("device_type", device_type); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("device_id", device_id); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("login_type", login_type); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("fb_id", fb_id); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("twitter_id", twitter_id); | |
alNameValuePairs.add(nameValuePairs); | |
nameValuePairs = new BasicNameValuePair("googleplus_id", googleplus_id); | |
alNameValuePairs.add(nameValuePairs); | |
String JsonString = getSoapResponseByPostFile("user/registration", | |
alNameValuePairs, alNameValuePairsFile); | |
Log.d("SOAP", "Result : " + JsonString); | |
return JsonString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment