hi,
i am using the below code to get synchronous call, but got network on main thread error
ANRequest request = AndroidNetworking.get(getResources().getString(R.string.server_url_user_mgmt_services) + "GetUserAuthorizationToken?eload_number={eloadnum}")
.addHeaders("Content-Type", "application/json")
.addHeaders("Authorization", CommonMethods.getSecurityHeader(OTP.this))
.addPathParameter("eloadnum", user.getUserIdentifier())
.build();
ANResponseresponse = request.executeForParsed(new TypeToken () {});
if (response.isSuccess()) {
JSONObject obj = response.getResult();
try {
boolean isValidUser = obj.getBoolean("isSuccessful");
if (isValidUser) {
if (!obj.getString("Token").isEmpty()) {
randomString = obj.getString("Token");
}
else
{
randomString = null;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
ANError error = response.getError();
// Handle Error
Toast.makeText(OTP.this, "Some error occured!\nTry Again", Toast.LENGTH_SHORT).show();
}
You must make synchronous call from background thread.
isnt that the Async Request?
AndroidNetworking.post("https://fierce-cove-29863.herokuapp.com/createAnUser")
.addBodyParameter("firstname", "Amit")
.addBodyParameter("lastname", "Shekhar")
.setTag("test")
.setPriority(Priority.MEDIUM)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
// do anything with response
}
@Override
public void onError(ANError error) {
// handle error
}
});
This will do the network task in the background and return the response in the main thread. However, you can also get the response in another thread of your choice by using setExecutor
is there a way for SSL Pinning in FAN?
or i have to use Okhttp with FAN to do so as okhttp provides SSL pinning feature
@mohsin1122 As FAN is built on OkHttp Layer, you can definitely pass custom OkHttpClient while initializing it.
i tried by this way but it is not showing errors as the sha key is incorrect
String hostname = "https://publicobject.com";
CertificatePinner certificatePinner = new CertificatePinner.Builder()
.add(hostname, "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
.build();
OkHttpClient client = new OkHttpClient().newBuilder().certificatePinner(certificatePinner).build();
//initialize fastnetwork android for api calls
AndroidNetworking.initialize(getApplicationContext(),client);
also try this way but still it gets the response for WS
String hostname = "https://publicobject.com";
CertificatePinner certificatePinner = new CertificatePinner.Builder()
.add(hostname, "sha256/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
.build();
OkHttpClient client = new OkHttpClient().newBuilder().certificatePinner(certificatePinner).build();
AndroidNetworking.post(getResources().getString(R.string.server_url_user_mgmt_services) + "GetUserDetails")
.setOkHttpClient(client)
.addHeaders("Content-Type", "application/json")
.addHeaders("Authorization", CommonMethods.getSecurityHeader(LoginActivity.this))
.addJSONObjectBody(json)
.setPriority(Priority.HIGH)
.build()
.setAnalyticsListener(new AnalyticsListener() {
@Override
public void onReceived(long timeTakenInMillis, long bytesSent, long bytesReceived, boolean isFromCache) {
Log.d("", " timeTakenInMillis : " + timeTakenInMillis);
Log.d("", " bytesSent : " + bytesSent);
Log.d("", " bytesReceived : " + bytesReceived);
Log.d("", " isFromCache : " + isFromCache);
}
})
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
}
@Override
public void onError(ANError anError) {
}
});
@mohsin1122 without the url how can I debug?
@mohsin1122 I am getting server error.
@mohsin1122 cannot help with little information. At least can tell me the exact error body you are getting. As the request is having many input like headers and JSON.
@amitshekhariitbhu is there a way we can have chat?
coz i cant share the details publicly
plz provide me the example how to implement ssl pinning using FAN.
I will provide it as an example on website, I think there is some problem from your server side
@amitshekhariitbhu thanks
plz provide the example asap as i have deadlines to meet
@mohsin1122 Have you tried your request from Postman (just for testing, is your server working).
@amitshekhariitbhu bro server is working fine
it requires username and password to work but due to security reason i am unable to provide you that hope u understand.
plz create a demo to use ssl pining with FAN
@mohsin1122 You are doing it right, but I do not know why that is not working. Try putting the password like this and do not add contentType as it will automatically pick.
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.certificatePinner(certificatePinner)
.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {
String credential = Credentials.basic("username", "password");
return response.request().newBuilder()
.header("Authorization", credential)
.build();
}
})
.build();
the snippet u provide is working but still not validating the certificate(not giving any errors)
Resolved
problem lie in this line
String hostname = "https://publicobject.com";
hostname is publicobject.com https is not a part of hostname