Issue
I have this problem: I want, when I insert and submit my data in a form, that an AsyncTask starts checking that everything is ok. In the specific case I am facing now, I want to verify that the username entered has not already been used, so I would like it to go to the server and via php to query the user's database. I wish that when the whole thing starts, the user screen is not blocked, that is, I would like to allow the user to move inside the screen anyway.
I enclose the code:
public class ControlloUsername extends AsyncTask<String, Void, String> {
AlertDialog a;
Context context;
public ControlloUsername(Context ct) {
// TODO Auto-generated constructor stub
context = ct;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
}
@Override
protected void onPostExecute(String result) { //sync
// TODO Auto-generated method stub
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
}
@Override
protected String doInBackground(String... params) { //sync
// TODO Auto-generated method stub
String username = params[0];
String login_url = Connector.db + "script/usercontrol.php";
String resultado = "";
URL u = null;
try {
u = new URL(login_url);
} catch (MalformedURLException e) {
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
resultado = writer.toString();
}
HttpURLConnection http = null;
try {
http = (HttpURLConnection) u.openConnection();
} catch (IOException e) {
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
resultado = writer.toString();
}
try {
http.setRequestMethod("POST");
} catch (ProtocolException e) {
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
resultado = writer.toString();
}
http.setDoInput(true);
http.setDoOutput(true);
http.setConnectTimeout(7000);
try {
OutputStream out = http.getOutputStream();
BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
String post_data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
bf.write(post_data);
bf.flush();
bf.close();
out.close();
InputStream in = http.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in, "iso-8859-1"));
String result = "";
String line = "";
while ((line = br.readLine()) != null) {
result += line;
}
bf.close();
in.close();
http.disconnect();
return result;
} catch (ConnectException e) {
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
resultado = "CONNECT" + writer.toString();
Log.d("PRINT", "CONNECT");
} catch (UnsupportedEncodingException e) {
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
resultado = "UNSUPPORTEDENCODING" + writer.toString();
Log.d("PRINT", "UNSUPPORTEDENCODING");
} catch (ProtocolException e) {
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
resultado = "PROTOCOL" + writer.toString();
Log.d("PRINT", "PROTOCOL");
} catch (MalformedURLException e) {
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
resultado = "MALFORMEDURL" + writer.toString();
Log.d("PRINT", "MALFORMEDURL");
} catch (IOException e) {
Writer writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
resultado = "IO" + writer.toString();
Log.d("PRINT", "IO");
}
return resultado;
}
}
RegisterActivity
String u = user.getText().toString();
ControlloUsername bg = new ControlloUsername(this);
try {
String res = bg.execute(u).get();
Log.d("PRINT", res);
if (res.contains("...
I've read that it's a problem of execute().get () and the AsyncTask's own methods.
Solution
You can use an interface to get the result back after your AsyncTask
finishes. Inside your AsyncTask:
onFinishListener mListener; // Create a variable for your interface
// Define the interface & callbacks
public interface onFinishListener{
// Replace 'variableType' with the appropriate type for your result
void onFinish(variableType myResult);
}
public void setOnFinishListener(onFinishListener l){
mListener = l;
}
@Override
protected void onPostExecute(variableType result){
if(mListener != null){
mListener.onFinish(result);
}
}
Then inside of your Activity, make sure to set the interface:
MyAsyncTask task = new MyAsyncTask();
task.execute();
task.setOnFinishListener(new MyAsyncTask.onFinishListener(){
@Override
public void onFinish(variableType myResult){
// The task has finished and you can now use the result
}
});
Answered By - Sammy T
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.