Conexion mediante PHP JSON erro NullException

  • Respuestas:0
Karrax
  • Posts del Foro: 7

28 ago. 2015 14:39:35 vía Web

Hola:

Tengo las conexiones a Bases de adtos hechas pero al querer conseguir datos de la misma dan problemas.

Quiero cargar ciertos markers en el mapa dependiendo de la ifnromacion que me traen mediante estos codigos:

load(metodo que carga los markers):

public void load() throws JSONException {

    String query = "SELECT * FROM fronton";
    dbconnection db = new dbconnection(query,true);
    db.execute();
    JSONObject solut = db.getSolution();
    JSONArray solution = solut.getJSONArray("fronton");

    for (int i = 0; i < solution.length(); i++) {

        JSONObject c = solution.getJSONObject(i);

        // Storing each json item in variable
        String name = c.getString("name");
        String light = c.getString("light");
        String cover = c.getString("cover");
        String pay = c.getString("pay");
        String type = c.getString("type");
        int state = c.getInt("state");
        double longi = c.getDouble("longitude");
        double lati = c.getDouble("latitude");

        Marker marker = mapa.addMarker(new MarkerOptions()
                .position(new LatLng(lati, longi))
                .title(name));

        if(state==0){
            marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
        }else{
            marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
        }


    }
}

JSONParser:

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

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

    // Making HTTP request
    try {

        // check for request method
        if(method == "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 == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }


    } 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();
    } 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;

}

}

y dbConnecction(el que se encarga de la conexion):

public class dbconnection extends AsyncTask<String, String, String> {

private String query;
private boolean get;
private JSONObject solu;

public dbconnection(String qu, boolean g){

    query = qu;
    get = g;

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

    String url =url;

    JSONParser jParser = new JSONParser();
    JSONArray products = null;
    List<NameValuePair> pm = new ArrayList<NameValuePair>();
    pm.add(new BasicNameValuePair("query", query));

    if (get) {
        JSONObject json = jParser.makeHttpRequest(url, "GET", pm);
        solu = json;

    } else{
     JSONObject json = jParser.makeHttpRequest(url, "POST", pm);

    }

    return null;
}

public JSONObject getSolution(){
    return solu;
}

}

El problema es que al querer ejecutar el emtodo Load en el mismo MainActivity, nada ams encender la aplicacion, saca este error:

Process: org.iratzarcarrasson.frontoiapp, PID: 21019
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.iratzarcarrasson.frontoiapp/org.iratzarcarrasson.frontoiapp.MainActivity}: java.lang.NullPointerException

que tipo de solucion me planteais a esto?

Muchisimas gracias

Contestar