RetrofitGetNoQueryApi(Only Url)
IN Manifest file
uses-permission android:name="android.permission.INTERNET"
android:usesCleartextTraffic="true"
// For Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.9.0'
// For Vollry Library
implementation 'com.android.volley:volley:1.2.1'
Please Note That Response class support only one in some projects
import com.android.volley.Response;
import retrofit2.Response;
Using Query Params
Its Interface
@GET("posts")
Call getPost();
// Call getPost();
It not shwoing ResponseBody in So Do not Confuse
void RetrofitGetNoQueryApi() {
RetrofitClient.getClient().getPost().enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccesful()) {
Toast.makeText(MainActivity.this, "" + response.body(), Toast.LENGTH_SHORT).show();
} else {
}
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
}
void VolleyGetNoQuery() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "https://jsonplaceholder.typicode.com/posts";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener() {
@Override
public void onResponse(String response) {
Toast.makeText(context, ""+response, Toast.LENGTH_SHORT).show();
TextView txt= findViewById(R.id.txt);
try {
JSONArray jsonArray = new JSONArray(response);
String a= jsonArray.getJSONObject(0).getString("title");
txt.setText(a);
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
// textView.setText("Error: " + error.getMessage());
Toast.makeText(context, ""+ error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
queue.add(stringRequest);
}
Using Query Parameters Get
Its Interface Code
@GET("OnlineCourseStudentDashboard/getTeacherDetails")
Call getQueryParams(@Query("course") String Course,
@Query("board") String Board);
// Call getQueryParams(@Query("course") String Course,
// @Query("board") String Board);
void RetrofitGetQueryApi() {
RetrofitClient.getClient().getQueryParams("12th Class Commerce","CBSE").enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
try {
String jsonResponse = response.body().string();
JSONArray jsonArray = new JSONArray(jsonResponse);
} catch (JSONException e) {
Toast.makeText(MainActivity.this, "Error :" + e.getMessage(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
}
} else {
}
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
}
void VolleyGetQuery() {
RequestQueue queue = Volley.newRequestQueue(this);
String baseUrl = "https://api.gharpeshiksha.com/OnlineCourseStudentDashboard/getTeacherDetails";
String param1 = "12th Class Commerce";
String param2 = "CBSE";
String url = baseUrl + "?course=" + param1 + "&board=" + param2;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener() {
@Override
public void onResponse(String response) {
Toast.makeText(context, "" + response, Toast.LENGTH_SHORT).show();
TextView txt = findViewById(R.id.txt);
try {
JSONArray jsonArray = new JSONArray(response);
String a= jsonArray.getJSONObject(0).getString("picUrl");
txt.setText(a);
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
queue.add(stringRequest);
}
Using Query Params Get
Its Interface Code
@GET("OnlineCourseStudentDashboard/getTeacherDetails")
Call getQueryParams(@Query("course") String Course,
@Query("board") String Board);
void RetrofitGetQueryApi() {
RetrofitClient.getClient().getQueryParams("12th Class Commerce","CBSE").enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.isSuccessful()) {
try {
String jsonResponse = response.body().string();
JSONArray jsonArray = new JSONArray(jsonResponse);
} catch (JSONException e) {
Toast.makeText(MainActivity.this, "Error :" + e.getMessage(), Toast.LENGTH_SHORT).show();
} catch (IOException e) {
}
} else {
}
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
}
void VolleyGetQuery() {
RequestQueue queue = Volley.newRequestQueue(this);
String baseUrl = "https://api.gharpeshiksha.com/OnlineCourseStudentDashboard/getTeacherDetails";
String param1 = "12th Class Commerce";
String param2 = "CBSE";
String url = baseUrl + "?course=" + param1 + "&board=" + param2;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener _String_ () {
@Override
public void onResponse(String response) {
Toast.makeText(context, "" + response, Toast.LENGTH_SHORT).show();
TextView txt = findViewById(R.id.txt);
try {
JSONArray jsonArray = new JSONArray(response);
String a= jsonArray.getJSONObject(0).getString("picUrl");
txt.setText(a);
} catch (JSONException e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, "" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
queue.add(stringRequest);
}
Using Post X-www-form-url-encoded
===============Its Interface Code===================
public interface ApiServices {
@FormUrlEncoded
@POST("OnlineCourseStudentLogin/accountVerification")
Call getBody_x_www_urlencoded(@Field("phone") String phone);
}
for postman Body Form-data
do this
@FormUrlEncoded
@POST("signin")
Call MyLogin(
@Field("userid") String Phone,
@Field("password") String Password);
=============Its Retrofit Clent==============================
public class RetrofitClient {
public static ApiServices getClient(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.gharpeshiksha.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiServices apiService = retrofit.create(ApiServices.class);
return apiService;
}
}
=============Api Calling in Retrofit =============================
void Retrofit_Body_x_www_form_url_encoded(){
RetrofitClient.getClient().getBody_x_www_urlencoded("6387044056").enqueue(new Callback() {
@Override
public void onResponse(Call call, retrofit2.Response response) {
if (response.isSuccessful()){
try {
String jsonResponse = response.body().string();
JSONObject jsonObject = new JSONObject(jsonResponse);
Toast.makeText(MainActivity.this, ""+jsonObject, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
}
// Toast.makeText(MainActivity.this, ""+response.body(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call call, Throwable t) {
}
});
}
====================Volley====================================
void Volley_Body_x_www_formurlencode(){
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://api.gharpeshiksha.com/OnlineCourseStudentLogin/accountVerification"; // Replace with your API endpoint URL
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener _String_() {
@Override
public void onResponse(String response) {
// Handle the successful response
// This is where you'll process the response from the server
JSONObject jsonResponse = null;
try {
jsonResponse = new JSONObject(response);
String a= jsonResponse.getString("ProfileCompleted");
Toast.makeText(MainActivity.this, ""+a, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle errors that occur during the request
}
}
) {
@Override
protected Map_String, String_ getParams() {
// Define the parameters to send in the request body
Map_String, String_ params = new HashMap<>();
params.put("phone", "6387040456");
// Add more parameters as needed
return params;
}
};
// Add the request to the RequestQueue
requestQueue.add(stringRequest);
}
Using Post BOdy Raw Json
===============Its Interface Code===================
public interface ApiServices {
@POST("server/api/mobile/v1/send/otp")
Call sendRawRequest(@Body RequestModel requestModel);
}
==============Creating Model For Requst Body=================
package in.akash.vibrant.it.myapicalls;
public class RequestModel {
String mobile;
public RequestModel(String mobile) {
this.mobile = mobile;
}
}
=============Its Retrofit Clent==============================
public class RetrofitClient {
public static ApiServices getClient(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://aio.thebyteiq.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiServices apiService = retrofit.create(ApiServices.class);
return apiService;
}
}
=============Api Calling in Retrofit =============================
void Retrofit_Body_raw_json() {
RequestModel requestModel = new RequestModel("6387040456");
RetrofitClient.getClient().sendRawRequest(requestModel).enqueue(new Callback() {
@Override
public void onResponse(Call call, retrofit2.Response response) {
if (response.isSuccessful()) {
Toast.makeText(MainActivity.this, "" + response.body(), Toast.LENGTH_SHORT).show();
;
}
}
@Override
public void onFailure(Call call, Throwable t) {
Toast.makeText(MainActivity.this, "Failled ", Toast.LENGTH_SHORT).show();
}
});
}
====================Volley====================================
void Volley_Body_raw_json(){
RequestQueue requestQueue = Volley.newRequestQueue(this);
String url = "https://aio.thebyteiq.com/server/api/mobile/v1/send/otp"; // Replace with your API endpoint URL
// String requestBody = "{\"name\":\"John Doe\",\"phone\":\"1234567890\"}";
String requestBody = "{\"mobile\":\"6387040456\"}";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener _String_() {
@Override
public void onResponse(String response) {
// Handle the successful response
// This is where you'll process the response from the server
JSONObject jsonResponse = null;
try {
jsonResponse = new JSONObject(response);
// String a= jsonResponse.getString("ProfileCompleted");
Toast.makeText(MainActivity.this, ""+jsonResponse, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle errors that occur during the request
}
}
) {
@Override
public byte[] getBody() {
return requestBody.getBytes();
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
// Add the request to the RequestQueue
requestQueue.add(stringRequest);
}