Thursday 30 August 2018

Retrofit Use



public interface Api {
    //the base URL for our API   
 //make sure you are not using localhost 
   //find the ip usinc ipconfig command   
 String BASE_URL = "http://192.168.43.124/ImageUploadApi/";

    //this is our multipart request    
//we have two parameters on is name and other one is description 
   @Multipart    @POST("Api.php?apicall=upload")
    Call<MyResponse> uploadImage(@Part("image\"; filename=\"myfile.jpg\" ") 
RequestBody file, @Part("desc") RequestBody desc);

}



------------------------------------------------------------------------------------------



public class ApiClient {

    private static Retrofit retrofit = null;

    private static Gson gson = new GsonBuilder().setLenient().create();

    public static Retrofit getClient() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(AppConstant.BASEURL.URL.trim())
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();
        }
        return retrofit;
    }

}

-----------------------------------------------------------------------------------------

public interface MyApiEndpointInterface {

@POST(AppConstant.ENDPOINT.CHAT_STATUS)
Call<JsonObject> chatUser(@Body JsonObject jsonObject);

}


-----------------------------------------------------------------------------------------

public class AppConstant {
  public interface BASEURL {

        //        String URL = "http://truckslogistics.com/Projects-Work/LoggedIn/Mobile_APP/";//   
      //   String URL = "http://fxpips.co.uk/loggedin/Mobile_APP/";    
    String URL = "http://www.loggedinapp.com/Mobile_APP/";
    }
public interface ENDPOINT {
String CHAT_STATUS = "chat/chatUser.php";
}
}


-------------------------------------------------------------------------------------------

------------------Library-------------
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'

---------------------------------------------------------------------

public class MyResponse  implements Serializable {
    String status = "";
    String message = "";
    String File_Url = "";

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getFile_Url() {
        return File_Url;
    }

    public void setFile_Url(String file_Url) {
        File_Url = file_Url;
    }

}

-----------------------------------------------------------------------------------------
<uses-permission android:name="android.permission.INTERNET" />

---------------------------------------------------------------------------------------
                 --------------Activity---------

private void apicallingSetChatStatus() {
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("method", "chat_user");
    jsonObject.addProperty("user_id", "1");
    jsonObject.addProperty("admin_id", "1");
    /*{"method":"chat_user","user_id":"1"}*/   
 /*{"method":"chat_user","user_id":"1","admin_id":""}*/  
  retrofitSetChatStatus(jsonObject);
}


private void retrofitSetChatStatus(JsonObject jsonObject) {
    MyApiEndpointInterface apiService =
            ApiClient.getClient().create(MyApiEndpointInterface.class);
    Call<JsonObject> call = apiService.chatUser(jsonObject);
    call.enqueue(new Callback<JsonObject>() {
        @Override     
   public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            if (response.body() != null) {
                try {
                    Log.d("SetChatResponse ....", String.valueOf(response.body()));

                    JSONObject jsonObject1 = new JSONObject(response.body().toString());
                    String message = jsonObject1.getString("message");
                    String status = jsonObject1.getString("status");
                    if (status.equals("200")) {
                        stautschat = "1";
                    } else {
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Function.Snackbar(ChatActivity.this, findViewById(android.R.id.content), "Something went wrong.");
            }
        }

        @Override      
  public void onFailure(Call<JsonObject> call, Throwable t) {
        }
    });
}

No comments:

Post a Comment

AutoComplete Address (Updated)

-------------------------------------Activity---------------------------------- package placeautocomplete.iteritory.com; import androi...