0

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;
}   
Tim
  • 35,413
  • 11
  • 95
  • 121
Samper
  • 73
  • 1
  • 4
  • Perhaps persisting the (session) cookies set by SAP would be sufficient? A singleton [BasicCookieStore](http://developer.android.com/reference/org/apache/http/impl/client/BasicCookieStore.html) in memory should just the thing for that. – Jens Jun 04 '12 at 19:24

1 Answers1

1

You could extend the Application object and put the DefaultHttpClient in there.

How to extend and use the Application object:

How to declare global variables in Android?

Community
  • 1
  • 1
Krylez
  • 17,414
  • 4
  • 32
  • 41