0% found this document useful (0 votes)
57 views

Status Resultserver Mstring: "Api Manager"

This document defines an API Manager class that handles communication with a web service API. The class includes methods for registering and updating user profiles, saving bookmarks, and getting profile and bookmark data. It uses Retrofit to interface with the API and handles both synchronous and asynchronous requests.

Uploaded by

hashim
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Status Resultserver Mstring: "Api Manager"

This document defines an API Manager class that handles communication with a web service API. The class includes methods for registering and updating user profiles, saving bookmarks, and getting profile and bookmark data. It uses Retrofit to interface with the API and handles both synchronous and asynchronous requests.

Uploaded by

hashim
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 10

package com.you.toyoubible.

api;

import android.util.Log;

import com.you.toyoubible.constant.Constants;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import retrofit.converter.GsonConverter;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class API_Manager {

private static final String TAG = "API manager";


private static API_Manager mInstance = null;
public boolean status;
public String resultServer;
private String mString;

private API_Manager() {
mString = "Hello";
}
public static API_Manager getInstance() {
if (mInstance == null) {
mInstance = new API_Manager();
}
return mInstance;
}

public String getString() {


return this.mString;
}

public void setString(String value) {


mString = value;
}

public String getHttpPost(String url, List<NameValuePair>


params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);

try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
System.err.println("Value of StatusCode is:" +
statusCode);
if (statusCode == 200) { // Status OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new
InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download result..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}

public Retrofit getRetrofitInstance(){


Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.webServiceBaseURL1)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}

// User Registration
public User register(String userName, String lastName,
String gender) {

Retrofit retrofit = new Retrofit.Builder()


.baseUrl(Constants.webServiceBaseURL1)
.addConverterFactory(GsonConverterFactory.create())
.build();

ApiInterface apiInterface =
retrofit.create(ApiInterface.class);
try{
Response<User> response =
apiInterface.register(userName,lastName,gender,
"","", "").execute();
return response.body();
}catch (Exception ex){
ex.printStackTrace();
return null;
}

/*String url = Constants.webServiceBaseURL +


Constants.saveUserURL;
resultServer = "";
List<NameValuePair> userDetailsList = new
ArrayList<NameValuePair>();
userDetailsList.add(new BasicNameValuePair("username",
userName));
userDetailsList.add(new BasicNameValuePair("lname",
lastName));
userDetailsList.add(new BasicNameValuePair("gender",
gender));
userDetailsList.add(new BasicNameValuePair("email", ""));
userDetailsList.add(new BasicNameValuePair("age", ""));
userDetailsList.add(new BasicNameValuePair("spouse",
""));

System.out.println("user details: " + userDetailsList);

try {
resultServer = getHttpPost(url, userDetailsList);
Log.d(TAG, "Registration Result: " + resultServer);
} catch (NullPointerException ne) {
ne.getMessage();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("SHIT", resultServer);
return resultServer;*/
}

//Settings--->update user
public UpdateUser updateUser(String userId, String
userName, String lastName, String age, String email, String
isPersonalised, String spouse,
String gender, String isDatabase) {

Retrofit retrofit = new Retrofit.Builder()


.baseUrl(Constants.webServiceBaseURL1)
.addConverterFactory(GsonConverterFactory.create())
.build();

ApiInterface apiInterface =
retrofit.create(ApiInterface.class);
try{
Response<UpdateUser> response =
apiInterface.updateUser(userId,userName,lastName,age,email,i
sPersonalised,spouse,gender,isDatabase).execute();
return response.body();
}catch (Exception ex){
ex.printStackTrace();
return null;
}

/*

String url = Constants.webServiceBaseURL +


Constants.updateUserUrl;
resultServer = "";
List<NameValuePair> userDetailsList = new
ArrayList<NameValuePair>();
userDetailsList.add(new BasicNameValuePair("user_id",
userId));
userDetailsList.add(new BasicNameValuePair("name",
userName));
userDetailsList.add(new BasicNameValuePair("lname",
lastName));
userDetailsList.add(new BasicNameValuePair("age", age));
userDetailsList.add(new BasicNameValuePair("email",
email));
userDetailsList.add(new
BasicNameValuePair("isPersonalised", isPersonalised));
userDetailsList.add(new BasicNameValuePair("spouse",
spouse));
userDetailsList.add(new BasicNameValuePair("gender",
gender));
userDetailsList.add(new
BasicNameValuePair("isdatabase", isDatabase));

System.out.println("user details update user: " +


userDetailsList);

try {
resultServer = getHttpPost(url, userDetailsList);
Log.d(TAG, "Update User Result: " + resultServer);
} catch (NullPointerException ne) {
ne.getMessage();
} catch (Exception e) {
e.printStackTrace();
}
return resultServer; */
}
// Get user profile
public String getProfile(String userId) {

/* Retrofit retrofit = new Retrofit.Builder()


.baseUrl(Constants.webServiceBaseURL1)

.addConverterFactory(GsonConverterFactory.create())
.build();

ApiInterface apiInterface =
retrofit.create(ApiInterface.class);
try{
Response<UpdateUser> response =
apiInterface.updateUser(userId,userName,lastName,age,email,i
sPersonalised,spouse,gender,isDatabase).execute();
return response.body();
}catch (Exception ex){
ex.printStackTrace();
return null;
}

*/
//Constants.getProfileUrl = Constants.getProfileUrl+userId;

String url = Constants.webServiceBaseURL +


Constants.getProfileUrl+userId;
resultServer = "";
List<NameValuePair> userDetailsList = new
ArrayList<NameValuePair>();

try {
resultServer = getHttpPost(url, userDetailsList);
Log.d(TAG, "Get Profile Result: " + resultServer);
} catch (NullPointerException ne) {
ne.getMessage();
} catch (Exception e) {
e.printStackTrace();
}
return resultServer;
}

/*
// Save Bookmark
NSDictionary *parameters =
@{@"user_id":userIdStr,@"page_id":page_id,
@"ispagesave":ispagesave,@"isBookmark":isBookmark
,@"countnumber":countnumber,@"bookid":bookidStr
,@"chapterid":chapteridStr,@"pagename":pageName};
*/

//Settings--->update user
public SaveBookmark saveBookmark(String userId, String
pageId, String isBookmark, String bookId, String chapterId,
String pageName) {

Retrofit retrofit = new Retrofit.Builder()


.baseUrl(Constants.webServiceBaseURL1)
.addConverterFactory(GsonConverterFactory.create())
.build();

ApiInterface apiInterface =
retrofit.create(ApiInterface.class);
try{
Response<SaveBookmark> response =
apiInterface.saveBookmark(userId,pageId,"",isBookmark,"",boo
kId,chapterId,pageName).execute();
return response.body();
}catch (Exception ex){
ex.printStackTrace();
return null;
}

// String url = Constants.webServiceBaseURL +


Constants.saveBookmarkUrl;
// resultServer = "";
// List<NameValuePair> verseDetailsList = new
ArrayList<NameValuePair>();
// verseDetailsList.add(new BasicNameValuePair("user_id",
userId));
// verseDetailsList.add(new BasicNameValuePair("page_id",
pageId));
// verseDetailsList.add(new
BasicNameValuePair("ispagesave", ""));
// verseDetailsList.add(new
BasicNameValuePair("isBookmark", isBookmark));
// verseDetailsList.add(new
BasicNameValuePair("countnumber", ""));
// verseDetailsList.add(new BasicNameValuePair("bookid",
bookId));
// verseDetailsList.add(new
BasicNameValuePair("chapterid", chapterId));
// verseDetailsList.add(new
BasicNameValuePair("pagename", pageName));
//
// System.out.println("bookmark details: " +
verseDetailsList);
//
// try {
// resultServer = getHttpPost(url, verseDetailsList);
// Log.d(TAG, "Save bookmark Result: " + resultServer);
// } catch (NullPointerException ne) {
// ne.getMessage();
// } catch (Exception e) {
// e.printStackTrace();
// }
// return resultServer;
}

public String getBookmark(String userId) {


String url = Constants.webServiceBaseURL +
Constants.getBookmarkUrl+userId;
resultServer = "";
List<NameValuePair> bookmarkList = new
ArrayList<NameValuePair>();

try {
resultServer = getHttpPost(url, bookmarkList);
Log.d(TAG, "Get Bookmark Result: " +
resultServer);
} catch (NullPointerException ne) {
ne.getMessage();
} catch (Exception e) {
e.printStackTrace();
}
return resultServer;
}

public String deleteBookmark(String userId, String pageId,


String isDeleteAll) {
String url = Constants.webServiceBaseURL +
Constants.deleteBookmarkUrl;
resultServer = "";
List<NameValuePair> bookmarkList = new
ArrayList<NameValuePair>();
bookmarkList.add(new BasicNameValuePair("user_id",
userId));
bookmarkList.add(new BasicNameValuePair("page_id",
pageId));
bookmarkList.add(new
BasicNameValuePair("isDeleteAll", isDeleteAll));

try {
resultServer = getHttpPost(url, bookmarkList);
Log.d(TAG, "Delete Bookmark Result: " +
resultServer);
} catch (NullPointerException ne) {
ne.getMessage();
} catch (Exception e) {
e.printStackTrace();
}
return resultServer;
}

//Settings--->update user
public String savePurchase(String userId, String
isPurchased) {

String url = Constants.webServiceBaseURL +


Constants.inAppSaveUrl;
resultServer = "";
List<NameValuePair> purchaseDetails = new
ArrayList<NameValuePair>();
purchaseDetails.add(new BasicNameValuePair("user_id",
userId));
purchaseDetails.add(new
BasicNameValuePair("isPerchased", isPurchased));

System.out.println("Save purchase details : " +


purchaseDetails);

try {
resultServer = getHttpPost(url, purchaseDetails);
Log.d(TAG, "Save purchase Result: " +
resultServer);
} catch (NullPointerException ne) {
ne.getMessage();
} catch (Exception e) {
e.printStackTrace();
}
return resultServer;
}

You might also like