2014-01-17 78 views
1

在我的应用程序中,用户必须登录才能访问应用程序。登录详细信息存储在MySQL数据库和php文件中。通常应用程序由于apache服务器而被关闭,并在我的logcat中显示以下详细信息,服务器没有响应时避免强制关闭错误

[<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
01-17 08:59:01.935: I/tagconvertstr1(1160): <html><head> 
01-17 08:59:01.935: I/tagconvertstr1(1160): <title>404 Not Found</title> 
01-17 08:59:01.935: I/tagconvertstr1(1160): </head><body> 
01-17 08:59:01.935: I/tagconvertstr1(1160): <h1>Not Found</h1> 
01-17 08:59:01.935: I/tagconvertstr1(1160): <p>The requested URL  /bcreasearchT/Service/loginresponse.php was not found on this server.</p> 
01-17 08:59:01.935: I/tagconvertstr1(1160): <hr> 
01-17 08:59:01.935: I/tagconvertstr1(1160): <address>Apache/2.2.15 (CentOS) Server at www.medsmonit.com Port 80</address> 
01-17 08:59:01.935: I/tagconvertstr1(1160): </body></html> 
01-17 08:59:01.935: I/tagconvertstr1(1160): ] 
01-17 08:59:01.945: E/JSON Parser(1160): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject 
01-17 08:59:01.955: I/Choreographer(1160): Skipped 40 frames! The application may be doing too much work on its main thread. 
01-17 08:59:01.965: I/tagconvertstr3(1160): [null] 
01-17 08:59:01.965: W/dalvikvm(1160): threadid=14: thread exiting with uncaught exception (group=0x40a71930) 
01-17 08:59:01.995: I/Choreographer(1160): Skipped 31 frames! The application may be doing too much work on its main thread. 
01-17 08:59:02.025: E/AndroidRuntime(1160): FATAL EXCEPTION: AsyncTask #3 
01-17 08:59:02.025: E/AndroidRuntime(1160): java.lang.RuntimeException: An error occured while executing doInBackground() 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at android.os.AsyncTask$3.done(AsyncTask.java:299) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at java.lang.Thread.run(Thread.java:856) 
01-17 08:59:02.025: E/AndroidRuntime(1160): Caused by: java.lang.NullPointerException 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at com.example.sms.LoginActivity$AttemptLogin.doInBackground(LoginActivity.java:280) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at com.example.sms.LoginActivity$AttemptLogin.doInBackground(LoginActivity.java:1) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
01-17 08:59:02.025: E/AndroidRuntime(1160):  ... 4 more 

我该如何纠正它?任何人都可以帮助我!

UPDATE:

这是我aynctask代码,

class AttemptLogin extends AsyncTask<String, String, String> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     username = (EditText) findViewById(R.id.mobnum); 
      passw = (EditText) findViewById(R.id.password); 
      uname = username.getText().toString(); 

       password = passw.getText().toString();  

     pDialog = new ProgressDialog(LoginActivity.this); 
     pDialog.setMessage("VALIDATING USER..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(false); 
     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 


      proInfo.clear(); 

     List<NameValuePair> params1 = new ArrayList<NameValuePair>(); 

     params1.add(new BasicNameValuePair("username", uname)); 
     params1.add(new BasicNameValuePair("pswd", password)); 

     JsonParser jLogin = new JsonParser(); 
     JSONObject json = jLogin.makeHttpRequest(url,"POST", params1); 


     Log.i("tagconvertstr3", "["+json+"]"); 
     try 
     { 

      JSONObject jUser = json.getJSONObject(TAG_SRESL); 
      userid = jUser.getString(TAG_USERID); 

      successL = jUser.getString(TAG_SUCCESS1); 

      proInfo.add(userid); 


      } 

     catch(JSONException e) 
     { 
      e.printStackTrace(); 

     } 


     return null; 
    } 

    protected void onPostExecute(String file_url) { 


     if(successL.equalsIgnoreCase("Yes")) 
     { 
      usrname=uname; 
      new ViewProfile().execute(); 

     } 

     else{ 
     final Dialog dialog = new Dialog(context); 
      dialog.setContentView(R.layout.custom_dialog); 
      dialog.setTitle("Login Failed"); 
      dialog.setCancelable(false); 
      dialog.setCanceledOnTouchOutside(false); 
      TextView txt = (TextView) dialog.findViewById(R.id.errorlog); 
       txt.setText("Incorrect username or password!"); 
       Button dialogButton = (Button) dialog.findViewById(R.id.release); 
       dialogButton.setOnClickListener(new OnClickListener() { 
        public void onClick(View vd) { 
         dialog.dismiss(); 

      } 
      }); 
       dialog.show(); 
       pDialog.dismiss(); 
      } 
     } 


    } 

这是我的JSON解析器类,

public JSONObject makeHttpRequest(String url, String method, 
     List<NameValuePair> params) { 


    try { 

     if(method.equalsIgnoreCase("POST")){ 


      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

      System.out.println(is); 

      System.out.println("getting the content"); 

     }else if(method.equalsIgnoreCase("GET")){ 

      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      String paramString = URLEncodedUtils.format(params, "utf-8"); 
      url += "?" + paramString; 
      HttpGet httpGet = new HttpGet(url); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 
     }   

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


    System.out.println("getting all the values "); 

    try {   
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 


     StringBuilder sb = new StringBuilder(); 
     String line = null; 

     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 

     is.close(); 

     json = sb.toString();  


    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 


    try { 

     Log.i("tagconvertstr1", "["+json+"]"); 

     jsonObject = new JSONObject(json); 
      System.out.println("json object parse finished"); 

     Log.i("tagconvertstr2","["+json+"]"); 




    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 




    return jsonObject; 

} 






public JSONObject getJSONFromUrl(String url) { 

      try { 

       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent();   

      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      try { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(
         is, "iso-8859-1"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       json = sb.toString(); 
      } catch (Exception e) { 
       Log.e("Buffer Error", "Error converting result " + e.toString()); 
      } 

      try { 
       Log.i("tagconvertstr", "["+json+"]"); 
       jsonObject = new JSONObject(json); 
       Log.i("tagconvertstr", "["+json+"]"); 

      } catch (JSONException e) { 
       Log.e("JSON Parser", "Error parsing data " + e.toString()); 
      } 


      return jsonObject; 

} 
+1

post'LoginActivity.java'并指示280行 – Raghunandan

+0

在这里发布您的AsyncTask代码...或者在使用该对象之前保留一个条件来检查对象是否为空.. –

+0

什么是第280行? – Raghunandan

回答

1
JSONObject jUser = json.getJSONObject(TAG_SRESL);//This is mt 280th line 

您的json为空。开始调试。 jLogin.makeHttpRequest(url,"POST", params1);返回null。

而且移动(最好)

username = (EditText) findViewById(R.id.mobnum); 
passw = (EditText) findViewById(R.id.password); 

onCreate

您可以直接通过PARAMS到的AsyncTask doInbackground

new AttemptLogin().execute(username.getText().toString(),passw.getText().toString()); 
在doInbackground

然后()

//String... params 
params[0] is username 
params[1] is password 

您同时还使用JSON对象之前缺少

@Override 
protected void onPostExecute(String file_url) { 
super.onPostExecute(file_url); 
+0

即,必须将返回的对象比较为空 – Barbie

+0

@vimala nope它只是响应表单服务器为空。应检查jsonparser代码 – Raghunandan

+0

@vimala发布JsonParser代码。 – Raghunandan

0

我认为你应该从服务器检查响应代码和ACT催化剂的因为它接缝了json创建失败,并且可能导致nullpointer。

+1

我试图通过比较null与响应..但它不起作用 – Barbie

+0

@vimala响应代码是int,因此不能等于null。检查int值并基于该值进行操作。 – user3182577

0

Kepp条件。在doInBackground中替换你的代码。

try { 
    if(json != null){ 
     JSONObject jUser = json.getJSONObject(TAG_SRESL); 
     userid = jUser.getString(TAG_USERID); 
     successL = jUser.getString(TAG_SUCCESS1); 
     proInfo.add(userid); 
    } 
}catch(JSONException e) { 
    e.printStackTrace(); 
} 

在检查equalsIgnoreCase之前检查字符串,替换onPostExecute代码。

if(successL != null && successL.equalsIgnoreCase("Yes")) { 
    usrname=uname; 
    new ViewProfile().execute(); 
} 

请试试看。

+0

检查是好的,但真正的问题是json返回的是null,应该首先被修复 – Raghunandan

+0

@vishesh chandra ..谢谢它的作品,但在我的后期执行我无法正确配置... – Barbie

+0

successL是字符串如何你可以比较它!= ??/ – Barbie

相关问题