How to pass raw data in API request using Retrofit2 [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I am trying to call login API using Retrofit2.
But in onResponse i alwasy get null as response.
Login API endpoint
#FormUrlEncoded
#POST("/api/login/{mobile}")
Call<ResObj> userLogin( #Field("phoneNumber") String mobile );
And the API implementation
private void doLogin(final String mobile){
Call<ResObj> call = userService.login(mobile);
call.enqueue(new Callback<ResObj>() {
#Override
public void onResponse(Call<ResObj> call, Response<ResObj> response) {
ResObj resObj = response.body(); // here i am getting null response.body()
if(resObj.getMessage().equals("true")){
Intent intent = new Intent(Login.this, ListActivity.class);
intent.putExtra("mobile", mobile);
startActivity(intent);
} else{
Toast.makeText(Login.this, "Phone Number is incorrect!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<ResObj> call, Throwable t) {
Toast.makeText(Login.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
ResObj class:
public class ResObj {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
I just want to know what causes the error and what are possible solutions.
UPDATE
POSTMAN

You are getting null response in your login API. It may be due to many reasons. You can check your API is working as expected or not using POSTMAN.
And inside your code, you can prevent this type of exception by checking OBJECT is null or not. like the following.
#Override
public void onResponse(Call<ResObj> call, Response<ResObj> response) {
ResObj resObj = response.body();
if(resObj != null){ // checking object is not null
if(resObj.getStatus()){
Intent intent = new Intent(Login.this, ListActivity.class);
intent.putExtra("mobile", mobile);
startActivity(intent);
} else{
Toast.makeText(Login.this, "Phone Number is incorrect!", Toast.LENGTH_SHORT).show();
}
}else{
// handle null response here.
}
}
Update:
According to your Response JSON, Your Model(ResObj) class should be like the following.
public class ResObj
{
private String date;
private String address;
private String accountName;
private String contactPerson;
private String timeOut;
private String problem;
private String srNo;
private String fieldEngineer;
private String joNo;
private String irNo;
private String designation;
private String email;
private String timeIn;
private String productType;
private boolean status;
private String contactNo;
public String getDate ()
{
return date;
}
public void setDate (String date)
{
this.date = date;
}
public String getAddress ()
{
return address;
}
public void setAddress (String address)
{
this.address = address;
}
public String getAccountName ()
{
return accountName;
}
public void setAccountName (String accountName)
{
this.accountName = accountName;
}
public String getContactPerson ()
{
return contactPerson;
}
public void setContactPerson (String contactPerson)
{
this.contactPerson = contactPerson;
}
public String getTimeOut ()
{
return timeOut;
}
public void setTimeOut (String timeOut)
{
this.timeOut = timeOut;
}
public String getProblem ()
{
return problem;
}
public void setProblem (String problem)
{
this.problem = problem;
}
public String getSrNo ()
{
return srNo;
}
public void setSrNo (String srNo)
{
this.srNo = srNo;
}
public String getFieldEngineer ()
{
return fieldEngineer;
}
public void setFieldEngineer (String fieldEngineer)
{
this.fieldEngineer = fieldEngineer;
}
public String getJoNo ()
{
return joNo;
}
public void setJoNo (String joNo)
{
this.joNo = joNo;
}
public String getIrNo ()
{
return irNo;
}
public void setIrNo (String irNo)
{
this.irNo = irNo;
}
public String getDesignation ()
{
return designation;
}
public void setDesignation (String designation)
{
this.designation = designation;
}
public String getEmail ()
{
return email;
}
public void setEmail (String email)
{
this.email = email;
}
public String getTimeIn ()
{
return timeIn;
}
public void setTimeIn (String timeIn)
{
this.timeIn = timeIn;
}
public String getProductType ()
{
return productType;
}
public void setProductType (String productType)
{
this.productType = productType;
}
public boolean getStatus ()
{
return status;
}
public void setStatus (boolean status)
{
this.status = status;
}
public String getContactNo ()
{
return contactNo;
}
public void setContactNo (String contactNo)
{
this.contactNo = contactNo;
}
}
You are passing parameter as raw data(according to your screen-shot). So your API endpoint would be like below.
#Headers("Content-Type: application/json")
#POST("/api/login")
Call<ResObj> userLogin(#Body JsonObject jsonObject);
And call your API like this
private void doLogin(final String mobile){
try {
JsonObject paramObject = new JsonObject();
paramObject.addProperty("mobile", mobile);
} catch (JSONException e) {
e.printStackTrace();
}
Call<ResObj> call = userService.login(paramObject);
call.enqueue(new Callback<ResObj>() {
//your rest of code
});
}
UPDATE-2:
To send object from one Activity to another using intent you have to make your model class Percelable. like this
// implements Parcelable
public class ResObj implements Parcelable {
// ...........your previous code here
// just simply add the following methods
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(date);
dest.writeString(address);
dest.writeString(accountName);
dest.writeString(contactPerson);
dest.writeString(timeOut);
dest.writeString(problem);
dest.writeString(srNo);
dest.writeString(fieldEngineer);
dest.writeString(joNo);
dest.writeString(irNo);
dest.writeString(designation);
dest.writeString(email);
dest.writeString(timeIn);
dest.writeString(productType);
dest.writeByte((byte) (status ? 1 : 0));
dest.writeString(contactNo);
}
public static final Parcelable.Creator<ResObj> CREATOR
= new Parcelable.Creator<ResObj>() {
public ResObj createFromParcel(Parcel in) {
return new ResObj(in);
}
public ResObj[] newArray(int size) {
return new ResObj[size];
}
};
protected ResObj(Parcel in) {
date = in.readString();
address = in.readString();
accountName = in.readString();
contactPerson = in.readString();
timeOut = in.readString();
problem = in.readString();
srNo = in.readString();
fieldEngineer = in.readString();
joNo = in.readString();
irNo = in.readString();
designation = in.readString();
email = in.readString();
timeIn = in.readString();
productType = in.readString();
status = in.readByte() != 0;
contactNo = in.readString();
}
}
Now pass your object via intent like the following.
if(resObj != null){
if(resObj.getStatus()){
Intent intent = new Intent(Login.this, ListActivity.class);
intent.putExtra("your_key", resObj); // pass resObj and use same key to get data
startActivity(intent);
} else{
Toast.makeText(Login.this, "Phone Number is incorrect!", Toast.LENGTH_SHORT).show();
}
}
Get data from your ListActivity like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final ResObj yourObject = getIntent().getParcelableExtra("your_key"); // make sure you use same key like data.
// Now you can use your data like that
yourEditText.setText(yourObject.getEmail());
}

Related

how to upload file from retrofit android java

am using retrofit for insert data to my webservice, I have made it before but without uploading the image and the insert is successful, the input field through the model class not in interface,how I add an input field fot uploading files through the model so that it can be sent to my web services storage folder?
I have tried but failed please help
for my insert in activity
btnsubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tanggal = textdate.getText().toString();
SimpleDateFormat formatter1=new SimpleDateFormat("dd/MM/yyyy");
Date date1= null;
try {
date1 = formatter1.parse(tanggal);
} catch (ParseException e) {
e.printStackTrace();
}
SwabtestModel sw = new SwabtestModel();
sw.sethasil(texthasil.getText().toString());
sw.settanggal(date1);
sw.settempat(texttempat.getText().toString());
sw.setuserid(Integer.valueOf(txtuserid.getText().toString()));
sw.setFile_name(new File(txturi.getText().toString()));
save(sw);
}
});
public void save(SwabtestModel sw){
Call<SwabtestModel> call = swabtestService.addswab(sw);
call.enqueue(new Callback<SwabtestModel>() {
#Override
public void onResponse(Call<SwabtestModel> call, Response<SwabtestModel> response) {
if(response.isSuccessful()){
String status = response.body().getStatus();
Toast.makeText(SwabtestActivity.this, status, Toast.LENGTH_LONG).show(); }
}
#Override
public void onFailure(Call<SwabtestModel> call, Throwable t) {
Log.e("ERROR: ", t.getMessage());
}
});
}
for my file chooser
public void onActivityResult(int request_code, int result_code, Intent data){
super.onActivityResult(request_code,result_code,data);
if(request_code==request_code && result_code== Activity.RESULT_OK){
if(data==null){
return;
}
uri= data.getData();
filePath = uri.getPath();
txturi.setText(filePath);
}
}
public void openfilechooser(){
Intent intent= new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent,request_code);
}
for my model class
public class SwabtestModel {
#SerializedName("hasil")
#Expose
private String hasil;
#SerializedName("tanggal")
#Expose
private Date tanggal;
#SerializedName("tempat")
#Expose
private String tempat;
#SerializedName("file_name")
#Expose
private File file_name;
#SerializedName("user_id")
#Expose
private Integer user_id;
String data;
String status;
public SwabtestModel(String hasil, Date tanggal, String tempat){
this.hasil = hasil;
this.tanggal = tanggal;
this.tempat = tempat;
}
public void sethasil(String hasil) {
this.hasil = hasil;
}
public String gethasil(){
return hasil;
}
public void settanggal(Date tanggal) {
this.tanggal = tanggal;
}
public Date gettanggal(){
return tanggal;
}
public void settempat(String tempat) {
this.tempat = tempat;
}
public String gettempat(){
return tempat;
}
public void setuserid(Integer user_id) {
this.user_id = user_id;
}
public Integer getuserid(){
return user_id;
}
public void setFile_name( File file_name) {
this.file_name =file_name ;
}
public File getfilename(){
return file_name;
}
public String getData() {
return data;
}
public String getStatus() {
return status;
}
}
my interface
public interface swabtestService
{
#GET("hasil-antigen-list")
Call<List<SwabtestModel>> getUsers();
#POST("insert-hantigen")
Call<SwabtestModel> addswab(#Body SwabtestModel swabtest);
}
To upload files you should use Multipart, Please refer to this post for example and please ping me if you have any queries https://stackoverflow.com/a/39953566
Take a data in list like #Part List<MultipartBody.Part> partFile
private List<MultipartBody.Part> getMapPartListSave(List<PojoAttachDocList> fields) {
List<MultipartBody.Part> mapPart = new ArrayList<>();
for (int i = 0; i < fields.size(); i++) {
**PojoAttachDocList** attachDoc = fields.get(i);
if (!attachDoc.isAttached() && attachDoc.getDocFile() != null && attachDoc.getDocFile().exists()
&& attachDoc.getDocFile().length() > 0) {
String fileParam = PARAMS_DOCUMENT + "[" + i + "]";
mapPart.add(MultipartBody.Part.createFormData(fileParam, attachDoc.getDocFile().getName(),
RequestBody.create(MediaType.parse("*/*"), attachDoc.getDocFile())));
}
}
return mapPart;
}
Convert it to MultipartBody

Retrofit response body null but status code is 200

I want to make a fitur to login, I want to make a feature to log in, initially I saw the response code shows 200, but when I saw the response body was null. I can still log in, but the logged in user data isn't saved. I used 2 db MySql and SQLite and im using retrofit and shared preference. And here my code
API Interface
public interface Api {
#FormUrlEncoded
#POST(Config.API_LOGIN_USER)
Call<UserOrtu> loginUser(
#Field("email") String email,
#Field("password") String password
);
PrefUtil.java
public class PrefUtil {
public static final String USER_SESSION = "user_session";
public static final String USER_STORAGE = "user_storage";
public static SharedPreferences getSharedPreferences(Context ctx){
return PreferenceManager.getDefaultSharedPreferences(ctx);
}
public static void putUser(Context ctx, String key, UserOrtu user){
Gson gson = new Gson();
String json = gson.toJson(user);
putString(ctx, key, json);
}
public static UserOrtu getUser(Context ctx, String key){
Gson gson = new Gson();
String json = getString(ctx, key);
UserOrtu user = gson.fromJson(json, UserOrtu.class);
return user;
}
public static void putString(Context ctx, String key, String value){
getSharedPreferences(ctx).edit().putString(key, value).apply();
}
public static String getString(Context ctx, String key){
return getSharedPreferences(ctx).getString(key, null);
}
public static void clear(Context ctx) {
getSharedPreferences(ctx).edit().clear().apply();
}
}
Models
UserOrtu.java
public class UserOrtu {
#SerializedName("id_user")
#Expose
private int idUser;
#SerializedName("nama")
#Expose
private String nama;
#SerializedName("email")
#Expose
private String email;
#SerializedName("password")
#Expose
private String password;
#SerializedName("error")
#Expose
private Boolean error;
#SerializedName("message")
#Expose
private String message;
public int getIdUser() {
return idUser;
}
public void setIdUser(int idUser) {
this.idUser = idUser;
}
public String getNama() {
return nama;
}
public void setNama(String nama) {
this.nama = nama;
}
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 Boolean getError() {
return error;
}
public void setError(Boolean error) {
this.error = error;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
SignInActivity.java
public class SignInActivity extends AppCompatActivity {
#BindView(R.id.input_email_signin)
TextInputEditText etEmail;
#BindView(R.id.text_register)
TextView tvRegister;
#BindView(R.id.input_password_signin)
TextInputEditText etPassword;
EmailValidator emailValidator;
PasswordValidator passwordValidator;
Context context;
private String email;
private String password;
private Api mApi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isSessionLogin()){
startActivity(new Intent(this, MainActivity.class));
this.finish();
}
setContentView(R.layout.activity_sign_in);
ButterKnife.bind(this);
AndroidThreeTen.init(this);
mApi = RetrofitBuilder.builder(this).create(Api.class);
}
boolean isEmail(EditText text){
emailValidator = new EmailValidator();
String email = text.getText().toString();
return emailValidator.isValid(email);
}
boolean isPassword(EditText text){
passwordValidator = new PasswordValidator();
String pass = text.getText().toString();
return passwordValidator.isValid(pass);
}
#OnClick(R.id.text_register) void toRegister(){
Intent intent = new Intent(this, SignUpActivity.class);
startActivity (intent);
}
#OnClick(R.id.btn_signin) void onLogin(){
if(isEempty(etEmail)){
etEmail.setError("Email harus diisi");
}else if(isEempty(etPassword)){
etPassword.setError("Password harus diisi");
}else if(!isEmail(etEmail)){
etEmail.setError("Email tidak valid");
}else if(!isPassword(etPassword)){
String str = passwordValidator.getString();
Toast.makeText(getApplicationContext(),str, Toast.LENGTH_SHORT).show();
}else {
loginAct();
}
}
void loginAct(){
email = etEmail.getText().toString();
password = etPassword.getText().toString();
final MaterialDialog dialog = DialogBuilder.showLoadingDialog(SignInActivity.this, "Updating Data", "Please wait..", false);
mApi.loginUser(email, password).enqueue(new Callback<UserOrtu>() {
#Override
public void onResponse(Call<UserOrtu> call, Response<UserOrtu> response) {
UserOrtu user = response.body();
Log.i("USER_LOGIN", response.message());
if (user != null){
//Masih error disini
//if (!user.getError()){
PrefUtil.putUser(getApplicationContext(), PrefUtil.USER_SESSION, user);
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
//this.finish();
//}
Toast.makeText(getApplicationContext(), user.getMessage(), Toast.LENGTH_SHORT).show();
}
if (response.code() == 403){
etPassword.requestFocus();
etPassword.setError(getString(R.string.error_password));
}
if (response.code() == 404){
etEmail.requestFocus();
etEmail.setError(getString(R.string.error_login));
}
dialog.dismiss();
}
#Override
public void onFailure(Call<UserOrtu> call, Throwable t) {
//Toast.makeText(getActivity(), t.getMessage(), Toast.LENGTH_SHORT).show();
dialog.dismiss();
Log.i("USER_LOGIN", t.getMessage());
DialogBuilder.showErrorDialog(SignInActivity.this, "Gagal Login");
}
});
}
// this method to check is user logged in ?
boolean isSessionLogin(){
return PrefUtil.getUser(getApplicationContext(), PrefUtil.USER_SESSION) != null;
}
}
And here's my result
enter image description here
Here's my JSON request
enter image description here
Your model is not representing same data as in JSON. With UserOrtu class it will works only if your response will have structure like below:
{
"id":1,
"nama": "Name",
"email":"email",
"message":"msg"
}
But as you can see 3 first fields are inside another object data. So your model should look more like:
class LoginResponse{
#SerializedName("data")
#Expose
private UserOrtu userData;
#SerializedName("message")
#Expose
private String message;
}

how to convert string into Json and extrat from it info

I'm using retrofit2 and Rxjava2 to insert/get information from mongodb and nodeJs server, for now, I receive all data as a string but I want to get hole collection Infos from my base so I need to convert string to JSON and get each information.
My code to receive data:
1- Service:
#POST("collect/get")
#FormUrlEncoded
Observable<String> getcollection(#Field("selector") String selector);
2-RetrofitClient:
if(instance == null){
instance = new Retrofit.Builder()
.baseUrl("http://transportor.ddns.net:3000/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create()).build();
}
3- Recieve function
private void getallcollection(String selector) {
compositeDisposable.add(myServices.getcollection(selector)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>(){
#Override
public void accept(String s) throws Exception {
Log.d("infos",s);
}
}));
}
I'm already prepared Collection class:
public class col {
private String creator;
private String emailcol;
private String date_creation_col;
private String nom_col;
private String long_col;
private String lat_col;
private String tel_fix_col;
private String tel_mobile_col;
private String creatorcreator;
private String heure_matin_col;
private String heure_apresmatin_col;
private String type;
private String imagePath;
public col(String creator, String emailcol, String date_creation_col, String nom_col, String long_col, String lat_col, String tel_fix_col, String tel_mobile_col, String creatorcreator, String heure_matin_col, String heure_apresmatin_col, String type, String imagePath) {
this.creator = creator;
this.emailcol = emailcol;
this.date_creation_col = date_creation_col;
this.nom_col = nom_col;
this.long_col = long_col;
this.lat_col = lat_col;
this.tel_fix_col = tel_fix_col;
this.tel_mobile_col = tel_mobile_col;
this.creatorcreator = creatorcreator;
this.heure_matin_col = heure_matin_col;
this.heure_apresmatin_col = heure_apresmatin_col;
this.type = type;
this.imagePath = imagePath;
}
public String getCreator() {
return creator;
}
public void setCreator(String creator) {
this.creator = creator;
}
public String getEmailcol() {
return emailcol;
}
public void setEmailcol(String emailcol) {
this.emailcol = emailcol;
}
public String getDate_creation_col() {
return date_creation_col;
}
public void setDate_creation_col(String date_creation_col) {
this.date_creation_col = date_creation_col;
}
public String getNom_col() {
return nom_col;
}
public void setNom_col(String nom_col) {
this.nom_col = nom_col;
}
public String getLong_col() {
return long_col;
}
public void setLong_col(String long_col) {
this.long_col = long_col;
}
public String getLat_col() {
return lat_col;
}
public void setLat_col(String lat_col) {
this.lat_col = lat_col;
}
public String getTel_fix_col() {
return tel_fix_col;
}
public void setTel_fix_col(String tel_fix_col) {
this.tel_fix_col = tel_fix_col;
}
public String getTel_mobile_col() {
return tel_mobile_col;
}
public void setTel_mobile_col(String tel_mobile_col) {
this.tel_mobile_col = tel_mobile_col;
}
public String getCreatorcreator() {
return creatorcreator;
}
public void setCreatorcreator(String creatorcreator) {
this.creatorcreator = creatorcreator;
}
public String getHeure_matin_col() {
return heure_matin_col;
}
public void setHeure_matin_col(String heure_matin_col) {
this.heure_matin_col = heure_matin_col;
}
public String getHeure_apresmatin_col() {
return heure_apresmatin_col;
}
public void setHeure_apresmatin_col(String heure_apresmatin_col) {
this.heure_apresmatin_col = heure_apresmatin_col;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
}
Actually I received all data and console show me : [{"_id":"5e22074673c926147c3a73f5","date_creation_col":"17-01-2020","creator":"Alaeddine","emailcol":"amir#gmail.com","nom_col":"amir","long_col":"10.179326869547367","lat_col":"36.83353893150942","tel_fix_col":"123","tel_mobile_col":"1234","adress_col":"rue Paris mision 34","heure_matin_col":"7","heure_apresmatin_col":"5","type":"collection","imagePath":"mmmmmmmmmmmm"}]
I want to know how to extract for example creator from this Json.
You can use a third-party JSON parser, like Google GSON, as you're already developing for Android. Java does not seem to contain a built-in JSON parser.
See this answer.

retrofit returning object params out of order, with # sign and quotes

I'm using retrofit to send a call to an api to post an issue in a bug-tracker through my android app. The API needs it formatted a specific way, so I created a custom object to pass as a parameter. Unfortunately when I'm sending the request to the API its formatting it incorrectly. It alphabetizes the parameters, puts quotes around both the keys and the values, and adds an # to the beginning of my data, leading the API to not be able to make sense of what I'm trying to post. The format I'm looking to send is:
{ fb_user_id: dummyUID,
email: dummy#email.com,
first_name: John,
last_name: Smith,
project: android,
type: bug,
subject: title,
description: description }
Instead of the above, my api is receiving the data as
#"description":"description",
"email":"dummy#email.com",
"fb_user_id":"dummyUID",
"first_name":"John",
"last_name":"Smith",
"project":"android",
"subject":"title",
"type":"bug"
I'm using retrofit to do so, with my interface looking like
public interface MyAPI {
#POST("/op_create_ticket")
Observable<JsonElement> createTicket(#Body Ticket ticket);
}
And my activity calling it looking like:
public class HelpActivity extends BaseActivity {
#BindView(R.id.textView)
TextView textView;
#BindView(R.id.spinner)
Spinner spinner;
#BindView(R.id.email)
EditText email;
#BindView(R.id.firstName)
EditText firstName;
#BindView(R.id.lastName)
EditText lastName;
#BindView(R.id.subject)
EditText title;
#BindView(R.id.description)
EditText description;
#BindView(R.id.button3)
Button button3;
FirebaseAuth fbauth = FirebaseAuth.getInstance();
FirebaseUser user = fbauth.getCurrentUser();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_help);
ButterKnife.bind(this);
}
#OnClick(R.id.button3)
public void onViewClicked() {
final GsonBuilder gsonBuilder = new GsonBuilder();
final Gson gson = gsonBuilder.create();
Retrofit.Builder builder = new Retrofit.Builder();
builder.baseUrl("https://my.url");
builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
builder.addConverterFactory(GsonConverterFactory.create());
Retrofit retrofit = builder.build();
MyAPI api = retrofit.create(TriggerWatchAPI.class);
Ticket ticket = new Ticket();
ticket.setFb_user_id(user.getUid());
ticket.setEmail(email.getText().toString());
ticket.setFirst_name(firstName.getText().toString());
ticket.setLast_name(lastName.getText().toString());
ticket.setProject("android");
ticket.setType("design");
ticket.setSubject(title.getText().toString());
ticket.setDescription(description.getText().toString());
api.createTicket(ticket).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<JsonElement>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(JsonElement jsonElement) {
Log.d(getTag(), "zzzOnNext");
}
#Override
public void onError(Throwable e) {
Log.e(getTag(), "err", e);
}
#Override
public void onComplete() {
}
});
}
}
I'm wondering if there's any kind of converter or something that can convert my object to the kind of data the API is looking for.
edit: forgot to add my custom pojo
package watch.trigger.Model;
public class Ticket {
private String project;
private String first_name;
private String email;
private String description;
private String subject;
private String last_name;
private String fb_user_id;
private String type;
public String getProject ()
{
return project;
}
public void setProject (String project)
{
this.project = project;
}
public String getFirst_name ()
{
return first_name;
}
public void setFirst_name (String first_name)
{
this.first_name = first_name;
}
public String getEmail ()
{
return email;
}
public void setEmail (String email)
{
this.email = email;
}
public String getDescription ()
{
return description;
}
public void setDescription (String description)
{
this.description = description;
}
public String getSubject ()
{
return subject;
}
public void setSubject (String subject)
{
this.subject = subject;
}
public String getLast_name ()
{
return last_name;
}
public void setLast_name (String last_name)
{
this.last_name = last_name;
}
public String getFb_user_id ()
{
return fb_user_id;
}
public void setFb_user_id (String fb_user_id)
{
this.fb_user_id = fb_user_id;
}
public String getType ()
{
return type;
}
public void setType (String type)
{
this.type = type;
}
#Override
public String toString()
{
return "ClassPojo [project = "+project+", first_name = "+first_name+", email = "+email+", description = "+description+", subject = "+subject+", last_name = "+last_name+", fb_user_id = "+fb_user_id+", type = "+type+"]";
}
}
don't send a custom object, send a JSON object as a String and in the API receive it as a JSON object.
first add this in your gradle:
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.retrofit2:converter-scalars:2.3.0'
1- change this
public interface MyAPI {
#Headers("Content-Type: application/json")
#POST("/op_create_ticket")
Observable<JsonElement> createTicket(#Body Ticket ticket);
}
to :
public interface MyAPI {
#POST("/op_create_ticket")
Observable<String> createTicket(#Body String ticket);
}
2- change this
Ticket ticket = new Ticket();
ticket.setFb_user_id(user.getUid());
ticket.setEmail(email.getText().toString());
ticket.setFirst_name(firstName.getText().toString());
ticket.setLast_name(lastName.getText().toString());
ticket.setProject("android");
ticket.setType("design");
ticket.setSubject(title.getText().toString());
ticket.setDescription(description.getText().toString());
api.createTicket(ticket).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<JsonElement>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(JsonElement jsonElement) {
Log.d(getTag(), "zzzOnNext");
}
#Override
public void onError(Throwable e) {
Log.e(getTag(), "err", e);
}
#Override
public void onComplete() {
}
});
to
JSONObject tickenJson= new JSONObject();
Ticket ticket = new Ticket();
ticket.setFb_user_id(user.getUid());
ticket.setEmail(email.getText().toString());
ticket.setFirst_name(firstName.getText().toString());
ticket.setLast_name(lastName.getText().toString());
ticket.setProject("android");
ticket.setType("design");
ticket.setSubject(title.getText().toString());
ticket.setDescription(description.getText().toString());
ticketJson.put("fb_user_id",user.getUid());
ticketJson.put("email",email.getText().toString());
ticketJson.put("first_name",firstName.getText().toString());
ticketJson.put("last_name",lastName.getText().toString());
ticketJson.put("project","android");
ticketJson.put("type","design");
ticketJson.put("type",title.getText().toString());
ticketJson.put("description",description.getText().toString());
api.createTicket(ticketJson.toString()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<JsonElement>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(JsonElement jsonElement) {
Log.d(getTag(), "zzzOnNext");
}
#Override
public void onError(Throwable e) {
Log.e(getTag(), "err", e);
}
#Override
public void onComplete() {
}
});

Signup with jwt token in android Retrofit

I want to register a user with their token, I want to register a user using retrofit in android but I keep getting this error:
ERROR::: Attempt to invoke virtual method 'void com.signup.User.setUsername(java.lang.String)' on a null object reference
Here is my code:
public class Session {
Context context;
private SharedPreferences prefs;
public Session(Context cntx) {
// TODO Auto-generated constructor stub
this.context = cntx;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
}
public void setJwtToken(String token) {
prefs.edit().putString("JwtToken", token).commit();
}
public String getJwtToken() {
String token = prefs.getString("JwtToken", "");
if (token == null || token.isEmpty()) {
token = "Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjIxNzc0NTI3OTksImlhdCI6MTUxNjAyMjk5OSwiaXNzIjoiQmFzb2JhYXMgTmVwYWwiLCJuYmYiOjE1MTYwMjI5OTksImp0aSI6Ikd1ZXN0VG9rZW4iLCJzdWIiOjB9.QikmNgBYmqch5HREGFEpUs4Xk3x-zFfDg5mhYJO7jM8";
}
return token;
}
}
public interface ApiInterface {
#POST("/api/users/signup")
Call<ResponseBody> signMeUp(#Header("Authorization") String token ,#Body User user);
}
public class MainActivity extends AppCompatActivity {
private EditText et_name, et_address, et_phone, et_username, et_email, et_password, et_confipassword;
private Button register;
private User user;
private SharedPreferences prefs;
private ApiInterface apiInterface;
private Session session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = findViewById(R.id.edit_text_name);
et_address = findViewById(R.id.edit_text_address);
et_phone = findViewById(R.id.edit_text_phonenumber);
et_username = findViewById(R.id.edit_text_username);
et_email = findViewById(R.id.edit_text_email);
et_password = findViewById(R.id.edit_text_password);
et_confipassword = findViewById(R.id.edit_text_confirm_password);
register = findViewById(R.id.signupButton);
register.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Login();
}
});
}
private void Login() {
user.setUsername(et_name.getText().toString());
user.setAddress(et_address.getText().toString());
user.setPhone(et_phone.getText().toString());
user.setName(et_name.getText().toString());
user.setEmail(et_email.getText().toString());
user.setPassword(et_password.getText().toString());
user.setPasswordConfirmation(et_confipassword.getText().toString());
signupUser(user);
}
private void signupUser(final User user) {
// Set up progressbar before call
apiInterface = ApiClient.getClient().create(ApiInterface.class);
Call<ResponseBody> call1 = apiInterface.signMeUp(session.getJwtToken(),user);
final Gson gson = new Gson();
final String json = gson.toJson(user);
call1.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.code() == 201) {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
//Starting main activity after user sees dialog
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else if (response.code() == 500) {
try {
JSONObject jsonObject = new JSONObject(response.errorBody().string());
Log.e("SignupFragment", jsonObject.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else
Log.e("SignupFragment", response.raw().toString());
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
}
}
public class User {
#SerializedName("name")
#Expose
private String name;
#SerializedName("email")
#Expose
private String email;
#SerializedName("password")
#Expose
private String password;
#SerializedName("password_confirmation")
#Expose
private String passwordConfirmation;
#SerializedName("image")
#Expose
private String image;
#SerializedName("phone")
#Expose
private String phone;
#SerializedName("address")
#Expose
private String address;
#SerializedName("username")
#Expose
private String username;
#SerializedName("pan_no")
#Expose
private String panNo;
#SerializedName("birthday")
#Expose
private String birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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 getPasswordConfirmation() {
return passwordConfirmation;
}
public void setPasswordConfirmation(String passwordConfirmation) {
this.passwordConfirmation = passwordConfirmation;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPanNo() {
return panNo;
}
public void setPanNo(String panNo) {
this.panNo = panNo;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
}
public class ApiClient {
public static final String BASE_URL = "https://myapp.herokuapp.com/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
ApiInterface apiInterface=retrofit.create(ApiInterface.class);
}
I want to signup a user via my app but I keep getting the error. And, if I remove the auth from header from my Interface I get a message asking a token, and when I provide a token it give me a null object reference error !!
You should instantiate variable before access it: User user = new User();
Update: do it in void Login()
create an instance of User inside onCreate method or up there where it's declared
Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
user = new User();
session = new Session(this);
}

Categories

Resources