I am new to Asynctask coding, and am having some issues.
In my code I have defined a subclass in an onClick
listener, and in that class I have created an Asynctask which is using HTTP connections, and has some XML parsing code. The problem is, when I click on my button it is not waiting for the asynch task to complete, and instead it gives an error saying "Out of Bound in main class". There is no opportunity to process my PostExecute because XML parsing in the Async task throws an exception like: "NetworkOnMainThread".
I could really use some help uncovering my bug, here is my code:
public class GetDirectoryInfoXMLParser {
public static ArrayList<GetDirectoryInfoTableContent> tableContent;
private GetDirectoryInfoTableContent table;
private Thread alertThread;
private ProgressDialog progressDialog;
Context context;
InputStream is;
public GetDirectoryInfoXMLParser(SettingsPage settings) {
this.context = settings;
}
public void getDataFromUrl(String webSite) throws XmlPullParserException,
IOException {
if (webSite != null) {
alert(context);
GetDirectoryInfoAsyncTask gDirectoryInfoAsyncTask = new GetDirectoryInfoAsyncTask();
gDirectoryInfoAsyncTask.execute(webSite);
} else {
}
}
private class GetDirectoryInfoAsyncTask extends
AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://mobile.primemrm.com/mobile.asmx/GetDirectoryInfo");
ProtocolVersion protocolVersion = post.getProtocolVersion();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("sUrl", params[0]));
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
is = entity.getContent();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XmlPullParserFactory factory;
try {
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
// InputStream is = getURlResponse(params[0]);
if (is == null) {
Log.e("GetDirectoryInfoXMLParser", "InputStream is Null");
return null;
}
else if (is != null) {
Log.e("GetDirectoryInfoXMLParser",
"InputStream is Not Null");
parser.setInput(new InputStreamReader(is));
}
int eventType = parser.getEventType();
String name;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
tableContent = new ArrayList<GetDirectoryInfoTableContent>();
} else if (eventType == XmlPullParser.START_TAG) {
name = parser.getName();
if (name.equalsIgnoreCase("Table")) {
table = new GetDirectoryInfoTableContent();
/*
* for(int i = 0; i< parser.getAttributeCount();
* i++){
* if(parser.getAttributeName(i).equalsIgnoreCase
* ("id")){
* table.setId(parser.getAttributeValue(i)); }else
* if(parser.getAttributeName(i).equalsIgnoreCase(
* "rowOrder"));
* table.setRowOrder(parser.getAttributeValue(i)); }
*/
} else if (name
.equalsIgnoreCase("EnablePublicMemberDirectory")) {
table.setEnablePublicMemberDirectory(parser
.nextText());
Log.e("GetDirectoryInfoXMLParser",
"EnablePublicMemberDirectory : "
+ table.getEnablePublicMemberDirectory());
} else if (name.equalsIgnoreCase("Response")) {
table.setEnablePublicMemberDirectory(parser
.nextText());
}
} else if (eventType == XmlPullParser.END_TAG) {
name = parser.getName();
if (name.equalsIgnoreCase("Table")) {
tableContent.add(table);
table = null;
}
}
eventType = parser.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
public void alert(final Context context) {
progressDialog = ProgressDialog.show(context, "", "Loading...");
alertThread = new Thread() {
@Override
public void run() {
try {
sleep(5000);
progressDialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
alertThread.start();
}
}
Thanks In advance.
Edited:
verify.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String logingString = login.getText().toString();
String passwordString = password.getText().toString();
websiteString = websiteUrl.getText().toString();
if (websiteString.equals(pref.getString(WEBSITE, null))
&& logingString.equals(pref.getString(PREF_USERNAME,
null))
&& passwordString.equals(pref.getString(PREF_PASSWORD,
null))) {
//return;
}
if (saveCredientials(logingString, passwordString,
websiteString)) {
/*Toast.makeText(getApplicationContext(),
"Saved User Name & Password", Toast.LENGTH_LONG)
.show();*/
} else {
Toast.makeText(getApplicationContext(),
"User Name (or) Password is empty",
Toast.LENGTH_LONG).show();
}
sVersion = version.getText().toString();
sWesiteUrl = websiteUrl.getText().toString();
sLogin = login.getText().toString();
sPassword = password.getText().toString();
if (sWesiteUrl == null || sWesiteUrl == ""
|| sWesiteUrl.length() == 0) {
Log.e("SettingsPage", "Jhansi check 1 sWebsite is null");
websiteNullAlertDialog();
//return;
} else if (sWesiteUrl != null) {
try {
getDirectoyrInforParser.getDataFromUrl(sWesiteUrl);
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (GetDirectoryInfoXMLParser.tableContent != null) {
if (GetDirectoryInfoXMLParser.tableContent.get(0)
.getEnablePublicMemberDirectory()
.equals("true")) {
Log.e("SettingsPage",
"Jhansi check 2 sWebsite is Public");
isLoginSucess = true;
Intent intent = new Intent(SettingsPage.this,
HomePage.class);
intent.putExtra("wesiteLogoUrl", sWesiteUrl);
startActivity(intent);
} else if (GetDirectoryInfoXMLParser.tableContent
.get(0).getEnablePublicMemberDirectory()
.equals("false")) {
Log.e("SettingsPage",
"Jhansi check 3 sWebsite is Private");
// ifMember();
if (sLogin == null || sLogin.equals("")
|| sLogin.length() == 0
|| sPassword == null
|| sPassword.equals("")
|| sPassword.length() == 0) {
Log.e("SettingsPage",
"Jhansi check 4 loging is null");
ifMember();
// m.sivajhansi
return;
// alertDialog();
}
try {
parser.getDataFromUrl(sWesiteUrl, sLogin,
sPassword);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (LoginXMLParser.tableContent != null
&& LoginXMLParser.tableContent.size() > 0 && sLogin.equals(LoginXMLParser.tableContent
.get(0).getContact_email())
&& sPassword
.equals(LoginXMLParser.tableContent
.get(0)
.getContact_password())) {
Log.e("SettingsPage", "data is : "
+ LoginXMLParser.tableContent);
/*if (isLoginCorrect && sLogin.equals(LoginXMLParser.tableContent
.get(0).getContact_email())
&& sPassword
.equals(LoginXMLParser.tableContent
.get(0)
.getContact_password())) {*/
isLoginSucess = true;
Toast.makeText(SettingsPage.this,
"Login Sucess", Toast.LENGTH_SHORT)
.show();
// isLoginSucess = true;
Intent intent = new Intent(
SettingsPage.this, HomePage.class);
intent.putExtra("wesiteLogoUrl", sWesiteUrl);
startActivity(intent);
// }
/*
* else { Log.e("SettingsPage",
* "Jhansi check 5 "); alertDialog(); }
*/
}else {
Log.e("SettingsPage", "DATA CHECK");
// m.sivajhansi
alertDialog();
}
} else {
Log.e("SettingsPage", "Jhansi check 7");
websiteNullAlertDialog();
}
} else if (GetDirectoryInfoXMLParser.tableContent == null){
Log.e("SettingsPage", "Jhansi check 8");
websiteNullAlertDialog();
}
}
}
});
The Class which is called onclick is:
public class GetDirectoryInfoXMLParser {
public static ArrayList<GetDirectoryInfoTableContent> tableContent;
private GetDirectoryInfoTableContent table;
public void getDataFromUrl(String webSite)
throws XmlPullParserException, IOException {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
InputStream is = getURlResponse(webSite);
if (is == null) {
Log.e("GetDirectoryInfoXMLParser", "InputStream is Null");
return ;
}
else if (is != null) {
Log.e("GetDirectoryInfoXMLParser", "InputStream is Not Null");
parser.setInput(new InputStreamReader(is));
}
int eventType = parser.getEventType();
String name;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
tableContent = new ArrayList<GetDirectoryInfoTableContent>();
}else if(eventType == XmlPullParser.START_TAG){
name = parser.getName();
if(name.equalsIgnoreCase("Table")){
table = new GetDirectoryInfoTableContent();
/*for(int i = 0; i< parser.getAttributeCount(); i++){
if(parser.getAttributeName(i).equalsIgnoreCase("id")){
table.setId(parser.getAttributeValue(i));
}else if(parser.getAttributeName(i).equalsIgnoreCase("rowOrder"));
table.setRowOrder(parser.getAttributeValue(i));
}*/
}else if(name.equalsIgnoreCase("EnablePublicMemberDirectory")){
table.setEnablePublicMemberDirectory(parser.nextText());
Log.e("GetDirectoryInfoXMLParser", "EnablePublicMemberDirectory : "+table.getEnablePublicMemberDirectory());
} else if(name.equalsIgnoreCase("Response")){
table.setEnablePublicMemberDirectory(parser.nextText());
}
}else if(eventType == XmlPullParser.END_TAG){
name = parser.getName();
if(name.equalsIgnoreCase("Table")){
tableContent.add(table);
table = null;
}
}
eventType = parser.next();
}
}
public InputStream getURlResponse(String webSite) {
InputStream is = null;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://mobile.primemrm.com/mobile.asmx/GetDirectoryInfo");
ProtocolVersion protocolVersion = post.getProtocolVersion();
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs
.add(new BasicNameValuePair("sUrl", webSite));
try {
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
is = entity.getContent();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return is;
}
}
Now this is my code working perfectly upto2.3.3,beacause of strictMode i got "NetworkOnMainThread"..so please Help from this Hell by giving me some Outline to handle this..Thanks..