[英]How to handle network state and HTTP status codes correctly
在一種方法中,我使用HttpURLConnection發送GET和POST請求。 正如您在示例代碼中看到的那樣,我捕獲了大多數異常。 但是我想知道這是否是正確的方法,以及在服務器響應中我應該如何實際處理“無可用網絡”和錯誤的狀態代碼之類的情況? 我問這個問題是因為我想通知用戶各種問題。
public static Bitmap getImage(String url) {
HttpURLConnection conn = null;
try {
URL newUrl = new URL(url);
conn = (HttpURLConnection) newUrl.openConnection();
conn.setDoInput(true);
conn.setDoOutput(false);
conn.setUseCaches(false);
conn.setRequestProperty("Accept-Charset", "utf-8");
conn.setRequestProperty("Content-Type", "image/png");
conn.setRequestMethod("GET");
conn.connect();
int total = conn.getContentLength();
InputStream is = conn.getInputStream();
// ... read the image here!
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (NullPointerException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (conn != null)
conn.disconnect();
if (progress != null)
progress.setProgress(1f);
}
}
用它來檢查網絡是否可用
ConnectivityManager conMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable()
&& conMgr.getActiveNetworkInfo().isConnected()) {
//connection aailable
}else{
//no connection
}
如果可以使用連接,則可以發出http請求;如果以后發生任何連接錯誤,則exception.getMessage()給出可以顯示給用戶的正確錯誤消息
嘗試下面的代碼,並根據需要在下面的類中添加:-
catch (Exception e)
{
e.printStackTrace();
publishProgress(ExceptionHandling.getFormattedMessageFromException(e.getMessage()));
}
異常處理
public class ExceptionHandling
{
public static String exception= "No address associated with hostname";
public static String exceptionReturn = "No Internet Connection";
public static String exceptionTimeOut = "ETIMEDOUT";
public static String exceptionTimeoutReturn = "Connection time out, try again";
public static String exceptionConnectionRefused = "Connection to http://54.254.213.212 refused";
public static String exceptionConRefusedReturn = "Connection lost, please try again...";
public static String exceptionEconnReset = "ECONNRESET";
public static String exceptionSOCKET = "SocketTimeoutException";
public static String exceptionTimeOuturl ="Connect to /154.25.13.12:80 timed out";
public static String exceptionTimeOuturl1 ="ap-southeast-1.compute.amazonaws.com/154.25.13.12:80 timed out";
public static String exceptionTimeOuturlreturn2 ="time out";
public static String exceptionTimeOuturlreturn3 ="refused";
public static String exceptionTimeOuturlreturn ="Connection time out, try again";
public static String exceptionEConnResetReturn = "Trying to connect ... ";
public static String returnVal = "Server connection failed. Please try again.";
public static String getFormattedMessageFromException(String exceptionMessage)
{
try
{
// return returnVal;exceptionSOCKET
if(exceptionMessage.contains(exceptionSOCKET))
{
return exceptionTimeoutReturn;
}
if(exceptionMessage.contains(exception))
{
return exceptionReturn;
}
if(exceptionMessage.contains(exceptionTimeOut))
{
return exceptionTimeoutReturn;
}
if(exceptionMessage.contains(exceptionConnectionRefused))
{
return exceptionConRefusedReturn;
}
if(exceptionMessage.contains(exceptionEconnReset))
{
return exceptionEConnResetReturn;
}
if(exceptionMessage.contains(exceptionTimeOuturl))
{
return exceptionTimeOuturlreturn;
}
if(exceptionMessage.contains(exceptionTimeOuturl1))
{
return exceptionTimeOuturlreturn;
}
if(exceptionMessage.contains(exceptionTimeOuturlreturn2))
{
return exceptionTimeOuturlreturn;
}
if(exceptionMessage.contains(exceptionTimeOuturlreturn3))
{
return exceptionConRefusedReturn;
}
else
{
return exceptionConRefusedReturn;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return exceptionMessage;
}
}
添加更多消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.