After days of reading tutorials incl. android developer page etc. I'm frustrated. I try to implement a request to my cloud server where I run a database with users. I have my SSL certificate. Problem is that really nothing is working. I tried so many codes. Step one is testing login with my XAMPP MySQL database and for the first step, it worked. Now, after trying to implement HTTPS to my server, nothing gonna happen. Maybe someone can help me.
Here is my actual code:
ApiClient.java
public abstract class ApiClient {
public static final String BASE_URL = "https://87.106.205.247/";
public static Retrofit retrofit = null;
public static Object getApiClient() throws IOException, KeyStoreException, CertificateException,
NoSuchAlgorithmException, KeyManagementException {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
InputStream caInput = new BufferedInputStream(new FileInputStream("load-der.crt"));
Certificate ca;
try {
ca = cf.generateCertificate(caInput);
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
} finally {
caInput.close();
}
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
return context;
HttpsURLConnection urlConnection =
(HttpsURLConnection)url.openConnection();
urlConnection.setSSLSocketFactory(context.getSocketFactory());
InputStream in = urlConnection.getInputStream();
copyInputStreamToOutputStream(in, System.out);
OkHttpClient okHttpClient = new OkHttpClient().newBuilder().addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request originalRequest = chain.request();
Request.Builder builder = originalRequest.newBuilder().header("Authorization",
Credentials.basic("username", "XXXXXXXX"));
Request newRequest = builder.build();
return chain.proceed(newRequest);
}
}).build();
if(retrofit == null)
{
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
private static void copyInputStreamToOutputStream(InputStream in, PrintStream out) {
}
}
ApiInterface
public interface ApiInterface {
@FormUrlEncoded
@POST("login_retrofit.php")
Call<KimbaUsersModel> login(@Field("email") String email,
@Field("password") String password);
}
Login Fragment
public class LoginRetrofit extends Fragment {
ApiInterface apiInterface;
private Button btn_login;
private EditText email, password;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.logintest, container, false);
btn_login = view.findViewById(R.id.btn_logintest);
email = view.findViewById(R.id.etEmail);
password = view.findViewById(R.id.etPassword);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login();
}
public void login() {
try {
apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
} catch (IOException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
Call<KimbaUsersModel> kimbaUsersCall = apiInterface.login(email.getText().toString(),
password.getText().toString());
kimbaUsersCall.enqueue(new Callback<KimbaUsersModel>() {
@Override
public void onResponse(Call<KimbaUsersModel> call, Response<KimbaUsersModel> response) {
if(response.body() != null){
KimbaUsersModel kimbaUsers = response.body();
Log.e("Fehler: ", response.toString());
if(kimbaUsers.isSuccess()){
Toast.makeText(getActivity(), "Login erfolgreich", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getActivity(), "Benutzer nicht registriert "
+kimbaUsers.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<KimbaUsersModel> call, Throwable t) {
Toast.makeText(getActivity(), "Login nicht erfolgreich" +t, Toast.LENGTH_SHORT).show();
Log.e("Fehler: ", t.toString());
}
});
}
});
return view;
}
}
and my Model.class
public class KimbaUsersModel {
@Expose
@SerializedName("email")
private String email;
@Expose
@SerializedName("password")
private String password;
@Expose
@SerializedName("success")
private boolean success;
@Expose
@SerializedName("message")
private String message;
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 isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
I know that I have to implement doInBackground and some other things. But my problem is that all the tutorials and two or three years old.
Thanks a lot.