Match functions if

2
  

I have Codes of if and else, but I do not know very well, I will explain what I wanted to do, in my app, if I typed Right, post in api as id - X, if I typed Romance, but it is only posting with either X, or as Y, I believe either error in the else if ..

     

My Adapter code for the 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 would do 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);
    
asked by anonymous 13.06.2017 / 21:15

1 answer

0

I do not know if I understood correctly, but what seems to be disturbing you is that break in the else if, for it to execute 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 think the correct thing would be 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 right, anything is available.

    
13.06.2017 / 21:45