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
andoption == 3
should do the same thing? I could not understand your doubt very well, but maybe there is the problem.– Dener
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
– Samara Carvalho