Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network on main thread #86

Closed
mohsin1122 opened this issue Feb 13, 2017 · 18 comments
Closed

Network on main thread #86

mohsin1122 opened this issue Feb 13, 2017 · 18 comments
Labels

Comments

@mohsin1122
Copy link

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();
ANResponse response = 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();
}

@amitshekhariitbhu
Copy link
Owner

You must make synchronous call from background thread.

@mohsin1122
Copy link
Author

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
                    }
                });

@amitshekhariitbhu
Copy link
Owner

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

@mohsin1122
Copy link
Author

mohsin1122 commented Feb 20, 2017

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

@amitshekhariitbhu
Copy link
Owner

@mohsin1122 As FAN is built on OkHttp Layer, you can definitely pass custom OkHttpClient while initializing it.

@mohsin1122
Copy link
Author

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) {
                           
                        }
                    });

@amitshekhariitbhu
Copy link
Owner

@mohsin1122 without the url how can I debug?

@amitshekhariitbhu
Copy link
Owner

@mohsin1122 I am getting server error.

@amitshekhariitbhu
Copy link
Owner

@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.

@mohsin1122
Copy link
Author

@amitshekhariitbhu is there a way we can have chat?
coz i cant share the details publicly

@mohsin1122
Copy link
Author

plz provide me the example how to implement ssl pinning using FAN.

@amitshekhariitbhu
Copy link
Owner

I will provide it as an example on website, I think there is some problem from your server side

@mohsin1122
Copy link
Author

@amitshekhariitbhu thanks
plz provide the example asap as i have deadlines to meet

@amitshekhariitbhu
Copy link
Owner

@mohsin1122 Have you tried your request from Postman (just for testing, is your server working).

@mohsin1122
Copy link
Author

@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

@amitshekhariitbhu
Copy link
Owner

amitshekhariitbhu commented Feb 20, 2017

@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();

@mohsin1122
Copy link
Author

the snippet u provide is working but still not validating the certificate(not giving any errors)

@mohsin1122
Copy link
Author

Resolved
problem lie in this line
String hostname = "https://publicobject.com";
hostname is publicobject.com https is not a part of hostname

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants