我有一个只有JSON解析器的旧代码,这在2.3及更低版本中运行良好。现在,我正在尝试添加Async逻辑,以便它也适用于Android 3.0及更高版本。
“货币转换器”是我的JSON解析器类。
这是JSON的代码片段
private void currencySave() {
// TODO Auto-generated method stub
AsyncTaskExample asyncTask = new AsyncTaskExample();
asyncTask.execute();
}
private class AsyncTaskExample extends AsyncTask<String, Void, String> {
// private String Content;
private ProgressDialog Dialog;
String response = "";
protected void onPreExecute() {
Dialog = new ProgressDialog(MoneyLoginActivity.this);
Dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
Dialog.setMessage("Loading Convertors. Please Wait ...");
Dialog.setCancelable(false);
Dialog.show();
}
protected String doInBackground(String... urls) {
try {
CurrencyConvertor jParser = new CurrencyConvertor();
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
// getting JSON string from URL
JSONObject rates = json.getJSONObject(TAG_RATES);
dUSD = rates.getString(TAG_USD);
dINR = rates.getString(TAG_INR);
dEURO = rates.getString(TAG_EUR);
dGBP = rates.getString(TAG_GBP);
response = "Success";
} catch (JSONException e) {
e.printStackTrace();
response = "Failed";
}
} catch (Exception e) {
response = "Failed";
}
return response;
}
protected void onPostExecute(String result) {
Dialog.dismiss();
Dialog = null;
if (response.equalsIgnoreCase("Success")) {
//do nothing
} else {
Toast.makeText(MoneyLoginActivity.this,
"Sorry Unable to retrive data. Please try again later",
Toast.LENGTH_LONG).show();
}
}
}
我得到空指针异常。这是日志
03-09 15:43:08.364:E / AndroidRuntime(1073):引起: java.lang.NullPointerException 03-09 15:43:08.364: E / AndroidRuntime(1073):at com.android.internal.os.LoggingPrintStream.println(LoggingPrintStream.java:298) 03-09 15:43:08.364:E / AndroidRuntime(1073):at com.jasmasuri.moneysheet.MoneyLoginActivity.decidePage(MoneyLoginActivity.java:128)
我在这段代码中得到它
CurrencyConvertor jParser = new CurrencyConvertor();
JSONObject json = jParser.getJSONFromUrl(url);