2014-05-25 3 views
-2

마침내 mySQL과 내 안드로이드 앱 간의 연결을 관리합니다. 문제는 예기치 않게 멈추어 앱을 방해하기 위해 네트워크 연결이 끊어지면 토스트를 만들 수 없다는 것입니다. 어떻게 구현할 수있는 아이디어가 있습니까?데이터베이스 연결이 끊어져 예기치 않게 중지되었습니다.

코드 :

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(); 
     } 

    } 

답변

0

doInBackground() 메소드는이 방법 내부 토스트 메시지를 가질 수 없습니다, 따라서 UI 스레드에 백그라운드 스레드에서 실행되지; 방법 내부

사용이 :

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