I'm developing an app that sends requests to SAP, I’ve been wrestling to keep the connection alive while I’m in the app.
I got three activities: main, connection and list.
When I start the app goes to main, after I tap on connection to establish a connection between SAP and the device and it connects successfully.
The issue that I have is when I press the return button to main and go to list activity, I cannot retrieve the data because I have lost my DefaultHttpClient that had the connection.
public String logInToSAP(String PIP, int PPort, String PSchema, String PUser, String PPassword) {
String result = "";
// HttpHost targetHost = new HttpHost(PIP, PPort, PSchema);
HttpHost targetHost = new HttpHost(PIP, PPort, PSchema);
// DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(),
targetHost.getPort()), new UsernamePasswordCredentials(PUser, PPassword));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local auth cache
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);
// Add AuthCache to the execution context
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(ClientContext.AUTH_SCHEME_PREF, authCache);
HttpGet request = new HttpGet("/sap/z_conn");
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(targetHost, request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(this, "Wrong Connection Parameters", Toast.LENGTH_SHORT).show();
result = "Wrong Connection Parameters";
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "IOException", Toast.LENGTH_SHORT).show();
result = "IOException";
}
return result;
}