
Retrofit Registration1 |
Retrofit Registration2 |
Retrofit Registration3 |
---|---|---|
Add below things in your grdale folder
1 2 3 |
implementation 'com.squareup.retrofit2:converter-gson:2.2.0' implementation 'com.squareup.retrofit2:retrofit:2.3.0' implementation 'com.google.code.gson:gson:2.8.1' |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?xml version="1.0" encoding="utf-8"?> <layout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.androindian.retrofit.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Name" android:id="@+id/name"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Mobile" android:id="@+id/mobile"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" android:id="@+id/email"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Pass" android:id="@+id/Pass"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Click" android:id="@+id/Reg"/> </LinearLayout> </layout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
package com.androindian.retrofit; import android.databinding.DataBindingUtil; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; import com.androindian.retrofit.databinding.ActivityMainBinding; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.json.JSONException; import org.json.JSONObject; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class MainActivity extends AppCompatActivity { ActivityMainBinding activityMainBinding; JsonObject object=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); activityMainBinding= DataBindingUtil.setContentView(MainActivity.this,R.layout.activity_main); Retrofit retrofit=new Retrofit.Builder() .baseUrl("http://androindian.com/apps/example_app/") .addConverterFactory(GsonConverterFactory.create()) .build(); Register register=retrofit.create(Register.class); activityMainBinding.Reg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { JSONObject j1=new JSONObject(); try { j1.put("name",activityMainBinding.name.getText().toString().trim()); j1.put("mobile",activityMainBinding.mobile.getText().toString().trim()); j1.put("email",activityMainBinding.email.getText().toString().trim()); j1.put("pswrd",activityMainBinding.Pass.getText().toString().trim()); j1.put("baction","register_user"); } catch (JSONException e) { e.printStackTrace(); } object=new JsonParser().parse(j1.toString()).getAsJsonObject(); Log.e("object",object.toString()); } }); Call<Response> responseCall=register.CreateUser(object); responseCall.enqueue(new Callback<Response>() { @Override public void onResponse(Call<Response> call, retrofit2.Response<Response> response) { Log.i("Res",response.body().getUser()); if (response.body().getResponse().trim().equals("success")) { Toast.makeText(getApplicationContext(), "" + response.body().getUser(), Toast.LENGTH_LONG).show(); } else if (response.body().getResponse().trim().equals("failed")) { Toast.makeText(getApplicationContext(), "" + response.body().getUser(), Toast.LENGTH_LONG).show(); } else if (response.body().getResponse().trim().equals("error")) { //Toast.makeText(getApplicationContext(), "" + status2, Toast.LENGTH_LONG).show(); } } @Override public void onFailure(Call<Response> call, Throwable t) { } }); } } |
Register.java
you need to create one interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.androindian.retrofit; import com.google.gson.JsonObject; import retrofit2.Call; import retrofit2.http.Body; import retrofit2.http.Headers; import retrofit2.http.POST; /** * Created by janga on 14-11-2017. */ public interface Register { @Headers("Content-Type:application/json") @POST("api.php") Call<Response> CreateUser(@Body JsonObject jsonObject); } |
Response.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.androindian.retrofit; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; /** * Created by janga on 14-11-2017. */ public class Response { @SerializedName("response") @Expose private String response; @SerializedName("user") @Expose private String user; public String getResponse() { return response; } public void setResponse(String response) { this.response = response; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } } |
you need to add internet permission in manifest file.