2016-04-15 14 views
0

hellow there。私はリモートサーバーに基づいているアプリを持っています。私のアプリはサーバーが動作していて、サーバーが利用できない場合はクラッシュします。localhostでアプリケーションをテストしていますので、処理したい、非同期タスク用のコードは以下のとおりです。 PostExecute使用中サーバーが利用できない場合のアプリの処理方法

public class UserBackgroundTask extends AsyncTask<String, Void, String> { 
    Context context; 
    public ProgressDialog progressDialog; 

    public UserBackgroundTask(Context context) { 
     this.context = context; 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = new ProgressDialog(MainActivity.this); 
     progressDialog.setMessage("Verifying Username and Password..."); 
     progressDialog.show(); 
    } 


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

     String login_url = "http://10.0.3.2/newRsNepal/Login.php"; 
     HttpURLConnection httpURLConnection = null; 
     String method = params[0]; 

     if (method.equals("login")) { 
      String login_user = params[1]; 
      String login_password = params[2]; 

      try { 
       URL url = new URL(login_url); 
       httpURLConnection = (HttpURLConnection) url.openConnection(); 
       httpURLConnection.setRequestMethod("POST"); 
       httpURLConnection.setDoOutput(true); 
       httpURLConnection.setDoInput(true); 
       OutputStream outputStream = httpURLConnection.getOutputStream(); 
       BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8")); 
       String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(login_user, "UTF-8") + "&" + 
         URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(login_password, "UTF-8"); 
       bufferedWriter.write(data); 
       bufferedWriter.flush(); 
       bufferedWriter.close(); 
       outputStream.close(); 
       InputStream inputStream = httpURLConnection.getInputStream(); 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1")); 
       String response = ""; 
       String line = ""; 
       while ((line = bufferedReader.readLine()) != null) { 
        response += line; 
       } 
       bufferedReader.close(); 
       inputStream.close(); 
       httpURLConnection.disconnect(); 
       return response; 
      } catch (MalformedURLException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 

     if (result.equalsIgnoreCase("1")) { 
      progressDialog.dismiss(); 
      Intent intent = new Intent(getApplicationContext(), InnerActivity.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Add new Flag to start new Activity 
      startActivity(intent); 
      finish(); 
     } else { 
      progressDialog.dismiss(); 
      Toast.makeText(context, "Invalid username and Password", Toast.LENGTH_LONG).show(); 
      textUser.setText(""); 
      textUser.setText(""); 
     } 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
     super.onProgressUpdate(values); 
    } 
} 
+0

... 場合(結果== NULL){ トースト.makeText(YourActivity.this、 "Your msg"、Toast.LENGTH_SHORT).show(); } – Mohit

答えて

2

このコード:

if (result.length>1) { 
    Toast.makeText(YourActivity.this,"Your msg", Toast.LENGTH_SHORT).show(); 
} 

または

if (result.size>1) { 
    Toast.makeText(YourActivity.this,"Your msg", Toast.LENGTH_SHORT).show(); 
    } 

または

MainActivityファイルのコードの下に置く
if (result==null) { 
    } 
+0

をフェッチする前に、 '503サービスUnavailable'要求コードを確認することができ –

0

//this function check internet connection 
    private boolean isNetworkAvailable() { 
       ConnectivityManager connectivityManager 
         = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
       NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
       return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
      } 

//put your code here 
if(isNetworkAvailable() == true) 
{ 
     //internet connection available put your code here 
     new UserBackgroundTask().execute(); 
} 
else 
{ 
     //internet connection not available  
} 
あなたは私はあなたの概念を使用して作られたいくつかの変更をし、最終的に問題が解決したしたデータ...
+0

作業中のバディ –

+0

アクティビティコードのアップロード... –

関連する問題