断开连接到数据库时意外停止

时间:2014-05-25 14:44:50

标签: java android mysql json try-catch

我终于设法在mySQL和我的Android应用程序之间建立连接。问题是,当网络断开连接时,我无法设置任何Toast,以通过意外停止来阻止应用程序。我有什么想法可以实现吗?

代码:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_layout_one, container, false);
    listView = (ListView) view.findViewById(R.id.listView1);

    accessWebService();
    return view;
}

// Async Task to access the web
    public class JsonReadTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(
                        response.getEntity().getContent()).toString();
            }
            catch (ClientProtocolException e) {
                //e.printStackTrace();
                Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                        Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                //e.printStackTrace();
                Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                        Toast.LENGTH_LONG).show();
            }
            catch (Exception e){
                //Log.e("log_tag", "Error in http connection"+e.toString());
                Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                        Toast.LENGTH_LONG).show();
            }
            return null;
        }

        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));

            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            }
            catch (IOException e) {
                //e.printStackTrace();
                Toast.makeText(getActivity(),
                        "Error..." + e.toString(), Toast.LENGTH_LONG).show();
            }
            return answer;
        }

        @Override
        protected void onPostExecute(String result) {
           ListDrwaer();
        }
    }// end async task

    public void accessWebService() {

        try{
            JsonReadTask task = new JsonReadTask();
            // passes values for the urls string array
            task.execute(new String[] { url });
        }
        catch (Exception e){
            Toast.makeText(getActivity(), "this is my Toast message!!! =)",
                    Toast.LENGTH_LONG).show();
        }

    }

1 个答案:

答案 0 :(得分:0)

doInBackground()方法在后台线程中运行,而不是在UI线程上运行,因此,您不能在此方法中使用Toast消息;

在方法中使用:

runOnUiThread(new Runnable() {     
    public void run() {
      Toast.makeText(getApplicationContext(), "Example for Toast", Toast.LENGTH_SHORT).show();    
       }
    });