I'm working with android studio , create an application which allows me to read data from a database on a server and load the data into the sqlite database that has android .
When I do it in my genymotion emulator everything is fine, I have a samsung tablet with android 5.1.1, I install the application and I get a perfect connection (I can read the data from the web and load it in sqlite).
The problem is when I try to install on another tablet with android 5.1.1, in these cases when I try to read from the web the application closes. Any idea why it does not work on the other tablets? I need to have installed some package or apk on the tablet that does not work?
Part of the code
public JSONObject makeHttpRequest(String url, String method,
List params) {
// Making HTTP request
try {
// check for request method
if(method.equals("POST")){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method.equals("GET")){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
//httpGet.addHeader("Cookie", "__test=06410fea214b2febfb23d387de6037da; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/");
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();System.out.println(e.getMessage());
} catch (ClientProtocolException e) {
e.printStackTrace();System.out.println(e.getMessage());
} catch (IOException e) {
e.printStackTrace();System.out.println(e.getMessage());
}
catch(Exception e){
e.printStackTrace();System.out.println(e.getMessage());
}
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();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
protected String doInBackground(String... args) {
BaseHelper baseHelper = new BaseHelper(MyActivity.this, "DEMODB", null, 1);
//Elimino todos los datos de la tabla producto (base sqlite tablet)
baseHelper.vaciarTabla("producto",baseHelper);
// Building Parameters
List params = new ArrayList();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(urlProductos, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_PRODUCTS);
// looping through All Products
//Log.i("ramiro", "produtos.length" + products.length());
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String codigo = c.getString(TAG_CODIGO);
String nombre = c.getString(TAG_NOMBRE);
String lista = c.getString(TAG_LISTA);
String preciou = c.getString(TAG_PRECIOU);
String preciobr = c.getString(TAG_PRECIOBR);
String stock = c.getString(TAG_STOCK);
String fecha = c.getString(TAG_FECHA);
System.out.println(id+" "+codigo+" "+lista+" "+nombre);
// inserto en mi tabla local
SQLiteDatabase db = baseHelper.getWritableDatabase();
if (db != null) {
ContentValues registroNuevo = new ContentValues();
//primer parametro nombre en la base, segundo el valor a ingresar
registroNuevo.put("prd_id", id);
registroNuevo.put("prd_codigo", codigo);
registroNuevo.put("prd_nombre", nombre);
registroNuevo.put("prd_lista", lista);
registroNuevo.put("prd_precio_u", preciou);
registroNuevo.put("prd_precio_br", preciobr);
registroNuevo.put("prd_stock", stock);
registroNuevo.put("prd_fecha_update", fecha);
long z = db.insert("producto", null, registroNuevo);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}