Hi There,
can you please assist? i am trying the helloworld example of visa direct using android platform. my keystore works on openssl but my app doesnot establish the connection (con.getResponseCode() = -1) with the following code:
String auth = userId + ":" + password;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
String authHeaderValue = "Basic " + new String(encodedAuth);
BufferedReader in;
try{
con.setRequestProperty("Authorization", authHeaderValue);
int status = con.getResponseCode();
// System.out.println("Http Status: " + status);
if (status == 200) {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
} else {
in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
System.out.println("Two-Way (Mutual) SSL test failed");
}
String response;
StringBuffer content = new StringBuffer();
while ((response = in.readLine()) != null) {
content.append(response);
}
in.close();
System.out.println(content.toString());
} catch (Exception e) {
e.printStackTrace();
}finally {
con.disconnect();
}
my error log:
W/zygote: Got a deoptimization request on un-deoptimizable method void java.io.FileInputStream.open0(java.lang.String)
W/zygote: Got a deoptimization request on un-deoptimizable method java.net.InetAddress[] libcore.io.Linux.android_getaddrinfo(java.lang.String, android.system.StructAddrinfo, int)
W/System.err: android.os.NetworkOnMainThreadException
W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450)
W/System.err: at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
W/System.err: at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:787)
W/System.err: at com.android.okhttp.Dns$1.lookup(Dns.java:39)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
W/System.err: at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
W/System.err: at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
W/System.err: at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
W/System.err: at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(DelegatingHttpsURLConnection.java:105)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(Unknown Source:0)
W/System.err: at com.bidata.asyncproject.MainActivity.testv(MainActivity.java:206)
Thank you in advance.
Solved! Go to Solution
Hi
I managed to solve this issue by calling AsyncTask "android.os.NetworkOnMainThreadException occurs because you are making network call on your main UI Thread". so my code took this format:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new Task().execute();
}
class MyDownloadTask extends AsyncTask<Void,Void,Void>
{
protected Long doInBackground(Void... params) {
URL url = new URL("https://sandbox.api.visa.com/vdp/helloworld");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String responseMsg = con.getResponseMessage();
int response = con.getResponseCode();
return null;
}
}
thanks
Hey @smol987,
That's excellent to hear and good job on resolving the issue. Thanks for sharing your solution with us, it's greatly appreciated. Please let us know if there's anything else we can do to help. Have a nice day.