1

My question based on this answers,
https://stackoverflow.com/a/45233401/4574163
but my result is strange..

My code like this :

activity_main.xml :

<LinearLayout android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView
        android:id="@+id/imgLogin"
        android:layout_width="200dp"
        android:layout_height="150dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:padding="5dp"
        android:background="@mipmap/ic_launcher_round"
        />

    <TextView
        android:id="@+id/txtLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imgLogin"
        android:layout_centerHorizontal="true"
        android:text="Holostik Track and Trace"
        android:textSize="20dp"
        android:visibility="gone" />

        <EditText
            android:id="@+id/edtEmail"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:ems="10"
            android:fontFamily="sans-serif"
            android:gravity="top"
            android:hint="Login ID"
            android:maxLines="10"
            android:paddingLeft="@dimen/edit_input_padding"
            android:paddingRight="@dimen/edit_input_padding"
            android:paddingTop="@dimen/edit_input_padding"
            android:singleLine="true"/>


    <EditText
        android:id="@+id/edtPass"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:fontFamily="sans-serif"
        android:hint="Password"
        android:inputType="textPassword"
        android:paddingLeft="@dimen/edit_input_padding"
        android:paddingRight="@dimen/edit_input_padding"
        android:paddingTop="@dimen/edit_input_padding"
        android:singleLine="true" />


    <RelativeLayout
        android:id="@+id/rel12"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textInputLayout2"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        >

        <Button
            android:id="@+id/loginSub"
            android:layout_width="wrap_content"
            android:layout_height="45dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@color/colorPrimary"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:layout_marginRight="10dp"
            android:text="Login"
            android:textColor="#ffffff" />
    </RelativeLayout>

</LinearLayout>

My partial MainActivity.java :

Call<LoginResponse> call1 = apiInterface.validateUser(Uname, Upass);
call1.enqueue(new Callback<LoginResponse>() {
    @Override
    public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
        LoginResponse loginResponse = response.body();

        Log.e("mulyo", "call 1 --> " + call.toString());
        Log.e("mulyo", "loginResponse 1 --> " + response.raw());
        Log.e("mulyo", "loginResponse 2 --> " + response.message());
        Log.e("mulyo", "loginResponse 3 --> " + response.isSuccessful());
        Log.e("mulyo", "loginResponse 4 --> " + response.code());
        if (loginResponse != null) {
            Log.e("mulyo", "getUserId          -->  " + loginResponse.getUid());
            Log.e("mulyo", "getFirstName       -->  " + loginResponse.getUname());
            Log.e("mulyo", "getLastName        -->  " + loginResponse.getUpass());

            String responseCode = loginResponse.getUname();
            if (responseCode != null && responseCode.equals("404")) {
                Toast.makeText(MainActivity.this, "Invalid Login Details \n Please try again", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "Welcome " + loginResponse.getUname(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onFailure(Call<LoginResponse> call, Throwable t) {
        Toast.makeText(getApplicationContext(), "onFailure called ", Toast.LENGTH_SHORT).show();
        call.cancel();
    }
});

My APIClient.java :

package com.m.testparamretrofit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class APIClient {

    public static final String BASE_URL = "http://tc.thongputragroup.com:828";
    private static Retrofit retrofit = null;

    public static Retrofit getClient() {
        if (retrofit == null) {
            OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .client(httpClient.build())
                    .build();
        }
        return retrofit;
    }

}

My APInterface.java :

package com.m.testparamretrofit;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

public interface APIInterface {

    @GET("API/Users/validateLogin.php")
    Call<LoginResponse> validateUser(@Query("user") String user, @Query("pass") String pass);


}

My LoginResponse.java :

package com.m.testparamretrofit;

import com.google.gson.annotations.SerializedName;

public class LoginResponse {

    @SerializedName("Uid")
    public String Uid;
    @SerializedName("Uname")
    public String Uname;
    @SerializedName("Upass")
    public String Upass;

    public LoginResponse(String Uname, String Upass) {
        this.Uname = Uname;
        this.Upass = Upass;
    }

    public String getUid() {
        return Uid;
    }

    public String getUname() {
        return Uname;
    }

    public String getUpass() {
        return Upass;
    }
}

My Common method is exactly same.

my API is :

{"status":1,"data":[{"Uid":"1","Uname":"admin"},{"Uid":"2","Uname":"Bu Sales"},{"Uid":"3","Uname":"Pusat"}]}

Ok, my question is didnt yet about extract API, but why little different input cause abnormality in http result ? * note i didnt yet implement post, cz still in learning progres.. :D

Input 1 :
Just input n and n in user and pass..
http://tc.thongputragroup.com:828/API/Users/validateLogin.php?user=n&pass=n

Result 1 :

2019-10-03 08:55:52.174 24760-24760/com.m.testparamretrofit E/mulyo: loginResponse 1 --> Response{protocol=http/1.1, code=200, message=OK, url=http://tc.thongputragroup.com:828/API/Users/validateLogin.php?user=n&pass=n}
2019-10-03 08:55:52.174 24760-24760/com.m.testparamretrofit E/mulyo: loginResponse 2 --> OK
2019-10-03 08:55:52.174 24760-24760/com.m.testparamretrofit E/mulyo: loginResponse 3 --> true
2019-10-03 08:55:52.174 24760-24760/com.m.testparamretrofit E/mulyo: loginResponse 4 --> 200
2019-10-03 08:55:52.174 24760-24760/com.m.testparamretrofit E/mulyo: getUserId          -->  null
2019-10-03 08:55:52.174 24760-24760/com.m.testparamretrofit E/mulyo: getFirstName       -->  null
2019-10-03 08:55:52.174 24760-24760/com.m.testparamretrofit E/mulyo: getLastName        -->  null

Input 2 :
Just input a and p in user and pass..
http://tc.thongputragroup.com:828/API/Users/validateLogin.php?user=a&pass=p

Result 2 :

2019-10-03 08:49:43.897 24760-24760/com.m.testparamretrofit E/mulyo: userId is -> a
2019-10-03 08:49:43.898 24760-24760/com.m.testparamretrofit E/mulyo: password is -> p
2019-10-03 08:49:44.243 24760-24760/com.m.testparamretrofit E/mulyo: call 1 --> retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall@5a15b06
2019-10-03 08:49:44.243 24760-24760/com.m.testparamretrofit E/mulyo: loginResponse 1 --> Response{protocol=http/1.1, code=500, message=Internal Server Error, url=http://tc.thongputragroup.com:828/API/Users/validateLogin.php?user=a&pass=p}
2019-10-03 08:49:44.244 24760-24760/com.m.testparamretrofit E/mulyo: loginResponse 2 --> Internal Server Error
2019-10-03 08:49:44.244 24760-24760/com.m.testparamretrofit E/mulyo: loginResponse 3 --> false
2019-10-03 08:49:44.244 24760-24760/com.m.testparamretrofit E/mulyo: loginResponse 4 --> 500

And several input that strange,,
why some get 200(ok) then other get 500(internal error)..

a & b = 200(ok)
a & c = 500
a & d = 200
z & z = 200
g & g = 200
..
.
etc..

budi
  • 53
  • 6

0 Answers0