Dependencies
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.9.1'
APIclient.java
package in.softwisdom.registration.reset;
import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient
{
public static final String BASE_URL = "Your URL";
static OkHttpClient httpClient = new OkHttpClient.Builder()
.readTimeout(60L, TimeUnit.SECONDS)
.connectTimeout(60L, TimeUnit.SECONDS)
.addNetworkInterceptor(new Interceptor()
{
public Response intercept(Interceptor.Chain paramAnonymousChain) throws IOException
{
return paramAnonymousChain.proceed(paramAnonymousChain.request().newBuilder().addHeader("test", "test").build());
}
}).addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC)).build();
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL).client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
ApiInterface.java
package in.softwisdom.registration.reset;
import in.softwisdom.registration.model.RegistrationResponse;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
public interface ApiInterface
{
@Multipart
@POST("register.php")
public abstract Call<RegistrationResponse>
register(@Part("name") RequestBody name,
@Part("email") RequestBody email,
@Part("mobile") RequestBody mobile,
@Part("website") RequestBody website,
@Part("facebook") RequestBody fb,
@Part("address") RequestBody address,
@Part("nos_of_stall") RequestBody stall,
@Part MultipartBody.Part image);
}
RegistrationResponse.java
package in.softwisdom.registration.model;
import com.google.gson.annotations.SerializedName;
public class RegistrationResponse{
@SerializedName("result")
private String result;
@SerializedName("success")
private int success;
public void setResult(String result){
this.result = result;
}
public String getResult(){
return result;
}
public void setSuccess(int success){
this.success = success;
}
public int getSuccess(){
return success;
}
@Override
public String toString(){
return
"RegistrationResponse{" +
"result = '" + result + '\'' +
",success = '" + success + '\'' +
"}";
}
}
MainActivity.java
package in.softwisdom.registration;
/*https://www.androidlearning.com/multipart-request-using-android-volley/*/
import android.Manifest;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.Observable;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.CursorLoader;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import de.hdodenhof.circleimageview.CircleImageView;
import in.softwisdom.registration.model.RegistrationModel;
import in.softwisdom.registration.model.RegistrationResponse;
import in.softwisdom.registration.reset.ApiClient;
import in.softwisdom.registration.reset.ApiInterface;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity
{
private Button fab_register;
private Activity activity = MainActivity.this;
private AlertDialog b;
private EditText reg_name, reg_email, reg_mobile, reg_web, reg_fb, reg_adr, reg_stall;
private ImageView reg_camera;
private ImageView reg_profile;
private Button btn_reg;
private String filepath = "";
private String TAG = "Registration";
private String name, email, mobile, website, fb, address, stall;
RequestBody requestFile,descName,descMobile,descEmail,descWebsite,descFb,descAddress,descStall ;
Uri uri;
InputStream is = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getPackageName()));
finish();
startActivity(intent);
return;
}
initComponent();
setListeners();
}
public void initComponent() {
fab_register = (Button) findViewById(R.id.fab_register);
}
public void setListeners() {
fab_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
regDialog();
}
});
}
public void regDialog() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(activity);
LayoutInflater inflater = (activity).getLayoutInflater();
View dialogView = inflater.inflate(R.layout.registration_dialog, null);
dialogBuilder.setView(dialogView);
dialogBuilder.setCancelable(false);
b = dialogBuilder.create();
b.show();
reg_name = dialogView.findViewById(R.id.reg_name);
reg_email = dialogView.findViewById(R.id.reg_email);
reg_mobile = dialogView.findViewById(R.id.reg_mobile);
reg_stall = dialogView.findViewById(R.id.reg_stall);
reg_web = dialogView.findViewById(R.id.reg_website);
reg_fb = dialogView.findViewById(R.id.reg_fb);
reg_adr = dialogView.findViewById(R.id.reg_adr);
reg_camera = dialogView.findViewById(R.id.reg_camera);
reg_profile = dialogView.findViewById(R.id.reg_profile);
btn_reg = dialogView.findViewById(R.id.btn_reg);
btn_reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = reg_name.getText().toString();
email = reg_email.getText().toString();
mobile = reg_mobile.getText().toString();
website = reg_web.getText().toString();
fb = reg_fb.getText().toString();
address = reg_adr.getText().toString();
stall = reg_stall.getText().toString();
if (name.length() == 0) {
reg_name.setError("Enter Name");
return;
}
if (email.length() == 0) {
reg_email.setError("Enter Email");
return;
}
if (mobile.length() == 0) {
reg_mobile.setError("Enter Mobile");
return;
}
if (mobile.length() > 10 || mobile.length() < 10) {
reg_mobile.setError("Mobile No Should Be In 10 Digits");
return;
}
if (website.length() == 0) {
reg_web.setError("Enter Website Link");
return;
}
if (fb.length() == 0) {
reg_fb.setError("Enter Facebook");
return;
}
if (address.length() == 0) {
reg_adr.setError("Enter Address");
return;
}
if (stall.length() == 0) {
reg_stall.setError("Enter No. Of Stall");
return;
}
if (filepath == "") {
Toast.makeText(activity, "Select Profile Image", Toast.LENGTH_SHORT).show();
return;
}
registration();
}
});
reg_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK
&& data != null && data.getData() != null)
{
uri = data.getData();
filepath = getPath(uri);
Log.e("uri==", String.valueOf(uri));
Log.e("file path==", String.valueOf(filepath));
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
reg_profile.setImageBitmap(bitmap);
} catch (IOException e)
{
e.printStackTrace();
}
}
}
public void registration() {
ApiInterface apiInterface=ApiClient.getClient().create(ApiInterface.class);
File file;
//default image
if(filepath.length()==0) {
file = new File(getCacheDir() + "/ic_user.jpeg");
try {
file.createNewFile();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_user);
//Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//select image
else
{
file = new File(String.valueOf(filepath));
}
RequestBody descName = RequestBody.create(MediaType.parse("text/plain"),name);
RequestBody descStall = RequestBody.create(MediaType.parse("text/plain"),stall);
RequestBody descMobile = RequestBody.create(MediaType.parse("text/plain"),mobile);
RequestBody descEmail = RequestBody.create(MediaType.parse("text/plain"),email);
RequestBody descAddress = RequestBody.create(MediaType.parse("text/plain"),address);
RequestBody descWebsite = RequestBody.create(MediaType.parse("text/plain"),website);
RequestBody descFb = RequestBody.create(MediaType.parse("text/plain"),fb);
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part f_path = MultipartBody.Part.createFormData("image_path", file.getPath(),
requestFile);
Call<RegistrationResponse> call=apiInterface.register(descName,descEmail,descMobile,descWebsite,
descFb,descAddress,descStall,f_path);
call.enqueue(new Callback<RegistrationResponse>() {
@Override
public void onResponse(Call<RegistrationResponse> call, Response<RegistrationResponse> response) {
if (response.body().getSuccess() == 1){
Toast.makeText(getApplicationContext(), "File Uploaded Successfully...", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Some error occurred...", Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<RegistrationResponse> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
Log.e(TAG,"onFail==="+t.getMessage());
Log.e(TAG,"onFailString==="+t.toString());
}
});
}
private String getPath(Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
CursorLoader loader = new CursorLoader(getApplicationContext(), contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String result = cursor.getString(column_index);
cursor.close();
return result;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="@drawable/bg"
tools:context=".MainActivity">
<Button
android:id="@+id/fab_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Registration"
android:layout_gravity="center"/>
</FrameLayout>
registration_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/register_design"
android:orientation="vertical">
<EditText
android:id="@+id/reg_name"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="120dp"
android:drawableLeft="@drawable/reg_name"
android:textSize="15dp"
android:paddingLeft="10dp"
android:drawablePadding="15dp"
android:textColorHint="@color/gray"
android:textColor="@color/black"
android:hint="Enter Name"
android:maxLines="1"
android:imeOptions="actionNext"
android:background="@android:color/transparent"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray"/>
<EditText
android:id="@+id/reg_email"
android:layout_width="match_parent"
android:layout_height="40dp"
android:drawableLeft="@drawable/reg_email"
android:textSize="15dp"
android:paddingLeft="10dp"
android:textColorHint="@color/gray"
android:drawablePadding="15dp"
android:textColor="@color/black"
android:maxLines="1"
android:imeOptions="actionNext"
android:background="@android:color/transparent"
android:hint="Enter Email"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray"/>
<EditText
android:id="@+id/reg_mobile"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="15dp"
android:paddingLeft="10dp"
android:drawableLeft="@drawable/reg_phone"
android:drawablePadding="15dp"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:inputType="number"
android:maxLines="1"
android:imeOptions="actionNext"
android:background="@android:color/transparent"
android:hint="Enter Mobile No."/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray"/>
<EditText
android:id="@+id/reg_website"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="15dp"
android:textColorHint="@color/gray"
android:textColor="@color/black"
android:paddingLeft="10dp"
android:drawableLeft="@drawable/reg_web"
android:drawablePadding="15dp"
android:maxLines="1"
android:imeOptions="actionNext"
android:background="@android:color/transparent"
android:hint="Enter Website Link"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray"/>
<EditText
android:id="@+id/reg_fb"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="15dp"
android:textColor="@color/black"
android:paddingLeft="10dp"
android:drawableLeft="@drawable/reg_fb"
android:drawablePadding="15dp"
android:textColorHint="@color/gray"
android:maxLines="1"
android:imeOptions="actionNext"
android:background="@android:color/transparent"
android:hint="Enter Facebook Link"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray"/>
<EditText
android:id="@+id/reg_adr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="4"
android:lines="4"
android:textSize="15dp"
android:paddingLeft="10dp"
android:drawableLeft="@drawable/reg_add"
android:drawablePadding="15dp"
android:textColorHint="@color/gray"
android:textColor="@color/black"
android:background="@android:color/transparent"
android:hint="Address"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/gray"/>
<EditText
android:id="@+id/reg_stall"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="15dp"
android:paddingLeft="10dp"
android:drawableLeft="@drawable/reg_stall"
android:drawablePadding="15dp"
android:textColor="@color/black"
android:textColorHint="@color/gray"
android:inputType="number"
android:maxLines="1"
android:imeOptions="actionDone"
android:background="@android:color/transparent"
android:hint="No. Of Stall"/>
<Button
android:id="@+id/btn_reg"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textAllCaps="false"
android:paddingLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textColorHint="@color/gray"
android:background="@color/colorPrimary"
android:textColor="@color/white"
android:textSize="17dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="20dp"
android:text="Register"/>
</LinearLayout>
<com.github.siyamed.shapeimageview.CircularImageView
android:id="@+id/reg_profile"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/dot_white"
app:siBorderWidth="20dp"
app:siBorderColor="@color/white"
android:src="@drawable/user" />
<ImageView
android:id="@+id/reg_camera"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_marginTop="70dp"
android:layout_marginLeft="30dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/reg_camera"/>
</FrameLayout>
No comments:
Post a Comment