没有互联网连接时应用崩溃

时间:2015-05-19 02:43:37

标签: java android eclipse android-asynctask

所以我试图创建一个连接到我的wampserver的简单登录应用程序,通过互联网上的教程,它工作正常,但问题发生在我的设备没有连接到互联网,我无法弄清楚为什么,但我很确定问题出在我的asynctask中。 所以我需要帮助,谢谢。

这是我的login.java

package id.wanda.smkkkristenimmanuelii;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity implements OnClickListener {

    private EditText user, pass;
    private Button mSubmit;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    private static final String LOGIN_URL = "http://192.168.1.110:80/smkkimmanuel2/login.php";


    // JSON element ids from repsonse of php script:
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_MESSAGE = "message";
    private static final String TAG_LEVEL_AKSES = "level_akses";
    private static final String TAG_JABATAN = "jabatan";

    public static String level_akses = "5", jabatan = "", namaUser = "";
    public static String login_status = "false";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // setup input fields
        user = (EditText) findViewById(R.id.username);
        pass = (EditText) findViewById(R.id.password);

        // setup buttons
        mSubmit = (Button) findViewById(R.id.btnLogin);

        // register listeners
        mSubmit.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        Login.this.finish();
        startActivity(new Intent(this, MenuUtama.class));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnLogin:
            new AttemptLogin().execute();
            break;
        default:
            break;
        }
    }

    class AttemptLogin extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        boolean failure = false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Login.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;
            String levelaks, jabat;
            String username = user.getText().toString();
            String password = pass.getText().toString();
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("username", username));
                params.add(new BasicNameValuePair("password", password));

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                        params);

                // check your log for json response
                Log.d("Login attempt", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    Log.d("Login Successful!", json.toString());

                    levelaks = json.getString(TAG_LEVEL_AKSES);
                    jabat = json.getString(TAG_JABATAN);

                    level_akses = levelaks;
                    jabatan = jabat;
                    namaUser = username;

                    login_status = "true";

                    Intent i = new Intent(Login.this, MenuUtama.class);
                    finish();
                    startActivity(i);
                    return json.getString(TAG_MESSAGE);
                } else {
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);

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

            return null;
        }
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
        }

    }


}

这是我的JSONParser.java:

package id.wanda.smkkkristenimmanuelii;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }


    public JSONObject getJSONFromUrl(final String url) {

        // Making HTTP request
        try {
            // Construct the client and the HTTP request.
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            // Execute the POST request and store the response locally.
            HttpResponse httpResponse = httpClient.execute(httpPost);
            // Extract data from the response.
            HttpEntity httpEntity = httpResponse.getEntity();
            // Open an inputStream with the data content.
            is = httpEntity.getContent();

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

        try {
            // Create a BufferedReader to parse through the inputStream.
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            // Declare a string builder to help with the parsing.
            StringBuilder sb = new StringBuilder();
            // Declare a string to store the JSON object data in string form.
            String line = null;

            // Build the string until null.
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            // Close the input stream.
            is.close();
            // Convert the string builder data to an actual string.
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // Try to parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // Return the JSON Object.
        return jObj;

    }


    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                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();

            }else if(method == "GET"){
                // request method is 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();
        }

        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 parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

这是我的崩溃报告:

05-19 09:56:44.040: E/AndroidRuntime(24861): FATAL EXCEPTION: AsyncTask #1
05-19 09:56:44.040: E/AndroidRuntime(24861): Process: id.wanda.smkkkristenimmanuelii, PID: 24861
05-19 09:56:44.040: E/AndroidRuntime(24861): java.lang.RuntimeException: An error occured while executing doInBackground()
05-19 09:56:44.040: E/AndroidRuntime(24861):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at java.lang.Thread.run(Thread.java:841)
05-19 09:56:44.040: E/AndroidRuntime(24861): Caused by: java.lang.NullPointerException
05-19 09:56:44.040: E/AndroidRuntime(24861):    at id.wanda.smkkkristenimmanuelii.Login$AttemptLogin.doInBackground(Login.java:138)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at id.wanda.smkkkristenimmanuelii.Login$AttemptLogin.doInBackground(Login.java:1)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-19 09:56:44.040: E/AndroidRuntime(24861):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-19 09:56:44.040: E/AndroidRuntime(24861):    ... 4 more
05-19 09:56:44.040: W/ActivityManager(571):   Force finishing activity id.wanda.smkkkristenimmanuelii/.Login
05-19 09:56:44.050: E/IMGSRV(192): :0: PVRDRMOpen: TP3, ret = 107

5 个答案:

答案 0 :(得分:1)

解决此问题的最佳方法是在尝试执行任何需要连接的操作之前,使用ConnectivityManager确定您的设备是否在线/是否已连接到互联网。

这样,您可以在离线时显示建议的屏幕,或者在线时尝试运行代码。从用户的角度来看,这有利于您的应用在没有可用连接时崩溃。

答案 1 :(得分:1)

问题是makeHttpRequest返回null

// getting product details by making HTTP request
 JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);

你可以简单地为你的try / catch添加额外的捕获

catch (NullPointerException e) {
   e.printStackTrace();
}

可能更好的做法是在发出请求之前检查与ConnectivityManager的连接

答案 2 :(得分:0)

更改 private static final String LOGIN_URL = "http://192.168.1.110:80/smkkimmanuel2/login.php";  至 private static final String LOGIN_URL = "http://127.0.0.1:80/smkkimmanuel2/login.php";

此问题的原因是您的IP绑定到该连接,当您未连接时,它不再是您的IP。

答案 3 :(得分:0)

避免崩溃的最佳方法是检查设备是否连接到互联网。

*ifConnected * : Do your process
*ifNotConnected *: Then display a dialog box to user that he/she is not connected to the internet.

这就是我在开发中所做的。 这可能会对您有所帮助: -

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager 
      = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
 } 

您需要添加

  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

答案 4 :(得分:0)

现在问题解决了,感谢@Karl和@Modge,

首先,感谢@Modge,为了防止应用程序崩溃,只需在AttempLogin类中添加一个catch异常,在Login.java中添加doInBackground方法,它将继续显示进程对话框&#34;尝试登录&#34; :

&#13;
&#13;
protected String doInBackground(String... args) {
			// TODO Auto-generated method stub
			// Check for success tag
			int success;
			String levelaks, jabat;
			String username = user.getText().toString();
			String password = pass.getText().toString();
			try {
				// Building Parameters
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("username", username));
				params.add(new BasicNameValuePair("password", password));

				Log.d("request!", "starting");
				// getting product details by making HTTP request
				JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
						params);

				// check your log for json response
				Log.d("Login attempt", json.toString());

				// json success tag
				success = json.getInt(TAG_SUCCESS);
				if (success == 1) {
					Log.d("Login Successful!", json.toString());

					levelaks = json.getString(TAG_LEVEL_AKSES);
					jabat = json.getString(TAG_JABATAN);

					level_akses = levelaks;
					jabatan = jabat;
					namaUser = username;

					login_status = "true";

					Intent i = new Intent(Login.this, MenuUtama.class);
					finish();
					startActivity(i);
					return json.getString(TAG_MESSAGE);
				} else {
					Log.d("Login Failure!", json.getString(TAG_MESSAGE));
					return json.getString(TAG_MESSAGE);

				}
			} catch (JSONException e) {
				e.printStackTrace();
			}catch (NullPointerException e) {
				   e.printStackTrace();
                                      //THIS IS IT.
			}

			return null;
		}
&#13;
&#13;
&#13;

要完成应用程序,我按照@Karl的建议添加ConnectivityManager,遵循此处的教程http://www.androidhive.info/2012/07/android-detect-internet-connection-status/。在创建ConnectionDetector类之后,我调整了我的Login类,它将是这样的:

&#13;
&#13;
package id.wanda.smkkkristenimmanuelii;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity implements OnClickListener {

	private EditText user, pass;
	private Button mSubmit;
	
  //ADDED
	// flag for Internet connection status
    Boolean isInternetPresent = false;  
    // Connection detector class
    ConnectionDetector cd;

	// Progress Dialog
	private ProgressDialog pDialog;

	// JSON parser class
	JSONParser jsonParser = new JSONParser();

	// php login script location:
	private static final String LOGIN_URL = "http://192.168.1.110:80/smkkimmanuel2/login.php";


	// JSON element ids from repsonse of php script:
	private static final String TAG_SUCCESS = "success";
	private static final String TAG_MESSAGE = "message";
	private static final String TAG_LEVEL_AKSES = "level_akses";
	private static final String TAG_JABATAN = "jabatan";

	public static String level_akses = "5", jabatan = "", namaUser = "";
	public static String login_status = "false";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);

		// setup input fields
		user = (EditText) findViewById(R.id.username);
		pass = (EditText) findViewById(R.id.password);
      
      //ADDED
		cd = new ConnectionDetector(getApplicationContext());

		// setup buttons
		mSubmit = (Button) findViewById(R.id.btnLogin);

		// register listeners
		mSubmit.setOnClickListener(this);
	}

	@Override
	public void onBackPressed() {
		Login.this.finish();
		startActivity(new Intent(this, MenuUtama.class));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnLogin:
			//ADDED
            // get Internet status
            isInternetPresent = cd.isConnectingToInternet();

            // check for Internet status
            if (isInternetPresent) {
                // Internet Connection is Present
                // make HTTP requests
                showAlertDialog(Login.this, "Internet Connection",
                        "You have internet connection", true);

    			/*new AttemptLogin().execute();*/
            } else {
                // Internet connection is not present
                // Ask user to connect to Internet
                showAlertDialog(Login.this, "No Internet Connection",
                        "You don't have internet connection.", false);
            }
			break;
		default:
			break;
		}
	}
	
  //ADDED
	@SuppressWarnings("deprecation")
	public void showAlertDialog(Context context, String title, String message, Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
 
        // Setting Dialog Title
        alertDialog.setTitle(title);
 
        // Setting Dialog Message
        alertDialog.setMessage(message);
         
        // Setting alert dialog icon
        alertDialog.setIcon((status) ? R.drawable.success : R.drawable.failed);
 
        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
 
        // Showing Alert Message
        alertDialog.show();
    }

	class AttemptLogin extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		boolean failure = false;

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(Login.this);
			pDialog.setMessage("Attempting login...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected String doInBackground(String... args) {
			// TODO Auto-generated method stub
			// Check for success tag
			int success;
			String levelaks, jabat;
			String username = user.getText().toString();
			String password = pass.getText().toString();
			try {
				// Building Parameters
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new BasicNameValuePair("username", username));
				params.add(new BasicNameValuePair("password", password));

				Log.d("request!", "starting");
				// getting product details by making HTTP request
				JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
						params);

				// check your log for json response
				Log.d("Login attempt", json.toString());

				// json success tag
				success = json.getInt(TAG_SUCCESS);
				if (success == 1) {
					Log.d("Login Successful!", json.toString());

					levelaks = json.getString(TAG_LEVEL_AKSES);
					jabat = json.getString(TAG_JABATAN);

					level_akses = levelaks;
					jabatan = jabat;
					namaUser = username;

					login_status = "true";

					Intent i = new Intent(Login.this, MenuUtama.class);
					finish();
					startActivity(i);
					return json.getString(TAG_MESSAGE);
				} else {
					Log.d("Login Failure!", json.getString(TAG_MESSAGE));
					return json.getString(TAG_MESSAGE);

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

			return null;
		}
	}

	/**
	 * After completing background task Dismiss the progress dialog
	 * **/
	protected void onPostExecute(String file_url) {
		// dismiss the dialog once product deleted
		pDialog.dismiss();
		if (file_url != null) {
			Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
		}

	}
	

}
&#13;
&#13;
&#13;

它的功能就像一个魅力,谢谢你的帮助。我真的很感激。