如果条件不能正常工作总是进入假块

时间:2015-10-01 05:15:23

标签: android

以下是我的代码,我试图获得服务器响应,如果状态等于成功我显示成功消息,如果失败显示失败但所有时间显示失败消息为什么它发生。请给我解决方案。 verify.java 我不知道它是执行条件

的真或假块
public class verifyotp extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pdialog = new ProgressDialog(Verifyotp.this);
        pdialog.setMessage("Loading....");
        pdialog.setIndeterminate(false);
        pdialog.setCancelable(false);
        pdialog.show();
    }
    @Override
    protected String doInBackground(String... strings) {
        try {
            List<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("edituser", Uid));
            param.add(new BasicNameValuePair("editotp", abc));
            JSONObject json = jpar.makeHttpRequest(URL_VERIFY_OTP, "POST", param);
            String sts=json.getString(TAG_status);
            Log.v(TAG,""+json.getString(TAG_status));
           if(sts=="succuss")
            {
                Log.v(TAG,"in success block");
                lflag=true;

            }
            else
           {
               lflag=false;

           }
        }catch(Exception e){
            e.printStackTrace();
            Log.v(TAG, "Exception at end :" + e.toString());
            //Log.e("TAG", "Error......!RecoverIt");
        } return null;
    }


    @Override
    protected void onPostExecute(String Result) {
        super.onPostExecute(Result);

            if(lflag==true)
            {
                Toast.makeText(Verifyotp.this, "Successsss", Toast.LENGTH_SHORT).show();
            }else
            {
                Toast.makeText(Verifyotp.this, "Faileddddd", Toast.LENGTH_SHORT).show();
            }
            pdialog.hide();
            pdialog.dismiss();
    }
}

它没有设置lflag,它总是显示失败的消息。另一个问题如下

public class JSONParser {

static InputStream is;
static JSONObject jObj;
static String json;
static JSONArray jarr;
private static final String TAG = "myAppSurun";
public static String alternateJSONString = "";
public static JSONArray alternateJSONArray = null;
// constructor
public JSONParser() {
    is = null;
    jObj = null;
    json = "";
    jarr = null;
}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
                                  List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method.equalsIgnoreCase("POST")){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));
            Log.v(TAG, "Param String :" + url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
            Log.v(TAG,"To the End of IF");

        }else if(method.equalsIgnoreCase("GET")){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            Log.v(TAG,"Param String :"+url);
            HttpGet httpGet = new HttpGet(url);
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
            Log.v(TAG,"To the End of ELSE");
        }

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
        Log.v(TAG,"JS_PARSER : Server Response -"+json);
    } catch (Exception e) {
        Log.v(TAG,"Error in getting json string from response : A further try will be given to use json string "+e.toString());
    }
    // try parse the string to a JSON object/Array And String
    try {

        if(json != null) {
            alternateJSONString = json;
            jObj = new JSONObject(json);
            Log.v(TAG,"JS_PARSER : JSON Object Created Successfully Use Object instance");
        }
        else
        {
            Log.v(TAG,"JS_PARSER : JSON String is blank trying one more time to parse JSON String");
        }
    }
    catch (Exception e) {
        try{
        if(json != null) {
            Log.v(TAG,"JS_PARSER :  Object Parsing failed Trying for Array if no more logs followed by \" JS_PARSER \" use JSONParser.alternateJSONArray for JSON instance. ");
            alternateJSONArray = new JSONArray(json);
        }
        else {
            Log.v(TAG,"JS_PARSER : JSON String is blank trying one last time to parse JSON String if no more logs followed by \" JS_PARSER \" use JSONParser.alternateJSONString for JSON instance");
        }
        }
        catch(Exception e1)
        {
            if(json !=null) {

                Log.v(TAG, "JS_PARSER : Array Parsing also failed if no more logs followed by \" JS_PARSER \" use JSONParser.alternateJSONString for backup JSON string for results");
            }
            else {
                Log.v(TAG, "JS_PARSER : Final try also failed Confirm server log for request error or follow following log trail"+e1);
            }
        }
    }
    // return JSON String
    return jObj;


}

这是我的jsonparser文件 如果我输入了错误的otp,它将显示我的回复

   1843-1904/com.example.surun.suruninfocore V/myAppSurun﹕ To the End of IF
      10-01 11:19:11.207    1843-1904/com.example.surun.suruninfocore V/myAppSurun﹕ JS_PARSER : Server Response -
        10-01 11:19:11.213    1843-1904/com.example.surun.suruninfocore V/myAppSurun﹕ JS_PARSER :  Object Parsing failed Trying for Array if no more logs followed by " JS_PARSER " use JSONParser.alternateJSONArray for JSON instance.
      10-01 11:19:11.213    1843-1904/com.example.surun.suruninfocore V/myAppSurun﹕ JS_PARSER : Array Parsing also failed if no more logs followed by " JS_PARSER " use JSONParser.alternateJSONString for backup JSON string for results
        10-01 11:19:11.224    1843-1904/com.example.surun.suruninfocore W/System.err﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
        10-01 11:19:11.227    1843-1904/com.example.surun.suruninfocore W/System.err﹕ at com.example.surun.suruninfocore.Verifyotp$verifyotp.doInBackground(Verifyotp.java:85)
      10-01 11:19:11.227    1843-1904/com.example.surun.suruninfocore W/System.err﹕ at com.example.surun.suruninfocore.Verifyotp$verifyotp.doInBackground(Verifyotp.java:68)

3 个答案:

答案 0 :(得分:3)

使用//Ajax to get idns sites by ruleId. var deleteButton='<i class="glyphicon glyphicon-remove" style="font-size:20px;"></i>'; var editButton='<i class="glyphicon glyphicon-pencil" style="font-size:20px;"></i>'; $.ajax({ url : '/idns/site/getSitesByRuleId/'+rule_id, method : 'get', dataType : 'json', success:function(data){ $('#siteDataTable').DataTable( { data:data, "destroy": true, "processing": true, columns: [{ data: 'siteId' },{ data: 'siteName' },{ data: 'userName' },{ data: 'channelName' },] } ); } }); // To edit site table record. var table = $('#siteDataTable').DataTable(); $('#siteDataTable tbody').on( 'click', 'tr', function () { console.log(table.row( this ).data()); } ); 代替equals。请尝试这种方式。我希望它能运作

  

在Java中,最常见的错误之一就是使用==来   比较字符串。你必须记住,==比较对象   引用,而不是内容。

     

string1.equals(String target)比较两个基于的字符串   字符串中的实际字符。

良好做法/正确方法

==

答案 1 :(得分:1)

String比较总是使用以下方式进行比较:

equals() OR equalsIgnoreCase()

对象比较总是使用以下方式进行比较:

==

在您的情况下尝试使用:

if(sts.equals("succuss")) OR if(sts.equalsIgnoreCase("succuss")) 

答案 2 :(得分:1)

您正在比较字符串,因此您应该使用

if(sts.equalsIgnoreCase("succuss"))
{
     lflag=true;
}

它会给出预期的结果。