改造中的onResponse中出现错误

时间:2018-07-05 06:00:00

标签: android retrofit2

我是android世界的新手。我试图为我的学校项目开发一个应用程序,却偶然发现了这个问题。请有人帮我。 我的片段代码如下。我想在其中用Image填写表格并上传到PHP,Mysql服务器进行注册。但是该应用程序崩溃了。

package com.dgdev.mtmicds;


import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.dgdev.mtmicds.DbAccess.Remote.APIClient;
import com.dgdev.mtmicds.DbAccess.Remote.ApiInterface;
import com.dgdev.mtmicds.DbAccess.Remote.UserRegistrationModel;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static android.app.Activity.RESULT_OK;
import static android.provider.MediaStore.Images.Media.*;
import static android.support.v4.content.PermissionChecker.checkSelfPermission;


/**
 * A simple {@link Fragment} subclass.
 */
public class ProfileFragment extends Fragment {
    View view;
    AppCompatImageView imageView;
    AppCompatEditText etFullname, etEmail, etDob, etMobile, etPsw, etRePsw, etAddr;
    AppCompatButton btnRegister, btnCancel;
    private static final int IMAGE_REQUEST = 7777;
    Bitmap bitmap;

    public ProfileFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_profile, container, false);

        imageView = (AppCompatImageView) view.findViewById(R.id.ProfileDP);
        etFullname = (AppCompatEditText) view.findViewById(R.id.tvfullanme);
        etEmail = (AppCompatEditText) view.findViewById(R.id.tvemail);
        etDob = (AppCompatEditText) view.findViewById(R.id.tvdob);
        etPsw = (AppCompatEditText) view.findViewById(R.id.tvpsw);
        etRePsw = (AppCompatEditText) view.findViewById(R.id.tvpsw_re);
        etAddr = (AppCompatEditText) view.findViewById(R.id.tvaddr);
        etMobile = (AppCompatEditText) view.findViewById(R.id.tvmobile);

        btnRegister = (AppCompatButton) view.findViewById(R.id.btnRegister);
        btnCancel = (AppCompatButton) view.findViewById(R.id.btnCancel);


        /*-----------------------------------------------------------------------------*/
        /* this onClickListener will be responsible for getting image URI from gallery */
        /*-----------------------------------------------------------------------------*/
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectImageFromGallery();
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                uploadData();
            }
        });
        return view;
    }

    private void uploadData() {
        String fullname = etFullname.getText().toString();
        String dob = convertTOMysqlDate(etDob.getText().toString());
        String mobile = etMobile.getText().toString();
        String addr = etAddr.getText().toString();
        String psw = etPsw.getText().toString();
        String prof_pic = imageToString();
        Toast.makeText(view.getContext(), dob, Toast.LENGTH_LONG).show();
        ApiInterface apiInterface = APIClient.GetClient().create(ApiInterface.class);
        Call<UserRegistrationModel> call = apiInterface.RegisterUser(fullname, dob, mobile, addr, psw, prof_pic);
        call.enqueue(new Callback<UserRegistrationModel>() {
            @Override
            public void onResponse(Call<UserRegistrationModel> call, Response<UserRegistrationModel> response) {
                UserRegistrationModel res = response.body();
                Toast.makeText(view.getContext(), res.getStatus(), Toast.LENGTH_LONG).show();
            }
            @Override
            public void onFailure(Call<UserRegistrationModel> call, Throwable t) {
                Toast.makeText(view.getContext(), "You are not able to talk to server!", Toast.LENGTH_LONG).show();
            }
        });
    }

    private String convertTOMysqlDate(String s) {
        String $MysqlDateString;
        String[] DateParts = s.split("/");
        $MysqlDateString = DateParts[2] + "-" + DateParts[1] + "-" + DateParts[0];
        return $MysqlDateString;
    }

    private void selectImageFromGallery() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, IMAGE_REQUEST);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK && data != null) {
            Uri path = data.getData();
            try {
                bitmap = getBitmap(getActivity().getApplicationContext().getContentResolver(), path);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    private String imageToString() {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, outputStream);
        byte[] ImageBytes = outputStream.toByteArray();
        return Base64.encodeToString(ImageBytes, Base64.DEFAULT);
    }
}

我在logcat中收到波纹管消息

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.dgdev.mtmicds.DbAccess.Remote.UserRegistrationModel.getStatus()' on a null object reference
        at com.dgdev.mtmicds.ProfileFragment$3.onResponse(ProfileFragment.java:105)

请帮助我...我是新手...

2 个答案:

答案 0 :(得分:0)

尝试一下

@Override
        public void onResponse(Call<UserRegistrationModel> call, Response<UserRegistrationModel> response) {
            if(response.isSuccessful()) {
                UserRegistrationModel res = response.body();
                if(res!=null) {
                    Toast.makeText(view.getContext(), res.getStatus(), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(view.getContext(), "Didn't get response", Toast.LENGTH_LONG).show();
                }
            }
        }

答案 1 :(得分:-1)

您的com.dgdev.mtmicds.DbAccess.Remote.UserRegistrationModel为空。通过进入调试模式来检查是否为其分配了任何值。在调试模式下逐步运行代码,并检查其初始化方式。

提示:在调试器中检查代码的这一行:

perror