Using loopj I make an HTTPGet to open a session and save the cookie, the response is parsed to get the sessionId (in my case it's required to send SOAP command to router). Passing the istance of AsyncHttpClient to the second method the session still open (but you can define an AsyncHttpClient as object of Class, I don't know how your code is writed) and performe the command
public void doCommandToRouter(final String channel, final genericResponse result) {
final AsyncHttpClient client = new AsyncHttpClient();
client.get("http://" + Constants.WIFICHANNEL.VS_DEFAULT_IP, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, Header[] headers, byte[] response) {
String session_id = parseSessionId(new String(response));
applyChanneltoStation(client,session_id,channel,result);
}
@Override
public void onFailure(int i, Header[] headers, byte[] response, Throwable throwable) {
Log.d(TAG, "Fail");
result.onError(i);
}
});
}
private void applyCommandtoRouter(final AsyncHttpClient client, final String session_id, final String bestChannel, final genericResponse result) {
numTestChange = 0;
String xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">... SOAP COMMAND ...</soapenv:Envelope>";
HttpEntity entity;
try {
entity = new StringEntity(xml, "UTF-8");
} catch (IllegalArgumentException e) {
Log.d("HTTP", "StringEntity: IllegalArgumentException");
return;
} catch (UnsupportedEncodingException e) {
Log.d("HTTP", "StringEntity: UnsupportedEncodingException");
return;
}
String contentType = "text/xml";
client.post( context, Constants.WIFICHANNEL.VS_SERVICE_URL, entity, contentType, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, Header[] headers, byte[] bytes) {
Log.d(TAG, "post ok");
//Log.d(TAG, "Response: " + new String(bytes));
result.onSuccess();
}
@Override
public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
Log.d(TAG, "Fail. Error " + i);
}
});
}
To parse the response
private String parseSessionId(String pageText) {
String pattern = "(dm_cookie=')(.+)(';)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(pageText);
String session_id = "";
while(m.find()) {
session_id = m.group(2);
Log.d(TAG, "Found session id: " + session_id);
}
return session_id;
}
Please keep in mind that this works only on my router, you must check how your router works because probably this with ctrl+c crtrl+v doesnt' work.