I am trying to raise an event from a Runnable class, but it does not seem to be working. My B4A subs never get hit. But inside the java library, it outputs the "Attempting to raiseevent..." which is after checking if the sub exists first. So it is detecting the sub, but not calling it for whatever reason.
Forgive me if something is not syntax-wise correct, I heavily modified this from our proprietary code so I could provide this example of my issue.
Here is my java code:
and my B4A Subs...
Forgive me if something is not syntax-wise correct, I heavily modified this from our proprietary code so I could provide this example of my issue.
Here is my java code:
B4X:
public void Validate(String postStr) {
final List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data", postStr));
Runnable r = new Runnable() {
public void run() {
postData("https://www.dummydomain.com/api.php?f=validate", nameValuePairs);
}
};
BA.submitRunnable(r, this, 0);
}
private void postData(String url, List<BasicNameValuePair> nameValuePairs) {
if (BA.debugMode == true) { BA.Log("URL:" + url); }
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
if (nameValuePairs != null) {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
for (BasicNameValuePair pair : nameValuePairs) {
if (BA.debugMode == true) { BA.Log("NAME: " + pair.getName() + " => VALUE: " + pair.getValue()); }
}
}
if (BA.debugMode == true) { BA.Log("execute..."); }
HttpResponse response = httpclient.execute(httppost);
if (BA.debugMode == true) { BA.Log("Read..."); }
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String body = "";
String line = "";
while ((line = rd.readLine()) != null) {
body = body + line;
}
if (BA.debugMode == true) { BA.Log("BODY:"+body); }
if (url.contains("?f=validate") == true) {
if (thisBA.subExists(thisEventName + "_validationresponse")) {
BA.Log("Attempting to raiseevent..." + thisEventName + "_validationresponse");
thisBA.raiseEventFromDifferentThread(MyDummyClass.this, MyDummyClass.this, 0, thisEventName + "_validationresponse", true, new Object[] { body });
}
} else if (url.contains("?f=otherrequest") == true) {
if (thisBA.subExists(thisEventName + "_otherrequestresponse")) {
BA.Log("Attempting to raiseevent..." + thisEventName + "_otherrequestresponse");
thisBA.raiseEventFromDifferentThread(MyDummyClass.this, MyDummyClass.this, 0, thisEventName + "_otherrequestresponse", true, new Object[] { body });
}
}
if (BA.debugMode == true) { BA.Log(body); }
} catch (ClientProtocolException e) {
if (thisBA.subExists(thisEventName + "_validationerror")) {
BA.Log("Attempting to raiseevent..." + thisEventName + "_validationerror");
thisBA.raiseEventFromDifferentThread(MyDummyClass.this, MyDummyClass.this, 0, thisEventName + "_validationerror", true, new Object[] { e.getMessage() });
}
if (BA.debugMode == true) { BA.Log(e.getMessage()); }
} catch (IOException e) {
if (thisBA.subExists(thisEventName + "_validationerror")) {
BA.Log("Attempting to raiseevent..." + thisEventName + "_validationerror");
thisBA.raiseEventFromDifferentThread(MyDummyClass.this, MyDummyClass.this, 0, thisEventName + "_validationerror", true, new Object[] { e.getMessage() });
}
if (BA.debugMode == true) { BA.Log(e.getMessage()); }
}
}
and my B4A Subs...
B4X:
Public Sub mydummyclass_validationresponse(data As String)
Log(data)
End Sub
Public Sub mydummyclass_otherrequestresponse(data As String)
Log(data)
End Sub
Last edited: