Retrofit post example

Retrofit post example

Step 1 : create Rest Interface like below attached find it.

Step 2 : create MainActivity like below attached find it.

Step 3 : create layout file like below attached find it.

Step 4: create gradle add it and (Sync) it.

Step 5 : Run execute and watch the output in layout. Over.

Step 6 : Comment this tutorial below or write what you want.

Step 1:

public interface DetailsAPI {

@FormUrlEncoded

@POST("/login")

void loginUser(@Field("email_id") String emailId, @Field("password") String password,

@Field("user_type") String newP,Callback<JsonObject> callback);

}

Step 2:

public class MainActivity extends AppCompatActivity {

public static final String Root_url = "http://drapp.sciensdemos.in/index.php/doctorapp_services/";

private EditText etMail;

private EditText etPassword;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public void loginUserClick(View view) {

etMail = (EditText) findViewById(R.id.etMail);

etPassword = (EditText) findViewById(R.id.etPassword);

loginUser();

}

private void loginUser() {

RestAdapter adapter = new RestAdapter.Builder().setEndpoint(Root_url).build();

DetailsAPI api = adapter.create(DetailsAPI.class);

api.loginUser(etMail.getText().toString(), etPassword.getText().toString(),

"2", new Callback<JsonObject>() {

@Override

public void success(JsonObject jsonObject, Response response) {

Toast.makeText(MainActivity.this, "Login successful!", Toast.LENGTH_SHORT).show();

Log.d("Lokesh", "Login successful");

}

@Override

public void failure(RetrofitError error) {

Log.d("Lokesh", "Login fail::" + error.toString());

Toast.makeText(MainActivity.this, "Login fail!", Toast.LENGTH_SHORT).show();

}

});

}

}

Step 3:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context="sciens.com.fad.MainActivity">

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputType="textEmailAddress"

android:ems="10"

android:hint="Enter mail"

android:id="@+id/etMail" />

<EditText

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputType="textPassword"

android:ems="10"

android:hint="Enter password"

android:id="@+id/etPassword" />

<Button

android:onClick="loginUserClick"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Login"/>

</LinearLayout>

Step 4: after put this line don't forget to sync it.

dependencies {

compile 'com.squareup.retrofit:retrofit:1.9.0'

}

Step 5:

Retrofit post