为什么我的图片没有上传到服务器并且Alwyas显示用户没有注册 [英] Why My Image Is Not Upload In Server And Alwyas Show User Not Register

查看:23
本文介绍了为什么我的图片没有上传到服务器并且Alwyas显示用户没有注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 android-studio 将图像上传到服务器,但我不会成功,请帮助我

I'm trying to upload image to server, with android-studio and But I will not succeed Please help me

这是我的代码(所有代码):

here is my code(all code):

java 代码:在这段代码中,我试图从图库中选择图片,然后上传图片

java code: in this codes im trying to select picture from gallery and then uploading image

一段时间后我在 logcat 中显示然后错误显示看起来像图像 javaclass.exception 所以请帮我找出这个问题的解决方案.

after some time i show in logcat then error show look like image javaclass.exception so please help me to find out the solution for this problem.

[error in logcat][1]
[1]: https://i.stack.imgur.com/WMmwN.png

signup.java

public class Sign_up extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
    
        ApiInterface apiInterface;
        Intent myfileintent;
        Spinner spinner,spinner1;// one for course,and another is indurestry types.
        String filepath=" ";
    
    
        //adding spinner list here.
        String[] Course = {"PHP","WORDPRESS","ANDROID","FLUTEER","PYTHON","DIGITAL MARKETING"};
        String[] I_types = {"15 DAYS","INDUSTRIAL VISIT"};
        EditText R_name,R_email,R_phone,R_password,R_cpassword;
        TextView c_pp,c_ab,c_af,c_mm,c_pay,R_af,R_ab,R_mark,R_payment,R_passphoto;
    
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode){
    
                case 10:
                    if (resultCode==RESULT_OK){
                        String path=data.getData().getPath();
                        R_passphoto.setText(path);
                       // uploadImage();
                    }
                    break;
    
                case 20:
                    if (resultCode==RESULT_OK){
                        String path=data.getData().getPath();
                        R_af.setText(path);
                       // uploadImage();
                    }
                    break;
    
                case 30:
                    if (resultCode==RESULT_OK){
                        String path =data.getData().getPath();
                        R_ab.setText(path);
                        //uploadImage();
                    }
                    break;
    
                case 40 :
                    if (resultCode==RESULT_OK){
                        String path =data.getData().getPath();
                        R_mark.setText(path);
                        //uploadImage();
                    }
                    break;
                case 50:
                    if (resultCode==RESULT_OK){
                        String path =data.getData().getPath();
                        R_payment.setText(path);
                       // uploadImage();
                    }
                    break;
                default:
                    Toast.makeText(Sign_up.this,"chosse file correctly",Toast.LENGTH_LONG).show();
    
            }
        }
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sign_up);
            apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
    
            //spinner hookup is here.
            spinner=findViewById(R.id.course_spinner);
            spinner1=findViewById(R.id.spinner4);
    
            //all edittext hookup is here..
            R_name=findViewById(R.id.name);
            R_email=findViewById(R.id.email);
            R_phone=findViewById(R.id.phone);
            R_passphoto=findViewById(R.id.editText2);
            R_af=findViewById(R.id.editTex3);
            R_ab=findViewById(R.id.editTex);
            R_mark=findViewById(R.id.editTex2);
            R_payment=findViewById(R.id.editTex4);
            R_password=findViewById(R.id.pwd);
            R_cpassword=findViewById(R.id.cpwd);
    
            //choose file hookup is here
            c_pp=findViewById(R.id.textView);
            c_af=findViewById(R.id.textView1);
            c_ab=findViewById(R.id.textView2);
            c_mm=findViewById(R.id.textView3);
            c_pay=findViewById(R.id.textView5);
    
            //adding first spinner list
            ArrayAdapter<String> aa = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, Course);
            aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            //first spinner working
            spinner.setAdapter(aa);
            spinner.setOnItemSelectedListener(this);
    
            //adding second spinner list
            ArrayAdapter<String> bb = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, I_types);
            bb.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(bb);
            spinner1.setOnItemSelectedListener(this);
    
            // all file choose control here..
            c_pp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                    myfileintent.setType("*/*");
                    startActivityForResult(myfileintent,10);
                }
            });
            c_af.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                    myfileintent.setType("*/*");
                    startActivityForResult(myfileintent,20);
                }
            });
            c_ab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                    myfileintent.setType("*/*");
                    startActivityForResult(myfileintent,30);
                }
            });
            c_mm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                    myfileintent.setType("*/*");
                    startActivityForResult(myfileintent,40);
                }
            });
            c_pay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myfileintent=new Intent(Intent.ACTION_GET_CONTENT);
                    myfileintent.setType("*/*");
                    startActivityForResult(myfileintent,50);
                }
            });
    
    
    
        }
    
        public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
            Spinner spin = (Spinner)parent;
            Spinner spin2 = (Spinner)parent;
            if(spin.getId() == R.id.course_spinner)
            {
    
                Toast.makeText(this, "Your choose :" + Course[position],Toast.LENGTH_SHORT).show();
            }
            if(spin2.getId() == R.id.spinner4)
            {
                Toast.makeText(this, "Your choose :" + I_types[position],Toast.LENGTH_SHORT).show();
            }
    
        }
    
        public void onNothingSelected(AdapterView<?> parent){
            //Toast.makeText(this, "Choose Countries :", Toast.LENGTH_SHORT).show();
        }
    
    
    
        @Override
        public void onPointerCaptureChanged(boolean hasCapture) {
    
        }
    
    //register working model
        public void registeruser(View view)
        {
            Call<responsemodel> responsemodelCall=apiInterface.registeruser(R_name.getText().toString(),R_email.getText().toString(),R_phone.getText().toString(),spinner.getSelectedItem().getClass().toString(),spinner1.getSelectedItem().getClass().toString(),R_password.getText().toString());
            responsemodelCall.enqueue(new Callback<responsemodel>() {
                @Override
                public void onResponse(Call<responsemodel> call, Response<responsemodel> response) {
                    if (response.body()!=null)
                    {
                        responsemodel responsemodel= response.body();
                        Log.i("Response", String.valueOf(responsemodel.isresponse_code()));
                        if(responsemodel.getStatus().equals("success")){
                            SharedPreferences sp=getSharedPreferences("credentials",MODE_PRIVATE);
                            SharedPreferences.Editor editor=sp.edit();
                            editor.putString("fname",R_name.getText().toString());
                            editor.putString("email",R_email.getText().toString());
                            editor.putString("number",R_phone.getText().toString());
                            editor.putString("cname",spinner.getSelectedItem().toString());
                            editor.putString("intern",spinner1.getSelectedItem().toString());
                            editor.commit();
                            editor.apply();
                            uploadImage();
                            Toast.makeText(Sign_up.this,"user register successfully",Toast.LENGTH_LONG).show();
    
    
                        }
                        else
                        {
                            Toast.makeText(Sign_up.this,"user not register",Toast.LENGTH_LONG).show();
                        }
                    }
                }
    
                @Override
                public void onFailure(Call<responsemodel> call, Throwable t) {
    
                    Toast.makeText(Sign_up.this,"something went wrong",Toast.LENGTH_LONG).show();
    
    
                }
            });
        }
    
        private void uploadImage(){
            File file=new File(filepath);
            File file_ab=new File(filepath);
            File file_m=new File(filepath);
            File file_p=new File(filepath);
            File file_pa=new File(filepath);
            RequestBody requestBody= RequestBody.create(MediaType.parse("*/*"), file);
            RequestBody requestBody1= RequestBody.create(MediaType.parse("*/*"), file_ab);
            RequestBody requestBody2= RequestBody.create(MediaType.parse("*/*"), file_m);
            RequestBody requestBody3= RequestBody.create(MediaType.parse("*/*"), file_p);
            RequestBody requestBody4= RequestBody.create(MediaType.parse("*/*"), file_pa);
            MultipartBody.Part part= MultipartBody.Part.createFormData("filename",file.getName(),requestBody);
            MultipartBody.Part part1= MultipartBody.Part.createFormData("filename1",file_ab.getName(),requestBody1);
            MultipartBody.Part part2= MultipartBody.Part.createFormData("filename2",file_m.getName(),requestBody2);
            MultipartBody.Part part3= MultipartBody.Part.createFormData("filename3",file_p.getName(),requestBody3);
            MultipartBody.Part part4= MultipartBody.Part.createFormData("filename4",file_pa.getName(),requestBody4);
            Retrofit retrofit=ApiClient.getApiClient();
            ApiInterface apiInterface=retrofit.create(ApiInterface.class);
            Call<responsemodel> responsemodelCall=apiInterface.uploadimage(R_af.getText().toString(),R_ab.getText().toString(),R_mark.getText().toString(),R_payment.getText().toString(),R_passphoto.getText().toString(),part,part1,part2,part3,part4);
            responsemodelCall.enqueue(new Callback<responsemodel>() {
                @Override
                public void onResponse(Call<responsemodel> call, Response<responsemodel> response) {
                    if (response.body()!=null)
                    {
                        responsemodel responsemodel= response.body();
                        Log.i("Response", String.valueOf(responsemodel.isresponse_code()));
                        if(responsemodel.getStatus().equals("success")){
    
                            SharedPreferences sp=getSharedPreferences("credentials",MODE_PRIVATE);
                            SharedPreferences.Editor editor=sp.edit();
                            editor.putString("adharfront",R_af.getText().toString());
                            editor.putString("adharback",R_ab.getText().toString());
                            editor.putString("mark",R_mark.getText().toString());
                            editor.putString("pay",R_payment.getText().toString());
                            editor.putString("pass",R_passphoto.getText().toString());
                            editor.commit();
                            editor.apply();
    
                        }
                    }
                }
                @Override
                public void onFailure(Call<responsemodel> call, Throwable t) {
    
                }
            });
        }
    }

apiinterface.java

   @FormUrlEncoded
        @POST("api/register")
        //agar part nahi work kar raha hai to field change kar dena hai..
        Call<responsemodel> registeruser(@Field("Name") String name, @Field("email") String email, @Field("phone") String phone,
                                         @Field("cname") String cname,
                                         @Field("intern") String intern, @Field("password") String password
        );
    
        @Multipart
        @POST("api/register")
        Call<responsemodel>uploadimage(@Part("adharfront")String adharfront, String part, @Part("adharback")String adharback, String part1, @Part("mark")String mark, MultipartBody.Part part2, @Part("pay") MultipartBody.Part pay, MultipartBody.Part part3, @Part("pass") MultipartBody.Part pass, MultipartBody.Part part4);

responsemodel.java

public class  register{
        private String fname;
        private String pass;
        private String adharfront;
        private String adharback;
        private String mark;
        private String pay;
        private String email;
        private String password;
        private String number;
        private String cname;
        private String intern;
        private String date;

        public String getFname(){
            return fname;
        }

        public void setFname(String fname){
            this.fname=fname;
        }

        public String getPass(){
            return pass;
        }

        public void setPass(String pass){
            this.pass=pass;
        }

        public String getAdharfront(){
            return adharfront;
        }

        public void setAdharfront(String adharfront){
            this.adharfront=adharfront;
        }

        public String getAdharback(){
            return adharback;
        }

        public void setAdharback(String adharback){
            this.adharback=adharback;
        }

        public String getMark(){
            return mark;
        }

        public void setMark(String mark){
            this.mark=mark;
        }

        public String getPay(){
            return pay;
        }

        public void setPay(String pay){
            this.pay=pay;
        }

        public String getEmail(){
            return email;
        }

        public void setEmail(String email){
            this.email=email;
        }

        public String getPassword(){
            return password;
        }

        public void setPassword(String password){
            this.password=password;
        }

        public String getNumber(){
            return number;
        }

        public void setNumber(String number){
            this.number=number;
        }

        public String getCname(){
            return cname;
        }

        public void setCname(String cname){
            this.cname=cname;
        }

        public String getIntern(){
            return intern;
        }

        public void setIntern(String intern){
            this.intern=intern;
        }

        public String getDate(){
            return date;
        }

        public void setDate(String date){
            this.date=date;
        }
    }

推荐答案

请更新以下代码.

apiinterface.java

@Multipart
@POST("api/register")
Call<responsemodel>registerWithImages(
        @Part("fname") RequestBody fname,
        @Part("email") RequestBody email,
        @Part("number") RequestBody number,
        @Part("cname") RequestBody cname,
        @Part("intern") RequestBody intern,
        @Part("password") RequestBody password,
        @Part MultipartBody.Part adharfront,
        @Part MultipartBody.Part adharback,
        @Part MultipartBody.Part mark,
        @Part MultipartBody.Part pass
);

signup.java

    private void registerUserWithImage(){
    MediaType text = MediaType.parse("text/plain");

    RequestBody nameBody = RequestBody.create(text, R_name.getText().toString());
    RequestBody emailBody = RequestBody.create(text, R_email.getText().toString());
    RequestBody numberBody = RequestBody.create(text, R_phone.getText().toString());
    RequestBody cnameBody = RequestBody.create(text, spinner.getSelectedItem().getClass().toString());
    RequestBody internBody = RequestBody.create(text, spinner1.getSelectedItem().getClass().toString());
    RequestBody passwordBody = RequestBody.create(text, R_password.getText().toString());

    File file=new File(filepath);
    File file_ab=new File(filepath);
    File file_m=new File(filepath);
    File file_p=new File(filepath);
    File file_pa=new File(filepath);
    RequestBody requestBody= RequestBody.create(MediaType.parse("*/*"), file);
    RequestBody requestBody1= RequestBody.create(MediaType.parse("*/*"), file_ab);
    RequestBody requestBody2= RequestBody.create(MediaType.parse("*/*"), file_m);
    RequestBody requestBody3= RequestBody.create(MediaType.parse("*/*"), file_p);
    RequestBody requestBody4= RequestBody.create(MediaType.parse("*/*"), file_pa);
    MultipartBody.Part part= MultipartBody.Part.createFormData("adharfront",file.getName(),requestBody);
    MultipartBody.Part part1= MultipartBody.Part.createFormData("adharback",file_ab.getName(),requestBody1);
    MultipartBody.Part part2= MultipartBody.Part.createFormData("mark",file_m.getName(),requestBody2);
    MultipartBody.Part part3= MultipartBody.Part.createFormData("pay",file_p.getName(),requestBody3);
    MultipartBody.Part part4= MultipartBody.Part.createFormData("pass",file_pa.getName(),requestBody4);

    Retrofit retrofit=ApiClient.getApiClient();
    ApiInterface apiInterface=retrofit.create(ApiInterface.class);
    Call<responsemodel> responsemodelCall=apiInterface.registerWithImages(
            nameBody,
            emailBody,
            numberBody,
            cnameBody,
            internBody,
            passwordBody,
            part,
            part1,
            part2,
            part3,
            part4
    );
    responsemodelCall.enqueue(new Callback<responsemodel>() {
        @Override
        public void onResponse(Call<responsemodel> call, Response<responsemodel> response) {
            if (response.body()!=null)
            {
                responsemodel responsemodel= response.body();
                Log.i("Response", String.valueOf(responsemodel.isresponse_code()));
                if(responsemodel.getStatus().equals("success")){

                    SharedPreferences sp=getSharedPreferences("credentials",MODE_PRIVATE);
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putString("adharfront",R_af.getText().toString());
                    editor.putString("adharback",R_ab.getText().toString());
                    editor.putString("mark",R_mark.getText().toString());
                    editor.putString("pay",R_payment.getText().toString());
                    editor.putString("pass",R_passphoto.getText().toString());
                    editor.commit();
                    editor.apply();

                }
            }
        }
        @Override
        public void onFailure(Call<responsemodel> call, Throwable t) {

        }
    });
}

希望对你有用!

这篇关于为什么我的图片没有上传到服务器并且Alwyas显示用户没有注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆