Post and receive Json String using retrofit in an android application

Viewed 49

I am new to android coding. In my android application , I have to match a string present in a log file on the server with the current signature of the android application using retrofit client. But I am unable to get any result and code is skipping the response part.

Here is my work around:

My API.JAVA class

package com.example.unify.api;

public interface Api
{

    @Multipart
    @POST("api/upload/")
    //Call<ResponseBody> uploadImages(@Part MultipartBody.Part part,@Part("somedata") RequestBody requestBody);
    Call<ResponseBody> uploadImages(@Header ("Authorization") String  authToken,@Part MultipartBody.Part part, @Part("image") RequestBody requestBody);
    //Call<ResponseBody> uploadImages(@Part MultipartBody.Part part, @Part("image") RequestBody requestBody);

    @FormUrlEncoded
    @POST("api/api-token-auth/")
    Call<ResponseBody> userLogin(@Field("username") String email, @Field("password") String password);


/*    @FormUrlEncoded
    @POST("api/hash/")
    Call<ResponseBody> uploadImages(@Header ("Authorization") String  authToken);*/
    @FormUrlEncoded
    @POST("api/hash/")
    Call<ResponseBody> signature_match(@Field("signature") String signage);


}

function which is not getting any response :

public static boolean checkAppSignature(Context context) {

        try {


            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);

            for (Signature signature : packageInfo.signatures) {
               String content= signature.toCharsString();
                X509Certificate cert=null;
                byte[] signatureBytes = signature.toByteArray();
                try {
                    CertificateFactory certFactory = CertificateFactory
                            .getInstance("X.509");
                   cert = (X509Certificate) certFactory.generateCertificate(new ByteArrayInputStream(signature.toByteArray()));
                    String base64EncodedPublicKey = Base64.encodeToString(cert.getPublicKey().getEncoded(),Base64.DEFAULT);

                  
                    Retrofit retro = RetrofitClient.getRetrofit();
                    Api uploadAP = retro.create(Api.class);
                    Call cal = uploadAP.signature_match("signature"+":"+base64EncodedPublicKey);

                    cal.enqueue(new Callback<ResponseBody>() {
                                     @Override
                                     public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                                         //WHEN THE RESPONSE IS RECEIVED FROM SERVER
                                         try {
                                             //CONVERT THE JSON RESPONSE TO STRING
                                             String sgn = response.body().string();
                                             JSONObject jsonObject = new JSONObject(sgn);
                                             String resultant = jsonObject.getString("signature");

                                             //Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_SHORT).show();

                                             //if(result.equals("Yes")) {
                                             if (resultant != null && !resultant.isEmpty() && !resultant.equals("null")) {
                                                 //IF THE AUTHENTICATION IS SUCCESSFUL
                                                 storedPublicKeyBase64=resultant;
                                             }
                                         } catch (Exception e) {
                                             e.printStackTrace();
                                         }
                                     }

                                     @Override
                                     public void onFailure(Call<ResponseBody> call, Throwable t) {
                                         //IF THERE IS NO RESPONSE FROM SERVER
                                         System.out.println("*** Declaration ***");

                                     }
                                 });

                    /************************************Matching Ends Here******************************************************/
                    if (storedPublicKeyBase64.equalsIgnoreCase(base64EncodedPublicKey)){
                        return Boolean.TRUE;
                    };

                } catch (CertificateException e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            
        }

        return Boolean.FALSE;
    }

Code is exiting after this line and not going further : cal.enqueue(new Callback() {

please help , I will be highly grateful.

0 Answers
Related