Equal functions if

Asked

Viewed 72 times

2

I have if and Else codes, but I don’t know very well,I’ll explain what I wanted to do , in my app, if I typed Right , posted in api as id - X ,if I typed Romance,posted with Id - Y , but he’s only posting either with X, or as a Y, I believe it’s either error in Else if..

My Adapter code for connection ..

private final int option;
private final String url;
private final String bookType;
private final IHttpConnection httpConnection;
private HttpURLConnection httpCon;

public HttpConnection(IHttpConnection httpConnection, String bookType,        String url, int option) {
    this.httpConnection = httpConnection;
    this.bookType = bookType;
    this.url = url;
    this.option = option;
}

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

    StringBuilder result = new StringBuilder();

    try {
        String urlLogin = url;


        URL url = new URL(urlLogin);

        httpCon = (HttpURLConnection) url.openConnection();
        httpCon.setRequestProperty("Accept", "application/json");
        httpCon.setRequestMethod("GET");
        httpCon.setRequestProperty("X-DreamFactory-Api-Key", "XXXXXX");
        httpCon.setRequestProperty("X-DreamFactory-Session-Token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.XXXXXwiOiJ0aGlhZ28uY2FtYXJnb0Bldm9sdXRpb25pdC5jb20uYnIiLCJmb3JldmVyIjpmYWxzZSwiaXNzXXXXXiIsImlhdCI6MTQ5NjY4MzIwOSwiZXhwIjoxNDk2Njg2ODA5LCJuYmYiOjE0OTY2ODMyMDksImp0aSI6IjA3M2E5ZWU5M2UzNTczYWZhMDFkMDkzYjAxOTQyZWFkIn0.-YyCwLdwhEpsj_mIq2PQyjoXj1Om_LuLykihkAfE_ug");
        httpCon.setRequestProperty("Authorization", "Basic  XXXXXXXX");


        InputStream in = new BufferedInputStream(httpCon.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        while ((line = reader.readLine()) != null) {
            result.append(line);
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        httpCon.disconnect();
    }
//    System.out.println(result.toString());

    return result.toString();
}



@Override
protected void onPostExecute(String r) {

    super.onPostExecute(r);

    try {

        JSONObject json = new JSONObject(r);
        JSONArray array = new JSONArray(json.getString("resource"));
        for (int i = 0; i < array.length(); i++) {

            JSONObject jsonObj = array.getJSONObject(i);



            if  (option == 0)  {
                httpConnection.getIdCatFromCadCategorias( jsonObj.getString("id_cat"));
            }

             if  (option == 3) {
                httpConnection.getIdCatFromCadCategorias( jsonObj.getString("id_cat"));
            }


            else if (option == 1) {
                httpConnection.getIdSubCategoriasFromCadSubcategoria( jsonObj.getString("id_subcat"));
                break;
            }
        }


    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}

My code that would make the POST :

btnadd1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

        //    new Activity_addinf.onbuttonclickHttpPost().execute();
            HttpConnection postCatLivro = new HttpConnection(Activity_addinf.this,
                    catliv.toString(),
                    "http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%20%3D%20Direito",
                    0);

                      new HttpConnection(Activity_addinf.this,
                                    catliv.toString(),
                    "http://192.168.1.207/api/v2/bookdemo/_table/cad_categorias?fields=id_cat&filter=tx_cat%20%3D%20Romance",
                    3);
  • option == 0 and option == 3 should do the same thing? I could not understand your doubt very well, but maybe there is the problem.

  • Yes they should do the same function, in function 0 it filters right for the ID, if it is Option 3 it filters Romance for the ID, basically the two have the same function

1 answer

0

I don’t know if I got it right, but what seems to be bothering you is that break in Else if, because it for the execution of for

        if  (option == 0)  {
            httpConnection.getIdCatFromCadCategorias( jsonObj.getString("id_cat"));
        }

         if  (option == 3) {
            httpConnection.getIdCatFromCadCategorias( jsonObj.getString("id_cat"));
        }


        else if (option == 1) {
            httpConnection.getIdSubCategoriasFromCadSubcategoria( jsonObj.getString("id_subcat"));
            break;
        }

I believe it would be right to remove the break;

    if  (option == 0)  {
        httpConnection.getIdCatFromCadCategorias( jsonObj.getString("id_cat"));
    } else if  (option == 3) {
        httpConnection.getIdCatFromCadCategorias( jsonObj.getString("id_cat"));
    } else if (option == 1) {
        httpConnection.getIdSubCategoriasFromCadSubcategoria( jsonObj.getString("id_subcat"));
    }

See if that’s it, anything I’m up for.

  • The break avoids posting several times, if I take the break it posts without stopping

  • I think it would be interesting to post the return of the API so we can help you better

Browser other questions tagged

You are not signed in. Login or sign up in order to post.