Issue
I Have String Array :
public static final String[] LACCOUNT =
{
"username=admin1&password=saa123456",
"username=admin2&password=klk123456",
"username=admin3&password=exf123456",
"username=admin4&password=uoq123456",
"username=admin5&password=qff123456"
};
and i have
public void okGet(String URL, String param) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL + "?" + param)
.get()
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String textResponse = response.body().string();
runOnUiThread(new Runnable() {
@Override
public void run() {
if (textResponse.contains(":1")) {
progressDialog.dismiss();
Toast.makeText(getActivity(), "YES", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
I want to loop this Request with AsyncTask
I've tried like this, but it did not work
private class aTaskWorker extends AsyncTask<String, Void, String> {
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
fab1.setVisibility(View.GONE);
tvLog.setText(s);
// tvLog.setText(response.body().string());
// Kondisi
String strResult = tvLog.getText().toString();
if (strResult.contains(":1")) {
// TODO : stop this loop
}
}
@Override
protected String doInBackground(String... params) {
int totAkun = Constans.LACCOUNT.length;
for (int i = 0; i < totAkun; i++) {
int counter = i + 1;
tvTask.setText(counter + " of " + totAkun);
OKPost(Constans._LOGIN, Constans.LACCOUNT[i]);
}
return null;
}
once again , I want to loop this Request with AsyncTask
how to do that ?
TQ
Solution
First off, you need to fix your okGet() function to use synchronous request. Since you are already using AsyncTask (asynchronous operation) to loop your requests, the request must be synchronous.
public String tryLogin(String URL, String param) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(URL + "?" + param)
.get()
.build();
try {
Response response = client.newCall(request).execute();
if (response != null && response.body() != null) {
return response.body().string();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Then, you modify your AsyncTask to do this:
private static class LoginAsyncTask extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... accounts) {
for (String account : accounts) {
// try to login each account from array of accounts
String loginResult = tryLogin(Constants.LOGIN, account);
if (loginResult != null && loginResult.contains(":1")) {
return true;
}
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
// hide progress bar
if (result) {
// Successful login
} else {
// Failed login
}
}
}
Then you call your AsyncTask using the function below:
new LoginAsyncTask().execute(Constants.LACCOUNT);
Answered By - noahutz
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.