我正在处理我的网络服务,但遇到了一个奇怪的问题。我无法解决这个问题,因为我是 android 开发的新手。我在异步任务的 onPostExecute() 方法中收到空指针异常。当我将结果与字符串进行比较时,就会发生这种情况。
if (result.contains("Duplicate")) or
if (result.equals("Duplicate")) or
if (result.equalsIgnoreCase("Duplicate")) or
这三个都给出错误。我已经通过 System.out.println(result); 打印了结果的值。它显示为空
为了让我的问题变得更奇怪,这种情况有时会发生,尤其是当我的老板测试它时。其他时候效果很好。设备和网络都保持不变。早些时候它只发生在模拟器上。我曾经重新启动 eclipse 并且问题曾经得到解决。
请帮帮我!
代码
private void startDownload() {
new AddTask().execute(FILENAME);
}
public class AddTask extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = ProgressDialog.show(ContactDetails.this, "Loading",
"Please Wait...");
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (result.contains("Duplicate")) { //---- here error comes
int ecolor = -16777216; // whatever color you want
String estring = "Account with this email already exists!!";
ForegroundColorSpan fgcspan = new ForegroundColorSpan(
ecolor);
SpannableStringBuilder ssbuilder = new SpannableStringBuilder(
estring);
ssbuilder.setSpan(fgcspan, 0, estring.length(), 0);
et2.setError(ssbuilder);
dialog.dismiss();
} else {
Toast.makeText(ContactDetails.this, "Account Created!",
Toast.LENGTH_LONG).show();
dialog.dismiss();
myDialog = new Dialog(ContactDetails.this);
myDialog.setContentView(R.layout.email_alert);
myDialog.setTitle("Confirmation");
myDialog.setCancelable(true);
// for OK
Button ok = (Button) myDialog.findViewById(R.id.alertOk);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ContactDetails.this,
Login.class);
startActivity(intent);
}
});
myDialog.show();
}
}
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected String doInBackground(String... params) {
is = null;
individual();
return is;
}
public void individual() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://website.com/contact.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
10);
nameValuePairs.add(new BasicNameValuePair("Name", str1));
nameValuePairs.add(new BasicNameValuePair("Email", str2));
nameValuePairs.add(new BasicNameValuePair("Password", str3));
nameValuePairs.add(new BasicNameValuePair("Address", str5));
nameValuePairs.add(new BasicNameValuePair("Pin", str11));
nameValuePairs.add(new BasicNameValuePair("City", spt1));
nameValuePairs.add(new BasicNameValuePair("State", str12));
nameValuePairs.add(new BasicNameValuePair("phone_visibility",
str_radio));
nameValuePairs.add(new BasicNameValuePair("No", str7));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// httpclient.execute(httppost);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = EntityUtils.toString(entity);
JSONArray jArray = new JSONArray(is);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
- -编辑 - -
结果得到空值的原因是什么?是网络问题吗?有没有办法可以确保我不会得到空结果?如果我得到 null,我的应用程序将无法正常运行