Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 需要了解Android Studio检查的原因”;“条件总是真实的”;在我引用的代码中_Java_Android_Android Studio - Fatal编程技术网

Java 需要了解Android Studio检查的原因”;“条件总是真实的”;在我引用的代码中

Java 需要了解Android Studio检查的原因”;“条件总是真实的”;在我引用的代码中,java,android,android-studio,Java,Android,Android Studio,代码如下: Pair<Boolean, String> updateServer() { final String LOG_TAG = "WS.UST.updateServer"; Pair<Boolean, String> retVal = null; URL url; String sRawResponse = null; JSONObject joResponse = nul

代码如下:

    Pair<Boolean, String> updateServer() {
        final String LOG_TAG = "WS.UST.updateServer";

        Pair<Boolean, String> retVal = null;

        URL url;
        String sRawResponse = null;
        JSONObject joResponse = null;
        HttpURLConnection connection = null;
        try {
            url = new URL("http://www.abc.in/v3.php");
            connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(TIMEOUT);
            connection.setReadTimeout(TIMEOUT);
            connection.setDoInput(true);

            final int response = connection.getResponseCode();
            if (response != HttpURLConnection.HTTP_OK)
                throw new IOException("Server response code (" + String.valueOf(response) + ") is not OK.");

            InputStreamReader isReader = new InputStreamReader(connection.getInputStream());
            StringBuilder s = new StringBuilder();
            char[] cBuf = new char[1024];
            for (int cRead = isReader.read(cBuf, 0, 1024); cRead != -1; cRead = isReader.read(cBuf, 0, 1024))
                s.append(cBuf, 0, cRead);
            isReader.close();

            sRawResponse = s.toString();
            joResponse = new JSONObject(s.toString());

            retVal = new Pair<>(joResponse.optBoolean("success"), joResponse.optString("message"));
        } catch (IOException | JSONException e) {
            Log.d(LOG_TAG, sRawResponse == null ? "null" : sRawResponse);
            Log.d(LOG_TAG, joResponse == null ? "null" : joResponse.toString());
            e.printStackTrace();
            retVal = new Pair<>(false, e.getMessage());
        } catch (Exception e) {
            retVal = new Pair<>(false, e.getMessage());
        } finally {
            if (connection != null)
                connection.disconnect();
        }
        return retVal;
    }
配对更新服务器(){
最终字符串LOG_TAG=“WS.UST.updateServer”;
Pair retVal=null;
网址;
字符串sRawResponse=null;
JSONObject joResponse=null;
HttpURLConnection=null;
试一试{
url=新url(“http://www.abc.in/v3.php");
connection=(HttpURLConnection)url.openConnection();
connection.setConnectTimeout(超时);
connection.setReadTimeout(超时);
connection.setDoInput(true);
final int response=connection.getResponseCode();
if(response!=HttpURLConnection.HTTP\u OK)
抛出新IOException(“服务器响应代码(“+String.valueOf(response)+”)不正常。”);
InputStreamReader isReader=新的InputStreamReader(connection.getInputStream());
StringBuilder s=新的StringBuilder();
char[]cBuf=新字符[1024];
for(int cRead=isReader.read(cBuf,0,1024);cRead!=-1;cRead=isReader.read(cBuf,0,1024))
s、 追加(cBuf,0,cRead);
isReader.close();
sRawResponse=s.toString();
joResponse=新的JSONObject(s.toString());
retVal=新对(joResponse.optBoolean(“成功”),joResponse.optString(“消息”);
}捕获(IOException | JSONException e){
Log.d(Log_标记,sRawResponse==null?“null”:sRawResponse);
Log.d(Log_标记,joResponse==null?“null”:joResponse.toString());
e、 printStackTrace();
retVal=新对(false,例如getMessage());
}捕获(例外e){
retVal=新对(false,例如getMessage());
}最后{
if(连接!=null)
连接断开();
}
返回返回;
}

Android Studio正在显示警告
值'joResponse'总是'null'更多。。。(Ctrl+F1)
在第一个catch块的第二行,我不明白为什么。有人能帮我理解警告的原因吗?

这有点简单,但需要注意可能出现异常的流程。让我们讨论一下代码中引发异常的流程

您在第一个
catch
block
IOException&jsoneexception
中添加了两个异常。最初,您有
joResponse=null
,现在代码中向下移动
IOException
可以由
InputStreamReader
生成,到目前为止,您还没有为
joResponse
分配任何值。此外,当您试图从
String
创建
JSONObject
时,会引发
JSONException
,因为此行将引发异常,因此
joResponse
在此也不会被再次赋值


因此,如果您的程序曾经到达第一个
catch
块,那么它肯定
joResponse
将是
null
。因此,Android Studio发出了警告

这有点简单,但需要注意可能出现异常的流程。让我们讨论一下代码中引发异常的流程

您在第一个
catch
block
IOException&jsoneexception
中添加了两个异常。最初,您有
joResponse=null
,现在代码中向下移动
IOException
可以由
InputStreamReader
生成,到目前为止,您还没有为
joResponse
分配任何值。此外,当您试图从
String
创建
JSONObject
时,会引发
JSONException
,因为此行将引发异常,因此
joResponse
在此也不会被再次赋值


因此,如果您的程序曾经到达第一个
catch
块,那么它肯定
joResponse
将是
null
。因此,Android Studio发出了警告

这是因为,如果发生IOException,代码将停止在“newURL(…);”或connection.getResponseCode()等处执行。任何IOException都将在分配joResponse之前发生。因此,在IOException捕获块中,joResponse始终为null。若发生了JSONException,则在赋值时发生了一些事情,因此joResponse为null

这是因为,如果发生IOException,代码将停止在“newURL(…);”或connection.getResponseCode()等处执行。任何IOException都将在分配joResponse之前发生。因此,在IOException捕获块中,joResponse始终为null。若发生了JSONException,则在赋值时发生了一些事情,因此joResponse为null

IOException | jsoneexception
(如果发生)总是在代码中初始化
joResponse
之前发生。因此发出警告

IOException | jsoneexception
(如果发生)总是在代码中初始化
joResponse
之前发生。因此发出警告

多谢各位。没想到Android Studio会如此彻底地检查代码@YogeshM,分析并没有那么复杂:特别是当两个异常都被检查时,编译器必须检查它们是否存在,以跟踪它们是否被声明为方法
抛出或捕获并重新抛出。而且,既然这些信息都是可用的,那么就拿着它来指出不那么严重的问题是一种生活质量。谢谢。没想到Android Studio会如此彻底地检查代码@YogeshM,分析并没有那么复杂:特别是当两个异常都被检查时,编译器必须检查它们是否存在,以跟踪它们是否被声明为方法
抛出或捕获并重新抛出。而且,由于这些信息无论如何都是可用的,因此,将其用于poin是一种生活质量