Can't send data via POST using Retrofit? - android

I am facing a problem,first of all I am new to android, and I am having a Post request, I am sending an object in body :
public class SingleContractRequest {
#SerializedName("userRole")
private String userRole;
#SerializedName("contratId")
private String contratId;
public SingleContractRequest(String userRole, String contractId) {
this.userRole = userRole;
this.contratId = contractId;
}
public String getUserRole() {
return userRole;
}
public void setUserRole(String userRole) {
this.userRole = userRole;
}
public String getContractId() {
return contratId;
}
public void setContractId(String contractId) {
this.contratId = contractId;
}
}
And that is my ContractApi, the method called is the second one :
public interface ContractApi {
#GET("contrats.php")
Single<List<ContractModel>> getContractList();
#POST("contrat.php")
Single<ContractModel> getContract(#Body SingleContractRequest body);
}
And here is my Module :
#Module
public class ApiModule {
public static String BASE_URL = "http://192.168.1.104/newconceptsphp/";
#Provides
public ContractApi provideContractApi(){
Gson gson = new GsonBuilder()
.setLenient()
.create();
return new Retrofit.Builder().baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(ContractApi.class);
}
#Provides
public ContractService provideContractService(){
return ContractService.getInstance();
}
}
And to call the api I have a method in my service :
public Single<ContractModel> getContract(SingleContractRequest request) {
return api.getContract(request);
}
So I could do that in one single method but I am creating many layers for f better architecture.
The error I am getting now and I don't know how to solve it :
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
This is the script I am consuming :
<?php
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header("Access-Control-Allow-Headers: Content-Type, Content-Range, Content-Disposition, Content-Description");
json_decode(file_get_contents("php://input"), true);
$dsn = "mysql:host=localhost;dbname=leranconcepts";
$user = "root";
$passwd = "";
$pdo = new PDO($dsn, $user, $passwd);
if (isset($_POST["userRole"])){
$userRole = $_POST["userRole"];
} else {
$userRole = null;
}
if (isset($_POST["contratId"])){
$contratId = $_POST["contratId"];
} else {
$contratId = null;
}
// managing products
if ($userRole === "VENDEUR"){
$sth = $pdo->prepare("SELECT * FROM contrat WHERE id=?");
} else if($userRole === "COURTIER"){
$sth = $pdo->prepare("SELECT id, imageUrl, courtier FROM contrat WHERE id=?");
}
$sth->execute([$contratId]);
$result = $sth->fetchAll(PDO::FETCH_OBJ);
echo json_encode($result[0]);
?>
I think this is to understand my problem, how to solve it.
Any help would be much appreciated guys.

Related

how to handle null or empty json key value response in retrofit 2 response body?

I am using retrofit 2 in android. My app crashes when one of the value return null key like "TOTAL_EXPENSE_AMOUNT": null. My json is like below:
[{
"YEAR": "2019",
"TOTAL_AMOUNT": "1580292",
"TOTAL_EXPENSE_AMOUNT": "1740857",
"TOTAL_BALANCE": "-160565"
},
{
"YEAR": "2018",
"TOTAL_AMOUNT": "144000",
"TOTAL_EXPENSE_AMOUNT": null,
"TOTAL_BALANCE": null
} ]
1..My retrofit Interface is below:
public interface Api {
#POST("User/yearly_balance_expense_list.php")
Call<List<Balance_Data_Model>> getBalanceList();
}
2. My api service is bellow like :
public class ApiService {
public static final String BASE_URL = IPConfigure.getIP();
Api api = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build().create(Api.class);
public Call<List<Balance_Data_Model>> getBalanceList(){
return api.getBalanceList();
}
}
**3.**My projo file is below :
public class Balance_Data_Model {
#SerializedName("YEAR")
#Expose
public String year = "";
#SerializedName("TOTAL_AMOUNT")
#Expose
public String total_amount = "";
#SerializedName("TOTAL_EXPENSE_AMOUNT")
#Expose
public String total_expense = "";
#SerializedName("TOTAL_BALANCE")
#Expose
public String total_balance = "";
//getter/setter here
}
**4.**I have used below code from activity
ApiService apiService = new ApiService();
Call<List<Balance_Data_Model>> call = apiService.getBalanceList();
call.enqueue(new Callback<List<Balance_Data_Model>>() {
#Override
public void onResponse(Call<List<Balance_Data_Model>> list, Response<List<Balance_Data_Model>> response) {
Log.e("success--->","success");
for(Balance_Data_Model m : response.body()){
Log.e("total_expense--->",m.total_expense);
Log.e("total_balance--->",m.total_balance);
}
}
#Override
public void onFailure(Call<List<Balance_Data_Model>> call, Throwable t) {
Log.e("failure--->",t.toString());
}
});
First loop it ok. when total_balance is null then app crashes. How to handle it?
It explained here:
So the proper way to handle this issue is using if-else.
if(m.total_expense!=null)
Log.e("total_expense--->",m.total_expense);
the Log is not print null value , it gives exception , if you want to print the log then use below
Log.e("total_expense--->",m.total_expense + "");
or you can check m.total_expense!=null then print without "".
Please handle all values from response, It is good.
if(response.body() != null){
for(Balance_Data_Model m : response.body()){
if(m.total_expense != null)
Log.e("total_expense--->",m.total_expense);
if(m.total_balance != null)
Log.e("total_balance--->",m.total_balance);
}
}
you can create a method for check the null value
public String amount (String json){
if(json == null){
return "";
else{
retrun json;
}
use like this `String amount=amount(responce.body().getAmount())`

Retrofit error: DB BEGIN_OBJECT but was STRING (when I changed the server that hold Api)

when I import my DB From a server to a localServer on my PC using XAMPP then I try to fetch data from DB using JSON Api and Retrofit I found that error
java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING
at line 1 column 1 path $
I put the Api.php and DB.php files in C:\xampp\htdocs\file and I fill the DB details from PHPMyAdmin DB Server after I import It to PHPMyAdmin so I think it's alright. I think the error is in Api.php and My app It's Just a login Page in MainActivity.java.
Api.php
<?php
include 'DB.php';
$db = DB::getInstance();
header("Content-Type: application/json;charset=utf-8");
function UserLoginMethod($username , $password , $lat, $long){
global $db;
$sql = "SELECT * FROM users WHERE user_email = ? AND user_password = ?
LIMIT 1";
$user_info = $db->query($sql, [$username, $password]);
CheckIsEmpty($user_info);
//echo $db->getSQL();
echo PrintJSON($user_info[0]);
}
switch (#$_GET['function']) {
case "Login":
UserLoginMethod(#$_GET['user_email'], #$_GET['user_password'], $_GET['user_location_latitude'], $_GET['user_location_longitude']);
break;
}
function CheckIsEmpty($query) {
if (is_null($query)||empty($query)){
die (json_encode(array('error' => 'no items funded.'),JSON_FORCE_OBJECT));
}
}
function PrintJSON($q){
$j = json_encode($q);
if (count($j) > 0 && !is_null($q) )
return $j;
else
die (json_encode(array('error' => 'no items funded.'),JSON_FORCE_OBJECT));
}
function CreateDieError($body){
die (json_encode(array("error" => $body),JSON_FORCE_OBJECT));
}
?>
ApiClient.java To connect to server
public class ApiClient {
public static final String BASE_URL = "http://192.168.1.38/";
public static final String PATH_URL = "/file/";//Path For Api
static Gson gson = new GsonBuilder()
.setLenient()
.create();
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null){
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
ApiInterface.java
public interface ApiInterface {
#Headers("Content-type: application/json")
#GET(ApiClient.PATH_URL+"Api.php")
Call<UserAccount> Login(#Query("function") String function, #Query("user_email") String email, #Query("user_password") String password,
#Query("user_location_latitude") Double Latitude,
#Query("user_location_longitude") Double Longitude);
}
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final Button loginBtn = findViewById(R.id.loginBtn);
TextView signupTxt = findViewById(R.id.txtSignup);
TextView forgetPassTxt = findViewById(R.id.txtForgetPassword);
loginBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class);
EditText emailEditeText = findViewById(R.id.edtEmail);
EditText passEditeText = findViewById(R.id.edtPassword);
UserLogin(apiInterface, emailEditeText, passEditeText);
}
});
private void UserLogin(ApiInterface apiInterface, EditText emailEditeText,
EditText passEditeText){
Call<UserAccount> call = apiInterface.Login("Login", emailEditeText.getText().toString(), Utility.md5(passEditeText.getText().toString()), Utility.myLocation.getLatitude(), Utility.myLocation.getLongitude());
//make a call to server
call.enqueue(new Callback<UserAccount>() {
#Override
public void onResponse(Call<UserAccount> call, Response<UserAccount> response) {
String errorBody = response.errorBody().toString();
Log.d("Message", "code..."+response.code() + " message..." + response.message()+" body..."+errorBody);
boolean check = response.isSuccessful();
Log.i("log12", String.valueOf(check));
String val = response.body().getError();
Log.i("log1",val);
if (val == null) {
prefManager.setLogin(true, response.body());
Toast.makeText(LoginActivity.this, " Login success" + response.body().getUsername(), Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}else {
if (response.body().getError().equals("no items funded.")){
Toast.makeText(LoginActivity.this, "check Email and Password", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(LoginActivity.this, response.body().getError(), Toast.LENGTH_SHORT).show();
Log.e("error", response.body().getError());
}
}
}
#Override
public void onFailure(Call<UserAccount> call, Throwable t) {
Toast.makeText(LoginActivity.this, "Network Error", Toast.LENGTH_SHORT).show();
Log.e("login",t.getLocalizedMessage());
}
});
}
UserAccount.java
public class UserAccount {
private int userID;
private String username;
private String user_email;
private String user_phone;
private String user_password;
private Double user_location_latitude;
private Double user_location_longitude;
private String error;
// getter and setter
}
any suggestions may be help. Thanks a lot
Solution: The problem is in Api exactly at count() function I change it to empty() function and it's work well.
change PATH_URL = "/file/" to "file/"

REST API call always onFailure

I am new to Android rest api, basically I was trying to retrieve some information from a demo website using rest api together with the retrofit. Somehow my api call always on failure, hope someone could help.
Api.class
String BASE_URL = "https://demo.openmrs.org/openmrs/ws/rest/v1/";
#GET("location?tag=Login%20Location")
Call<List<RetroLocation>> getLocation();
Location.class
#SerializedName("display")
#Expose
protected String display;
#Expose
private String name;
public RetroLocation() {}
public RetroLocation(String display) {
this.display = display;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
Call the api from the main class
private void retriveLocation() {
//create object for the RetrofitInstance
RestApi api = RetrofitInstance.getRetrofitInstance().create(RestApi.class);
//making the call object using the api method created in the api class
Call<List<RetroLocation>> call = api.getLocation();
//making the call using enqueue(), it takes callback interface as an argument
call.enqueue(new Callback<List<RetroLocation>>() {
#Override
public void onResponse(Call<List<RetroLocation>> call, Response<List<RetroLocation>> response) {
progressDoalog.dismiss();
if (response.body() != null) {
//goes to my list
List<RetroLocation> locationList = response.body();
//creating an string array for the listview
String[] location = new String[locationList.size()];
//fill in the array with the response data from json
for (int i = 0; i < locationList.size(); i++) {
location[i] = locationList.get(i).getName();
}
//displaying the string array into the listView
ArrayAdapter adapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.custom_row, R.id.location, location);
listviewLocation.setAdapter(adapter);
Log.d("result", "Respond");
//generateDataList(response.body());
}else
Log.d("result", "No response");
}
#Override
public void onFailure(Call<List<RetroLocation>> call, Throwable t) {
//if(progressDoalog != null && progressDoalog.isShowing())
//progressDoalog.dismiss();
Toast.makeText(MainActivity.this, "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
Log.d("result", "onFailuer");
}
});
RestInstance.class
public static Retrofit getRetrofitInstance() {
if (retrofit == null) {
retrofit = new retrofit2.Retrofit.Builder()
.baseUrl(RestApi.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
if(retrofit!=null)
Log.d("result", "logged in");
return retrofit;
}
static OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.header("Authorization", "Basic "+ getAuth())
.header("Accept", "application/json")
.method(original.method(),original.body())
.build();
return chain.proceed(request);
}
}).build();
Update: Here is the throwable error message
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
at com.google.gson.stream.JsonReader.beginArray(JsonReader.java:350)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:119)
at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:218)
at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:112)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
The output of base_url
<org.openmrs.module.webservices.rest.SimpleObject serialization="custom">
<unserializable-parents/>
<map>
<default>
<loadFactor>0.75</loadFactor>
<threshold>12</threshold>
</default>
<int>16</int>
<int>3</int>
<string>uuid</string>
<string>b1a8b05e-3542-4037-bbd3-998ee9c40574</string>
<string>display</string>
<string>Inpatient Ward</string>
<string>links</string>
<list>
<org.openmrs.module.webservices.rest.web.Hyperlink>
<rel>self</rel>
<uri>
http://demo.openmrs.org/openmrs/ws/rest/v1/location/b1a8b05e-3542-4037-bbd3-998ee9c40574
</uri>
</org.openmrs.module.webservices.rest.web.Hyperlink>
</list>
</map>
<linked-hash-map>
<default>
<accessOrder>false</accessOrder>
</default>
</linked-hash-map>
</org.openmrs.module.webservices.rest.SimpleObject>
<org.openmrs.module.webservices.rest.SimpleObject serialization="custom">
<unserializable-parents/>
<map>
<default>
<loadFactor>0.75</loadFactor>
<threshold>12</threshold>
</default>
<int>16</int>
<int>3</int>
<string>uuid</string>
<string>2131aff8-2e2a-480a-b7ab-4ac53250262b</string>
<string>display</string>
<string>Isolation Ward</string>
<string>links</string>
<list>
<org.openmrs.module.webservices.rest.web.Hyperlink>
<rel>self</rel>
<uri>
http://demo.openmrs.org/openmrs/ws/rest/v1/location/2131aff8-2e2a-480a-b7ab-4ac53250262b
</uri>
</org.openmrs.module.webservices.rest.web.Hyperlink>
</list>
</map>
<linked-hash-map>
<default>
<accessOrder>false</accessOrder>
</default>
</linked-hash-map>
</org.openmrs.module.webservices.rest.SimpleObject>
try this
BASE URL https://demo.openmrs.org/
Retrofit Interface
#GET("openmrs/ws/rest/v1/location")
Call<List<RetroLocation>> getLocation(#Query("tag") String tag);
Change like this may work.
String BASE_URL = "https://demo.openmrs.org/";
#GET("openmrs/ws/rest/v1/location?tag=Login%20Location")
Call<List<RetroLocation>> getLocation();

How can I use from GraphQl in android?

I need to a simple example for use GraphQl in android .
How can I use from GraphQl in android (tutorial).
In order to use GraphQL (in general), you need two things:
1. A GraphQL server
There are a few ways how you could go about this. Of course, you could simply go and implement one yourself in any server-side language you like.
Other (faster) approaches are to take advantage of existing tooling and generate a GraphQL API using services like graphql-up or create-graphql-server or even services like Graphcool (disclaimer: I work for them).
2. A GraphQL client library
Though this one isn't strictly necessary and you could also simply interact with the GraphQL server through plain HTTP (sending your queries and mutations in the body of POST requests), it is certainly beneficial to use existing tools that take repetitive work like caching or UI integrations off your shoulders. One of the most popular GraphQL clients right now is Apollo, and they're very actively working on a version for Android as well. However, this hasn't been officially released yet. So, you either have to use their existing development version of chose the former approach using plain HTTP for now.
Here is an example of querying GraphQl from Client. In this example I am using Retrofit 2:
// QueryHelper.java
// This line below is the simple format of Gql query
query = "query{me{name, location, majorOfInterest,profilePhoto{url(size: 400) }}}";
//Post the query using Retrofit2
GqlRetrofitClient.getInstance(getContext()).fetchUserDetails(new GqlQueryRequest(queryUserDetails)).enqueue(new Callback<UserDetails>() {
#Override
public void onResponse(Call<UserDetails> call, Response<UserDetails> response) {
//OnResponse do something();
}
#Override
public void onFailure(Call<UserDetails> call, Throwable t) {
Log.d(TAG, "Failed to fetch User details");
}
});
//GqlClient.java
public class GqlRetrofitClient {
public static final String BASE_URL = BuildConfig.DOMAIN;
private static GqlRetrofitClient sInstance;
private GqlRetrofitService mGqlRetrofitService;
Gson gson = new GsonBuilder().create();
private GqlRetrofitClient(final Context context) {
// Network Interceptor for logging
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new Interceptor() {
#Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("X-User-Token", "AUTH_TOKEN")
.addHeader("X-User_Email", "Email")
.addHeader("content-type", "application/json")
.build();
return chain.proceed(request);
}
})
.addInterceptor(httpLoggingInterceptor)
.build();
// Retrofit initialization
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.client(okHttpClient)
.build();
mGqlRetrofitService = retrofit.create(GqlRetrofitService.class);
}
// Create an instance of GqlRetrofitClient to create retrofit service
public static GqlRetrofitClient getInstance(Context context){
if(sInstance == null){
sInstance = new GqlRetrofitClient(context.getApplicationContext());
}
return sInstance;
}
// Method call to get User details
public Call<UserDetails> fetchUserDetails(GqlQueryRequest queryUserDetails){
return mGqlRetrofitService.getUserDetails(queryUserDetails);
}
}
//GqlRetrofitService.java
public interface GqlRetrofitService{
#POST("/api/graph.json")
Call<UserDetails> getUserDetails(#Body GqlQueryRequest body);
}
In your manifest to add
<uses-permission android:name="android.permission.INTERNET"/>
Your dependencies
// Kotlin Coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.4'
//OkHttp
implementation ("com.squareup.okhttp3:okhttp:3.12.12"){
force = true //API 19 support
}
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.12'
//retrofit
implementation "com.squareup.retrofit2:retrofit:2.7.1"
implementation "com.squareup.retrofit2:converter-scalars:$2.7.1"
Also Java 8 compatibility
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
With the service
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST
interface GraphQLService {
#Headers("Content-Type: application/json")
#POST("/")
suspend fun postDynamicQuery(#Body body: String): Response<String>
}
you can create a object
import retrofit2.Retrofit
import retrofit2.converter.scalars.ScalarsConverterFactory
object GraphQLInstance {
private const val BASE_URL: String = "http://192.155.1.55:2000/"
val graphQLService: GraphQLService by lazy {
Retrofit
.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(ScalarsConverterFactory.create())
.build().create(GraphQLService::class.java)
}
}
In the activity you can create this method
private fun post(userId: String){
val retrofit = GraphQLInstance.graphQLService
val paramObject = JSONObject()
paramObject.put("query", "query {users(userid:$userId){username}}")
GlobalScope.launch {
try {
val response = retrofit.postDynamicQuery(paramObject.toString())
Log.e("response", response.body().toString())
}catch (e: java.lang.Exception){
e.printStackTrace()
}
}
}
You can check the example in GitHub and my post
Note: if you need a mutation should be to change this line
paramObject.put("query", "query {users(userid:$userId){username}}")
to
paramObject.put("query", "mutation {users(userid:$userId){username}}")
personally I use Retrofit and I took this Link Credits
and changed some things.
This is the code:
In File "GraphQLConverter.java":
public class GraphQLConverter extends Converter.Factory {
private static final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
private GraphQueryProcessor graphProcessor;
private final Gson mGson;
private GraphQLConverter(Context context) {
graphProcessor = new GraphQueryProcessor(context);
mGson = new GsonBuilder()
.enableComplexMapKeySerialization()
.setLenient()
.create();
}
public static GraphQLConverter create(Context context) {
return new GraphQLConverter(context);
}
/** Override Converter.Factory Methods **/
#Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
return null;
}
#Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
if(type == QueryContainerBuilder.class){
return new GraphRequestConverter(methodAnnotations);
} else {
return null;
}
}
/** RequestConverter Class **/
private class GraphRequestConverter implements Converter<QueryContainerBuilder, RequestBody> {
private Annotation[] mAnnotations;
private GraphRequestConverter(Annotation[] annotations) {
mAnnotations = annotations;
}
#Override
public RequestBody convert(#NonNull QueryContainerBuilder containerBuilder) {
QueryContainerBuilder.QueryContainer queryContainer = containerBuilder
.setQuery(graphProcessor.getQuery(mAnnotations))
.build();
return RequestBody.create(MEDIA_TYPE, mGson.toJson(queryContainer).getBytes());
}
}
}
In File "GraphQuery.java":
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface GraphQuery {
String value() default "";
}
In File "GraphQueryProcessor.java":
class GraphQueryProcessor {
private static final String TAG = GraphQueryProcessor.class.getSimpleName();
// GraphQl Constants
private static final String EXT_GRAPHQL = ".graphql";
private static final String ROOT_FOLDER_GRAPHQL = "graphql";
private final Map<String, String> mGraphQueries;
private Context mContext;
GraphQueryProcessor(Context context) {
mGraphQueries = new WeakHashMap<>();
mContext = context;
populateGraphQueries(ROOT_FOLDER_GRAPHQL);
}
/** Package-Private Methods **/
String getQuery(Annotation[] annotations) {
if(mGraphQueries == null || mGraphQueries.isEmpty()){
populateGraphQueries(ROOT_FOLDER_GRAPHQL);
}
GraphQuery graphQuery = null;
for (Annotation annotation : annotations) {
if (annotation instanceof GraphQuery) {
graphQuery = (GraphQuery) annotation;
break;
}
}
if (graphQuery != null) {
String fileName = String.format("%s%s", graphQuery.value(), EXT_GRAPHQL);
if (mGraphQueries != null && mGraphQueries.containsKey(fileName)) {
return mGraphQueries.get(fileName);
}
}
return null;
}
/** Private Methods **/
private void populateGraphQueries(#NonNull String path) {
try {
String[] paths = mContext.getAssets().list(path);
if (paths != null && paths.length > 0x0) {
for (String item : paths) {
String absolute = path + "/" + item;
if (!item.endsWith(EXT_GRAPHQL)) {
populateGraphQueries(absolute);
} else {
mGraphQueries.put(item, getFileContents(mContext.getAssets().open(absolute)));
}
}
}
} catch (IOException ioE) {
BaseEnvironment.onExceptionLevelLow(TAG, ioE);
}
}
private String getFileContents(InputStream inputStream) {
StringBuilder queryBuffer = new StringBuilder();
try {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
for (String line; (line = bufferedReader.readLine()) != null; )
queryBuffer.append(line);
inputStreamReader.close();
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
return queryBuffer.toString();
}
}
In File "QueryContainerBuilder.java":
public class QueryContainerBuilder {
// Mask Types
private static final byte MASK_REPLACE_QUERY_ARGUMENTS = 0b1; // Invece di inviare il json con le variabili va a inserirle nella query i valori sostituendo i tipi degli argomenti.
private static final byte MASK_REPLACE_EXPLICIT_QUOTES = MASK_REPLACE_QUERY_ARGUMENTS << 0b1; // Alle stringhe non vengono automaticamente messe le virgolette ma devono essere aggiunte nei valori passati per le variabili.
private static final byte MASK_REPLACE_WITH_PLACEHOLDERS = MASK_REPLACE_EXPLICIT_QUOTES << 0b1; // Va a sostituire i placeholders "<key_var_name>" presenti nella query con i valori delle variabili.
private QueryContainer mQueryContainer;
private byte mMask;
public QueryContainerBuilder() {
mQueryContainer = new QueryContainer();
}
/** Setter Methods **/
public QueryContainerBuilder setQuery(String query) {
mQueryContainer.setQuery(query);
return this;
}
public QueryContainerBuilder setReplaceQueryArguments(){
mMask = MASK_REPLACE_QUERY_ARGUMENTS;
return this;
}
public QueryContainerBuilder setReplaceExplicitQuotes(){
mMask = MASK_REPLACE_QUERY_ARGUMENTS | MASK_REPLACE_EXPLICIT_QUOTES;
return this;
}
public QueryContainerBuilder setReplaceWithPlaceholders(){
mMask = MASK_REPLACE_QUERY_ARGUMENTS | MASK_REPLACE_WITH_PLACEHOLDERS;
return this;
}
/** Public Methods **/
public QueryContainerBuilder putVariable(String key, Object value) {
mQueryContainer.putVariable(key, value);
return this;
}
public boolean containsVariable(String key) {
return mQueryContainer.containsVariable(key);
}
/** Builder Methods **/
public QueryContainer build() {
if((mMask & MASK_REPLACE_QUERY_ARGUMENTS) != 0x0){
if((mMask & MASK_REPLACE_WITH_PLACEHOLDERS) != 0x0){
mQueryContainer.replaceVariablesPlaceholdersInQuery();
} else {
mQueryContainer.replaceVariablesInQuery(mQueryContainer.mVariables, 0x0);
}
mQueryContainer.mVariables = null;
}
return mQueryContainer;
}
/** Public Static Classes **/
public class QueryContainer {
#SerializedName("variables")
private LinkedHashMap<String, Object> mVariables;
#SerializedName("query")
private String mQuery;
QueryContainer() {
mVariables = new LinkedHashMap<>();
}
/** Private Methods **/
private void setQuery(String query) {
mQuery = query;
}
private void putVariable(String key, Object value) {
mVariables.put(key, value);
}
private boolean containsVariable(String key) {
return mVariables != null && mVariables.containsKey(key);
}
private void replaceVariablesInQuery(LinkedHashMap<String, Object> map, int index){
if(!TextUtils.isEmpty(mQuery) && map.size() > 0x0){
List<String> keys = new ArrayList<>(map.keySet());
for(String key : keys){
Object value = map.get(key);
if(value instanceof LinkedHashMap){
replaceVariablesInQuery((LinkedHashMap<String, Object>) value, index);
} else {
int i = mQuery.indexOf(key + ":", index) + key.length() + 0x1;
int z;
if(keys.indexOf(key) < keys.size() - 0x1){
z = mQuery.indexOf(",", i);
} else {
z = mQuery.indexOf(")", i);
int x = mQuery.substring(i, z).indexOf('}');
if(x != -0x1){
if(mQuery.substring(i, i + 0x4).contains("{")){
x++;
}
z -= ((z - i) - x);
}
}
String replace;
if((mMask & MASK_REPLACE_EXPLICIT_QUOTES) != 0x0){
replace = String.valueOf(value);
} else {
replace = value instanceof String ?
"\"" + value.toString() + "\"" : String.valueOf(value);
}
String sub = mQuery.substring(i, z)
.replaceAll("[\\\\]?\\[", "\\\\\\[").replaceAll("[\\\\]?\\]", "\\\\\\]")
.replaceAll("[\\\\]?\\{", "\\\\\\{").replaceAll("[\\\\]?\\}", "\\\\\\}");
mQuery = mQuery.replaceFirst(sub.contains("{}") ? sub.replace("{}", "").trim() : sub.trim(), replace);
index = z + 0x1;
}
}
}
}
private void replaceVariablesPlaceholdersInQuery(){
if(!TextUtils.isEmpty(mQuery) && mVariables.size() > 0x0){
for(String key : mVariables.keySet()){
mQuery = mQuery.replaceFirst("\\<" + key + "\\>", mVariables.get(key) != null ? mVariables.get(key).toString() : "null");
}
mVariables = null;
}
}
}
}
Put your queries in a "graphql" directory in the "assets" folder with the ".graphql" extension for your query files. You can change the extension or the folder by changing the "EXT_GRAPHQL" or "ROOT_FOLDER_GRAPHQL" constants in "GraphQueryProcessor". You can use these formats for the queries:
query {
myQuery(param1: <myParam1>) {
....
}
}
If you use this format you need to use "MASK_REPLACE_WITH_PLACEHOLDERS" in your QueryContainerBuilder. Also you need to pass as the HashMap key the name of the placeholder without the "<...>", so in this case "myParam1".
The others format are just common GraphQL queries, like:
query ($p1: String!) {
muQuery(p1: $id) {
...
}
}
With this format you can use normal QueryContainerBuilder behaviour (no mask applyed, so it will pass and generate the "variables" json object.) or the "MASK_REPLACE_QUERY_ARGUMENTS" which will remove the "$id" and place the value.
When you init Retrofit add the "GraphQLConverter". Take care about the "ConvertFactories" order! You can put more ConvertFactory, but they consume the input so if in this case you put "Gson" before "GraphQL" the "GsonConverted" will consume the input data:
new Retrofit.Builder()
.baseUrl(mBaseUrl)
.addConverterFactory(GraphQLConverter.create(context))
.addConverterFactory(GsonConverterFactory.create(gson))
.client(getBaseHttpClient(interceptor))
.build();
In your Retrofit API:
#POST(AppConstants.SERVICE_GQL)
#GraphQuery(AppConstants.MY_GRAPHQL_QUERY_FILENAME)
fun callMyGraphQlQuery(#Body query: QueryContainerBuilder): Call<MyGraphQlResponse>
Call examples:
val query = QueryContainerBuilder()
.putVariable("myParam1", myValue)
.setReplaceWithPlaceholders()
createService(API::class.java).callMyGraphQlQuery(query)
val query = QueryContainerBuilder()
.putVariable("p1", myValue)
.setReplaceQueryArguments()
createService(API::class.java).callMyGraphQlQuery(query)
val query = QueryContainerBuilder()
.putVariable("p1", myValue)
createService(API::class.java).callMyGraphQlQuery(query)
Idk if the "MASK_REPLACE_QUERY_ARGUMENTS" works right, I used it only 2/3 times and then the back-end was changed and wrote better.
I did those cases (masks) to process the queries because I had this 3 case of queries with the back-end I was calling.
You can just add others query processing behavior just by adding another mask and the code in the "QueryContainerBuilder".
If anyone use this code and change it making it better, please write me the changes so I will change the code in my library too.
Thanks you,
have a nice coding and day :D
Bye!

Retrofit2 Tail Recursion Using RxJava / RxAndroid

I am really trying to get a hang of using Retrofit with RxJava / RxAndroid. I've done this using normal Retrofit2 Callback method in a previous app without the use of Reactive Programming and it worked fine. So, here is it. I need to Tail Recall a function meant to fetch all Local Government from the server. The API uses pagination (I have to construct the URL with ?page=1, perPage=2). I've to do this till I've the whole data. So, below is my Rx code
public static Observable<LgaListResponse> getPages(Context acontext) {
String token = PrefUtils.getToken(acontext);
BehaviorSubject<Integer> pageControl = BehaviorSubject.<Integer>create(1);
Observable<LgaListResponse> ret2 = pageControl.asObservable().concatMap(integer -> {
if (integer > 0) {
Log.e(TAG, "Integer: " + integer);
return ServiceGenerator.createService(ApiService.class, token)
.getLgas(String.valueOf(integer), String.valueOf(21))
.doOnNext(lgaListResponse -> {
if (lgaListResponse.getMeta().getPage() != lgaListResponse.getMeta().getPageCount()) {
pageControl.onNext(initialPage + 1);
} else {
pageControl.onNext(-1);
}
});
} else {
return Observable.<LgaListResponse>empty().doOnCompleted(pageControl::onCompleted);
}
});
return Observable.defer(() -> ret2);
}
And my ServiceGenerator Class
public class ServiceGenerator {
private static final String TAG = "ServiceGen";
private static OkHttpClient.Builder builder = new OkHttpClient.Builder();
private static Retrofit.Builder retrofitBuilder =
new Retrofit.Builder()
.baseUrl(BuildConfig.HOST)
.addCallAdapterFactory(RxJavaCallAdapterFactory.createWithScheduler(Schedulers.io()))
.addConverterFactory(GsonConverterFactory.create(CustomGsonParser.returnCustomParser()));
public static <S> S createService(Class<S> serviceClass, String token) {
builder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
/*builder.addNetworkInterceptor(new StethoInterceptor());*/
builder.connectTimeout(30000, TimeUnit.SECONDS);
builder.readTimeout(30000, TimeUnit.SECONDS);
if (token != null) {
Interceptor interceptor = chain -> {
Request newRequest = chain.request().newBuilder()
.addHeader("x-mobile", "true")
.addHeader("Authorization", "Bearer " + token).build();
return chain.proceed(newRequest);
};
builder.addInterceptor(interceptor);
}
OkHttpClient client = builder.build();
Retrofit retrofit = retrofitBuilder.client(client).build();
Log.e(TAG, retrofit.baseUrl().toString());
return retrofit.create(serviceClass);
}
public static Retrofit retrofit() {
OkHttpClient client = builder.build();
return retrofitBuilder.client(client).build();
}
public static class CustomGsonParser {
public static Gson returnCustomParser(){
return new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
#Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}
#Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.create();
}
}
}
So, I noticed on the first call, I get a response, but on the second one, the 440Error is thrown. The URL is formed, but the request throws a 400Error. I don't know why it's throwing a 400 everything is working fine if I use POSTMAN to test. And, I tested with my old code too. The Log is too long, so I put it in pastebin LOGS any help thanks. I've written most of this app with RxAndroid / RxJava. Thanks
I suggest you simplify things (and remove recursion). First build up your pages using something like
public static Observable<LgaListResponse> getPages(Context acontext, int initialPage, int perPage) {
String token = PrefUtils.getToken(acontext);
BehaviorSubject<Integer> pagecontrol = BehaviorSubject.<Integer>create(initialPage);
Observable<LgaListResponse> ret2 = pagecontrol.asObservable().concatMap(
new Func1<Integer,Observable<LgaListResponse>>() {
Observable<LgaListResponse> call(Integer pageNumber) {
if (pageNumber > 0) {
return ServiceGenerator.createService(ApiService.class, token)
.getLgas(String.valueOf(aKey), String.valueOf(perPage))
.doOnNext(
new Action1<LgaListResponse>() {
void call(LgaListResponse page) {
if (page.getMeta().getPage() != page.getMeta().getPageCount()) {
pagecontrol.onNext(page.getMeta().getNextPage());
} else {
pagecontrol.onNext(-1);
}
}
}
);
}
else {
return Observable.<LgaListResponse>empty().doOnCompleted(()->pagecontrol.onCompleted());
}
}
}
);
return Observable.defer(
new Func0<Observable<LgaListResponse>() {
Observable<LgaListResponse> call() {
return ret2;
}
}
);
}
then subscribe to the resulting observable. It looks horrible because I've avoided using lambdas but it should work.

Categories

Resources