Hi I am trying check if a username is available but has to go through an async task to check. tried a few ways but wasn't able to do it. So, I came with this:
async task is in a normal class, so will call a new instance, call the async task (checkusername), then wait for async task to finish (on postexecute, it will update a public variable, 'resultOut'). while at the activity, to prevent from locking, loop to 1000 OR resultOut before it exits.
I keep getting 'resultOut' as false. Here's the code:
urlConnection url = new urlConnection();
if(url.isConnected(view.getContext())) {
Toast.makeText(getApplicationContext(), "connected ", Toast.LENGTH_LONG).show();
boolean result=false;
url.checkUserName(user_name);
int i = 0;
while (!result && i!=1000){
i++;
result=url.resultOut;
}
Here is the urlconnection class (edited)
private void connectUrl(String action, String user_name, String pwd) {
String urlParam = null;
strAction=action;
switch (action){
case "update":
urlParam = "/test.php?action=update&user_id=7&lat="+latitude+"&lon="+longitude;
break;
case "locate":
urlParam = "/test.php?action=locate&user_id=7";
break;
case "insert":
urlParam = "/test.php?action=insert&user_name="+user_name+"&pwd="+pwd;
break;
case "check":
urlParam = "/test.php?action=check&user_name="+user_name;
}
//Toast.makeText(, "Connecting to site: " + WB_URL + urlParam, Toast.LENGTH_LONG).show();
new DownloadWebpageTask().execute(WB_URL + urlParam);
}
private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
result=result.trim();
switch (strAction){
case "update":
int i = Integer.parseInt(result);
if (i > 0) {
//Toast.makeText(getApplicationContext(), "Update successful", Toast.LENGTH_LONG).show();
}else{
//Toast.makeText(getApplicationContext(), "Update failed", Toast.LENGTH_LONG).show();
}
break;
case "locate":
String[] s = result.split(",");
case "insert":
break;
case "check":
int x = Integer.parseInt(result);
resultOut=true;
//Toast.makeText(getApplicationContext(), "Check Result: "+x, Toast.LENGTH_LONG).show();
if (x==0){
//cannot locate name so, name usable
isUserNameUsed=false;
}else{
isUserNameUsed=true;
}
}
//showLocation(null);
}
}
private String downloadUrl(String myurl) throws IOException {
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d(DEBUG_TAG, "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
}
Help appreciated, thank you in advance. Richard
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…