Friday 16 June 2017

Retrofit With POST Method



STEP-1 : AndroidManifest.xml
--------------------------------------------------------------
<uses-permission android:name="android.permission.INTERNET"/>
--------------------------------------------------------------------

STEP-2 : App Level Gradle

---------------------------------------------------------------------
    // retrofit, gson
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
------------------------------------------------------------------------

STEP-3 : ApiInterface.interface
-----------------------------------------------------------------
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface ApiInterface {

   @FormUrlEncoded
   @POST("/currency/cu.php")
   Call<POJOs> getRetrofitResponse(@Field("amount") String amount, @Field("fromc") String from, @Field("to") String to);
}

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

STEP-4 : ApiClient.java
---------------------------------------------------------------------
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

    public static final String BASE_URL = "http://192.168.1.3";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

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

STEP-5 : MainActivity.java
------------------------------------------------------------------------
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);

        apiService.getRetrofitResponse("2", "USD", "INR").enqueue(new Callback<POJOs>() {
            @Override
            public void onResponse(Call<POJOs> call, Response<POJOs> response) {

                System.out.println(response.code());
                if(response.isSuccessful()) {

                    Double s = response.body().getValue();
                        Toast.makeText(MainActivity.this, "OUTPUT : "+s,Toast.LENGTH_LONG).show();

                }else {
                  //  int statusCode  = response.code();
                    // handle request errors depending on status code
                }
            }

            @Override
            public void onFailure(Call<POJOs> call, Throwable t) {
                Log.d("MainActivity", "error loading from API");

            }
        });
 
    }
}

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

STEP-6 : POJOs.java
---------------------------------------------------------------------
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class POJOs {

    @SerializedName("value ")
    @Expose
    private Double value;

    public Double getValue() {
        return value;
    }

    public void setValue(Double value) {
        this.value = value;
    }

}
--------------------------------------------------------------------------

STEP-7 : cu.php
---------------------------------------------------------------------------------
<?php
function currencyConvertor($amount,$from_Currency,$to_Currency) {
     $amount = urlencode($amount);
      $from_Currency = urlencode($from_Currency);
      $to_Currency = urlencode($to_Currency);
      $get = "https://www.google.com/finance/converter?a=$amount&from=$from_Currency&to=$to_Currency";
      $ch = curl_init();
                                        $url = $get;
                                        // Disable SSL verification
                                        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                                        // Will return the response, if false it print the response
                                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                                        // Set the url
                                        curl_setopt($ch, CURLOPT_URL,$url);
                                        // Execute
                                        $result=curl_exec($ch);
                                        // Closing
                                        curl_close($ch);
      $get = explode("<span class=bld>",$result);
      $get = explode("</span>",$get[1]); 
      $converted_amount = preg_replace("/[^0-9\.]/", null, $get[0]);
      //return number_format($converted_amount, 2, '.', '');
 return round($converted_amount, 3);
   
}

if(isset($_POST['amount']) && isset($_POST['fromc']) && isset($_POST['to']))
 {

         $amount = $_POST['amount'];
         $fromc = $_POST['fromc'];
        $to   = $_POST['to'];
        $curr =   currencyConvertor($amount,$fromc,$to);
        echo json_encode(array("value "=>$curr));
       
 }

?>
-------------------------------------------------------------------------------------

IMAGE ON POSTMAN
-------------------------------------------------------------------


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













22

No comments:

Post a Comment