I am trying to read a uri which returns either a 0 or -1. The asynctask is called from inside a Fragment. Basically, its a login when the user clicks a button the inputs are checked on the remote server.
The Login Code is :
// Validate the user.
String uri = HLConsts.SERV_URI + "ValidateLoginUser?uname=" + szUserName + "&password=" + szPassword + "&android=1";
ReadUri rd = new ReadUri(uri);
int cnt =0;
rd.execute();
while ( rd.getSzReturn() == null || cnt > 5) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
cnt++;
}
if(cnt > 5) {
errors++;
}
String szValid = rd.getSzReturn();
Snackbar.make(root, "Name = " + szUserName + " Pass = " + szPassword , Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
============================ The Async Task is :
public class ReadUri extends AsyncTask< Void , Void, String> {
// private Context mContext;
private String mUrl;
private String szReturn = null;
public String getSzReturn() {
return szReturn;
}
public ReadUri( String url) {
// mContext = context;
mUrl = url;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// dynamictext = (TextView) myView.findViewById(R.id.textview_first);
}
@Override
protected String doInBackground(Void... params) {
String resultString = null;
resultString = getString(mUrl);
return resultString;
}
@Override
protected void onPostExecute(String strings) {
super.onPostExecute(strings);
szReturn =strings;
}
//======================================================================================
private String getString(String InUrl) {
String ret = "Nothing";
URL url = null;
try {
url = new URL(InUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
// The line below is where the exception is called
int response = 0;
try {
response = httpURLConnection.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("Response", String.valueOf(response));
if (response == HttpURLConnection.HTTP_OK) {
// Response successful
InputStream inputStream = null;
try {
inputStream = httpURLConnection.getInputStream();
} catch (IOException ex) {
ex.printStackTrace();
}
// Parse it line by line
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
StringBuffer sb = new StringBuffer();
while (true) {
try {
// if (!((temp = bufferedReader.readLine()) != null)) {
while((line=bufferedReader.readLine())!=null)
{
// sb.append(line); //appends line to string buffer
// sb.append("\n"); //line feed
ret += line;
Log.w("MyApp",line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
// Parsing of data here
}
} else {
Log.e("SAMES", "INCORRECT RETURN CODE: " + response);
}
httpURLConnection.disconnect();
// urlConnection.disconnect();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return ret;
}
}
Please would you help. I have set breakpoints and none of them are triggered and doInBackground is never called.
Thanks Jeff.
This was fixed by not using an external class to run the asynctask.
The example is below : AsyncTask Android example