在我尝试 AsyncTask
我从我的服务器得到电子邮件地址。在 onPostExecute()
我必须检查是电子邮件地址 empty
或 null
。我用下面的代码来检查它:
if (userEmail != null && !userEmail.isEmpty()) {
Toast.makeText(getActivity(), userEmail, Toast.LENGTH_LONG).show();
UserEmailLabel.setText(userEmail);
}
但是在我的 Toast
中我看到 null
被打印出来了。我的完整代码:
private class LoadPersonalData extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Void doInBackground(String... res) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_id", PrefUserName));
params.add(new BasicNameValuePair("type", type_data));
JSONObject json = jsonParser.makeHttpRequest(Url, "POST", params);
String result = "";
try {
result = json.getString("message");
} catch (JSONException e) {
e.printStackTrace();
}
if (result.equals("success")) {
try {
userEmail = json.getString("email");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (userEmail != null && !userEmail.isEmpty()) {
Toast.makeText(getActivity(), userEmail, Toast.LENGTH_LONG).show();
UserEmailLabel.setText(userEmail);
}
}
如何检查 null
和 empty
字符串?