Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.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 检查If-else块中的条件_Java_Android - Fatal编程技术网

Java 检查If-else块中的条件

Java 检查If-else块中的条件,java,android,Java,Android,在我的程序中,我想在将图像上载到服务器时显示三种状态,状态为: 上传完成 上载失败 已经存在 我仍然在代码中实现了Upload Done和Failed条件,但不知道在If else块中已经存在需要在何处以及如何实现 String strStatusID = "0"; String strError = ""; try { JSONObject c = new JSONObject(resServer); strStatusID = c.ge

在我的程序中,我想在将图像上载到服务器时显示三种状态,状态为:

  • 上传完成

  • 上载失败

  • 已经存在

  • 我仍然在代码中实现了Upload Done和Failed条件,但不知道在If else块中已经存在需要在何处以及如何实现

        String strStatusID = "0";
        String strError = "";
    
        try {      
    
        JSONObject c = new JSONObject(resServer);
        strStatusID = c.getString("StatusID");
        strError = c.getString("Message");
        } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    
        // Prepare Status
        if(strStatusID.equals("0"))
        {   
            status.setImageResource(R.drawable.upload_failed);
            ImageButton btnUpload = (ImageButton) v.findViewById(R.id.btnUpload);
            btnUpload.setEnabled(true);
        }
        else
        {
            status.setImageResource(R.drawable.upload_done);
        }
    
        }
    
    编辑的代码:-

        String strStatusID = "0" ;
        String strError = "" ;
    
            try {      
    
            JSONObject c = new JSONObject(resServer);
            strStatusID = c.getString("StatusID");
            strError = c.getString("Message");
            } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
    
    
            if(strStatusID.equals("0"))
            {   
                status.setImageResource(R.drawable.upload_failed);
                ImageButton btnUpload = (ImageButton) v.findViewById(R.id.btnUpload);
                btnUpload.setEnabled(true);
            }
            else if((strStatusID.equals("1"))   // Syntax error on token ")", ) expected after this token
            {
              status.setImageResource(R.drawable.upload_done);
            }
            else
            {
                status.setImageResource(R.drawable.already_exist);
            }
    
            }
    

    如果

    if(strStatusID.equals("0"){
        ...
    } 
    else if(strStatusID.equals("1")){
        ...
    } 
    else {
       ...
    }
    

    如果使用
    enum
    作为状态,并使用
    switch
    语句,您的代码会更好。

    如果

    if(strStatusID.equals("0"){
        ...
    } 
    else if(strStatusID.equals("1")){
        ...
    } 
    else {
       ...
    }
    
     if(strStatusID.equals("0"))
                     {   
                        status.setImageResource(R.drawable.upload_failed);
                        ImageButton btnUpload = (ImageButton) v.findViewById(R.id.btnUpload);
                        btnUpload.setEnabled(true);
                     }
                     else if((strStatusID.equals("1")
                    {
                      status.setImageResource(R.drawable.alreadyexist);
                    }
                    else
                    {
                        status.setImageResource(R.drawable.upload_done);
                    }
    

    如果使用
    enum
    作为状态和
    switch
    语句,代码会更好。

    而不是if-else条件使用if-elseif条件

     if(strStatusID.equals("0"))
                     {   
                        status.setImageResource(R.drawable.upload_failed);
                        ImageButton btnUpload = (ImageButton) v.findViewById(R.id.btnUpload);
                        btnUpload.setEnabled(true);
                     }
                     else if((strStatusID.equals("1")
                    {
                      status.setImageResource(R.drawable.alreadyexist);
                    }
                    else
                    {
                        status.setImageResource(R.drawable.upload_done);
                    }
    
     String strStatusID = "0";
        String strError = "";
    
        try {      
    
        JSONObject c = new JSONObject(resServer);
        strStatusID = c.getString("StatusID");
        strError = c.getString("Message");
        } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    
        // Prepare Status
        if(strStatusID.equals("0"))
        {   
            status.setImageResource(R.drawable.upload_failed);
            ImageButton btnUpload = (ImageButton) v.findViewById(R.id.btnUpload);
            btnUpload.setEnabled(true);
        }
        else   if(strStatusID.equals("1"))
        {
            status.setImageResource(R.drawable.upload_done);
        }
    else{
    
    }
        }
    

    使用if-else条件代替if-elseif条件

     String strStatusID = "0";
        String strError = "";
    
        try {      
    
        JSONObject c = new JSONObject(resServer);
        strStatusID = c.getString("StatusID");
        strError = c.getString("Message");
        } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
    
        // Prepare Status
        if(strStatusID.equals("0"))
        {   
            status.setImageResource(R.drawable.upload_failed);
            ImageButton btnUpload = (ImageButton) v.findViewById(R.id.btnUpload);
            btnUpload.setEnabled(true);
        }
        else   if(strStatusID.equals("1"))
        {
            status.setImageResource(R.drawable.upload_done);
        }
    else{
    
    }
        }
    

    “已存在”必须是服务器的应答。 您可以使用不同的解决方案:

    if (strStatusID.equals("0")){
        //upload failed
        ....
    }else{
        //upload success
        if (strStatusID.equals("1")){
             //upload ok
        }else{
             //already exist
        }
    }
    


    “已存在”必须是服务器的应答。 您可以使用不同的解决方案:

    if (strStatusID.equals("0")){
        //upload failed
        ....
    }else{
        //upload success
        if (strStatusID.equals("1")){
             //upload ok
        }else{
             //already exist
        }
    }
    


    谢谢我勾选有用,但还是小问题,请看我编辑的代码谢谢我勾选有用,但还是小问题,请看我编辑的代码谢谢我勾选有用,但还是小问题,请看我编辑的代码谢谢我勾选有用,但还是小问题,请看我编辑的代码如果(如果(strStatusID.equals(“1”)在编辑代码中有任何其他问题,那么(strStatusID.equals(“1”)应该是else吗?谢谢,我勾选了有用的,但仍然是一个小问题,请参阅我编辑的代码else if((strStatusID.equals(“1”)应该是else if(strStatusID.equals(“1”))编辑后的代码中还有其他问题吗?谢谢,我勾选了有用,但问题仍然很小,请参阅我编辑的代码OK.Write status.setImageResource(R.drawable.Ready_存在);在else if(strStatusID.equals(“1”))条件中谢谢我勾选了有用,但问题仍然很小,请参阅我编辑的代码OK.Write status.setImageResource(R.drawable.Ready_存在);在else if中(strStatusID等于(“1”))条件哦,我的运气不好,不需要使用额外的开始括号,现在向你们所有人表示感谢,但不能接受所有…@西蒙是第一个回答这个问题的人…哦,我的运气不好,不需要使用额外的开始括号,现在向你们所有人表示感谢,但不能接受所有…@西蒙是第一个回答这个问题的人…已经接受,无论如何,感谢您提供的解决方案:)接受为有用已接受,无论如何,感谢您提供的解决方案:)接受为有用