我正在处理我的网络服务,我遇到了一个奇怪的问题。我无法解决这个问题,因为我是Android开发新手。我在Async Task的onPostExecute()方法中得到空指针异常。当我将结果与字符串进行比较时会发生这种情况。
if (result.contains("Duplicate")) or
if (result.equals("Duplicate")) or
if (result.equalsIgnoreCase("Duplicate")) or
这三个都给出了错误。我已经打印了System.out.println(result)的结果值;它显示 null
为了让我的问题更奇怪,这种情况有时只会在我的老板测试时发生。其他时候它运作得非常好。设备和网络都保持不变。之前它曾经只在模拟器上发生过。我曾经重启过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();
}
}
--- --- EDIT
获得null值的原因是什么?这是网络问题吗? 有没有办法可以确保我不会得到空结果? 如果我得到null,我的应用程序将无法正常运行
答案 0 :(得分:2)
检查您是否从结果中获取数据......
if(result!=null){
// here check the things you need
}
它对我有用
答案 1 :(得分:1)
检查result
是否为null
..如果不是,则仅执行您的操作..如下所示..
if(result!=null){
if (result.contains("Duplicate")) or
if (result.equals("Duplicate")) or
if (result.equalsIgnoreCase("Duplicate")) or
}
答案 2 :(得分:1)
您在individual()
中遇到了许多例外情况。当其中一个发生时,您的result
可能仍为空,但您不会知道。
你应该:
更改异常捕获代码并存储发生异常的其他信息,并在onPostExecute
中使用该信息。
只需接受result
可以为null并在onPostExecute
中正确处理(检查null并执行一些适当的操作)。
答案 3 :(得分:1)
在结果
上添加NPE检查@Override
protected void onPostExecute(String result)
{
if(result == null)
{
// handle null result, debug or whatever
return;
}
....
//rest of your code
}
修改强> 我相信这是你的问题
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = EntityUtils.toString(entity);
响应可能会获得空值,因此response.getEntity();
会引发异常,并且is = EntityUtils.toString(entity);
将会到达,因此它将具有空值
并且在doInBackground中,您返回is
,这是空的:)所以您需要处理该空值并接受有时您将获得空值的事实。欢呼声
答案 4 :(得分:0)
protected String doInBackground
返回 String
您将返回 null 。
onPostExecute(String result)
处理 String 。
您无法在 null 上调用&#34;包含&#34; 。
修复:
@Override
protected String doInBackground(String... params) {
is = individual();
return is;
}
public String individual() {
...将您的JSON数据放入String ...