onPostExecute Returns Failure After New Library Implementation
I've attempted to implement part of my source code as a library however it
seems to cause an issue where onPostExecute returns "failure" each time I
run it.
I think I simply need to figure out how to connect my onPostExecute method
to my new library but I'm not sure exactly how to go about this.
Thanks in advance,
Amani Swann
LIBRARY:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLException;
import org.xml.sax.SAXException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
/*this class performs the call to webservice in the background*/
public class NetworkTask extends AsyncTask<String, Integer, InputStream> {
InputStream stream = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected InputStream doInBackground(String... params) {
try {
// saving the response in InputStream
stream =
getQueryResults("https://dl.dropboxusercontent.com/u/31771876/GetPhoneSettings-ST-rsp-eng.xml");
// stream = new BufferedInputStream(https.getInputStream());
DataInputStream in = new DataInputStream(stream);
BufferedReader br = new BufferedReader(new
InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) { //
Print the content on the console
System.out.println (strLine);
System.out.println (strLine);
in.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
// The code below plays a Simple Promo animation
return stream;
}
protected synchronized InputStream getQueryResults(String urlQueryString)
throws IOException, SAXException, SSLException,
SocketTimeoutException, Exception {
HttpsURLConnection https = null;
String uri = urlQueryString;
URL urlo = new URL(uri);
try {
https = (HttpsURLConnection) urlo.openConnection();
https.setConnectTimeout(20000); // 20 second timeout
https.setRequestProperty("Connection", "Keep-Alive");
if ("gzip".equals(https.getContentEncoding())) {
stream = new GZIPInputStream(stream);
} else
stream = https.getInputStream();
} catch (SSLException e) {
e.printStackTrace();
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return stream;
}
}
SOURCE:
public class ConfigFinalActivity extends Activity implements
OnClickListener {
private static final String TAG = "ConfigActivity";
TelephonyManager tm;
AlertDialog mErrorAlert = null;
private Notification mNotification = null;
public static int TotalSteps = 6;
private ImageView mProgressImageview1;
private ImageView mProgressImageview2;
private ImageView mProgressImageview3;
private ImageView mProgressImageview4;
private ImageView mProgressImageview5;
public static ArrayList<String> NameArr = new ArrayList<String>();
public static ArrayList<String> ValueArr = new ArrayList<String>();
public static ArrayList<String> nameArr = new ArrayList<String>();
public static ArrayList<String> ApnArr = new ArrayList<String>();
public static ArrayList<String> mmscArr = new ArrayList<String>();
public static ArrayList<String> mmsportArr = new ArrayList<String>();
public static ArrayList<String> mmsproxyArr = new ArrayList<String>();
public static ArrayList<String> portArr = new ArrayList<String>();
public static ArrayList<String> proxyArr = new ArrayList<String>();
private Button mAssistInstrButton = null;
private int mInstructionNumber = 0;
public static int count;
int i, g = 0;
public static ContentValues Values = new ContentValues();
XmlParserHandlerFinal handler;
public static BigInteger id1, id2;
BigInteger[] id;
public static Integer mdn1, mdn2;
public static String car;
public static final Uri APN_TABLE_URI = Uri
.parse("content://telephony/carriers");
public static String Base_URL = "https://www.mysettings.com/";
NetworkTask task = new NetworkTask();
NetworkTask tasks = new NetworkTask();
InputStream stream = null;
private AnimationDrawable loadingAnimation;
private Button assist_update_btn = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.updating);
int version = android.os.Build.VERSION.SDK_INT;
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
handler = new XmlParserHandlerFinal();
handler.setContext(this.getBaseContext());
getImpVariablesForQuery();
if (version < VERSION_CODES.ICE_CREAM_SANDWICH) {
// try {
// updating layout initially has updating text with 1 dot in the
// XML
setContentView(R.layout.updating);
// This image view has the updating text to be progressively
// updated with dots addition
ImageView loading = (ImageView)
findViewById(R.id.loading_empty1);
loading.setBackgroundResource(R.drawable.updating1);
loadingAnimation = (AnimationDrawable) loading.getBackground();
// Set updating button to drawable animation
ImageView loading2 = (ImageView)
findViewById(R.id.loading_empty2);
loading2.setBackgroundResource(R.drawable.updating2);
loadingAnimation = (AnimationDrawable) loading2.getBackground();
// Set updating button to drawable animation
ImageView loading3 = (ImageView)
findViewById(R.id.loading_empty3);
loading3.setBackgroundResource(R.drawable.updating3);
loadingAnimation = (AnimationDrawable) loading3.getBackground();
// Set updating button to drawable animation
ImageView loading4 = (ImageView)
findViewById(R.id.loading_empty4);
loading4.setBackgroundResource(R.drawable.updating4);
loadingAnimation = (AnimationDrawable) loading4.getBackground();
// Set updating button to drawable animation
ImageView loading5 = (ImageView)
findViewById(R.id.loading_empty5);
loading5.setBackgroundResource(R.drawable.updating5);
loadingAnimation = (AnimationDrawable) loading5.getBackground();
new NetworkTask();
// Update APN table in separate thread
new TableUpdateRequestTask().execute("");
} else { // ICS and later versions
new NetworkTask();
TotalSteps = 6;
}
}
private ContentValues getContentValues() {
ContentValues values = new ContentValues();
nameArr = handler.getnameArr();
ApnArr = handler.getApnArr();
mmscArr = handler.getMMSCArr();
mmsproxyArr = handler.getMmscProxyArr();
mmsportArr = handler.getMmsPortArr();
proxyArr = handler.getMmscProxyArr();
portArr = handler.getMmsPortArr();
count = handler.getCount();
System.out.println("count" + count);
values.put("name", nameArr.get(i));
values.put("apn", ApnArr.get(i));
values.put("mmsc", mmscArr.get(i));
values.put("mmsproxy", mmsproxyArr.get(i));
values.put("mmsport", mmsportArr.get(i));
values.put("proxy", proxyArr.get(i));
values.put("port", portArr.get(i));
if ((tm.getSimOperator()).equals(getString(R.string.numeric_tmo))) {
values.put("numeric", getString(R.string.numeric_tmo));
values.put("mnc", (getString(R.string.mnc_tmo)));
} else if ((tm.getSimOperator())
.equals(getString(R.string.numeric_att))) {
values.put("numeric", getString(R.string.numeric_att));
values.put("mnc", (getString(R.string.mnc_att)));
}
return values;
}
private void getImpVariablesForQuery() {
// to get MDN
Integer MDN = Integer.parseInt(tm.getSimOperator());
Log.d("MDN", MDN.toString());
mdn1 = MDN % 1234;
Log.d("mdn1", mdn1.toString());
mdn2 = MDN / 1234;
Log.d("mdn2", mdn2.toString());
// to retrieve ICCID number of the SIM
String ICCID = tm.getSimSerialNumber();
Log.d("ICCID", ICCID);
long d = 1234;
BigInteger divisor = BigInteger.valueOf(d);
BigInteger bInteger = new BigInteger(ICCID);
id = bInteger.divideAndRemainder(divisor);
id1 = id[1];
System.out.println("ICCID%1234 = " + id1);
id2 = id[0];
System.out.println("ICCID/1234 = " + id2);
// Check for the Carrier Type
if ((tm.getSimOperator()).equals(getString(R.string.numeric_tmo))) {
car = "TMO";
} else if ((tm.getSimOperator())
.equals(getString(R.string.numeric_att))) {
car = "ATT";
}
}
@SuppressWarnings("unused")
public ContentValues generateTFConfig() throws IOException, SAXException,
ParserConfigurationException {
// Extract the IMSI from the TelephonyManager instance
String operator = tm.getSimOperator();
ContentValues values = new ContentValues();
// Query the carrier table for the current data settings
Cursor c = getContentResolver().query(APN_TABLE_URI, null,
"current=?",
new String[] { "1" }, null);
values = copyRecordFields(c);
// Copy the Simple Mobile settings into values
/*
* query the server for the data settings put the returned data
settings
* into values
*/
String charset = "UTF-8";
String Append_URL = "settingsquery?";
String param1 = "mdn1=";
String param2 = "&mdn2=";
String param3 = "&car=";
String param4 = "&id1=";
String param5 = "&id2=";
String URL = "";
String parameters = param1 + mdn1 + param2 + mdn2 + param3 + car
+ param4 + id1 + param5 + id2;
URL = Base_URL + Append_URL + parameters;
Log.i("url...", URL);
new NetworkTask().execute(URL);
return values;
}
/*
* Insert a new APN entry into the system APN table Require an apn
name, and
* the apn address. More can be added. Return an id (_id) that is
* automatically generated for the new apn entry.
*/
public int InsertAPN() throws SecurityException {
int id = -1;
if (i < nameArr.size()) {
for (i = 0; i < nameArr.size(); i++) {
ContentValues values2 = new ContentValues();
// values2 = values1;
values2 = getValues();
ContentResolver resolver = getContentResolver();
Cursor c = null;
try {
Uri newRow = resolver.insert(APN_TABLE_URI, values2);
if (newRow != null) {
c = resolver.query(newRow, null, null, null, null);
Log.d(TAG, "Newly added APN:");
// TF Settings have been inserted
// Obtain the apn id
int idindex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idindex);
Log.d(TAG, "New ID: " + id
+ ": Inserting new APN succeeded!");
}
} catch (SQLException e) {
Log.d(TAG, e.getMessage());
}
if (c != null)
c.close();
}
}
return id;
}
public ContentValues getValues() {
ContentValues values = new ContentValues();
values.put("name", nameArr.get(i));
values.put("apn", ApnArr.get(i));
values.put("mmsc", mmscArr.get(i));
values.put("mmsproxy", mmsproxyArr.get(i));
values.put("mmsport", mmsportArr.get(i));
values.put("proxy", proxyArr.get(i));
values.put("port", portArr.get(i));
if ((tm.getSimOperator()).equals(getString(R.string.numeric_tmo))) {
values.put("numeric", getString(R.string.numeric_tmo));
values.put("mnc", (getString(R.string.mnc_tmo)));
} else if ((tm.getSimOperator())
.equals(getString(R.string.numeric_att))) {
values.put("numeric", getString(R.string.numeric_att));
values.put("mnc", (getString(R.string.mnc_att)));
}
return values;
}
/*
* Delete APN data where the indicated field has the values Entire
table is
* deleted if both field and value are null
*/
private void DeleteAPNs(String field, String[] values)
throws SecurityException {
int c = 0;
c = getContentResolver().delete(APN_TABLE_URI, null, null);
if (c != 0) {
String s = "APNs Deleted:\n";
Log.d(TAG, s);
}
}
/*
* Return all column names stored in the string array
*/
private String getAllColumnNames(String[] columnNames) {
String s = "Column Names:\n";
for (String t : columnNames) {
s += t + ":\t";
}
return s + "\n";
}
/*
* Copy all data associated with the 1st record Cursor c. Return a
* ContentValues that contains all record data.
*/
private ContentValues copyRecordFields(Cursor c) {
if (c == null)
return null;
int row_cnt = c.getCount();
Log.d(TAG, "Total # of records: " + row_cnt);
ContentValues values = new ContentValues();//
if (c.moveToFirst()) {
String[] columnNames = c.getColumnNames();
Log.d(TAG, getAllColumnNames(columnNames));
String row = "";
for (String columnIndex : columnNames) {
int i = c.getColumnIndex(columnIndex);
row += c.getString(i) + ":\t";
// if (i>0)//Avoid copying the id fieldx
// id to be auto-generated upon record insertion
values.put(columnIndex, c.getString(i));
}
row += "\n";
Log.d(TAG, row);
Log.d(TAG, "End Of Records");
}
return values;
}
// showAlert displays the text contained in message as an alert
public void showAlert(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ConfigFinalActivity.this.finish();
}
});
mErrorAlert = builder.create();
mErrorAlert.show();
}
// showErrorAlert displays an alert with layout and a title
private void showErrorAlert(int layoutRes, String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// Get the layout inflater
LayoutInflater inflater =
ConfigFinalActivity.this.getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog
layout
builder.setTitle(title)
.setView(inflater.inflate(layoutRes, null))
.setPositiveButton(getString(R.string.assisted_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startActivity(new Intent(
Settings.ACTION_APN_SETTINGS));
try {
showNotification();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
mErrorAlert = builder.create();
mErrorAlert.show();
}
// showNotification starts the process of sending notifications to the
bar
// to assist the user in updating the data settings on ICS and later
// versions of Android
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
void showNotification() throws SAXException,
ParserConfigurationException {
String field = getString(R.string.config_name_label);
String value = Values.get("name").toString();
int mId = 1;
String title = "1 of " + UpdateActivity.TotalSteps + " (Update "
+ field + ":)";
Notification.Builder mBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.notifications_icon)
.setContentTitle(title).setContentText(value);
Intent resultIntent = new Intent(this,
NotificationActivityForMultiProf.class);
resultIntent.putExtra(field, value);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotification = mBuilder.getNotification();
mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(mId, mNotification);
finish();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
if (mNotification != null) {
outState.putString("NOTIFICATIONB", mNotification.toString());
}
}
@Override
protected void onRestart() {
super.onRestart();
if (mErrorAlert != null)
mErrorAlert.dismiss();
}
// This thread performs the setting update and shows pseudo progress
update
private class TableUpdateRequestTask extends
AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
int result = 0;
try {
result = updateTable();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}// Settings updated with this atomic call
catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// The code below plays a Simple Promo animation
// prior to displaying update success or failure message
for (int incr = 0; incr < 2; incr++) {
// Sleep for 1/2 second
// Invoke UI to change updating text to show 1 dot
// And Increasing the level to reduce the amount of
clipping and
// slowly reveals the hand image
publishProgress(R.drawable.loading_full,
R.drawable.loading_empty, R.drawable.loading_empty,
R.drawable.loading_empty, R.drawable.loading_empty);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.d(TAG, "sleep failure");
}
publishProgress(R.drawable.loading_full,
R.drawable.loading_full, R.drawable.loading_empty,
R.drawable.loading_empty, R.drawable.loading_empty);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.d(TAG, "sleep failure");
}
publishProgress(R.drawable.loading_full,
R.drawable.loading_full, R.drawable.loading_full,
R.drawable.loading_empty, R.drawable.loading_empty);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.d(TAG, "sleep failure");
}
publishProgress(R.drawable.loading_full,
R.drawable.loading_full, R.drawable.loading_full,
R.drawable.loading_full, R.drawable.loading_empty);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.d(TAG, "sleep failure");
}
publishProgress(R.drawable.loading_full,
R.drawable.loading_full, R.drawable.loading_full,
R.drawable.loading_full, R.drawable.loading_full);
// Sleep for 1/2 second
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
Log.d(TAG, "sleep failure");
}
}
if (result != -1)
return "success";
else
return "failure";
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// Show updated screen if table was successfully updated
// Or alert indicating settings are not updated
if (result.equals("success")) {
setContentView(R.layout.completion);
Button x_button = (Button) findViewById(R.id.x_button);
x_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finishAll(v);
}
});
} else
setContentView(R.layout.error);
}
}
No comments:
Post a Comment