Hey everyone,

I got a weird bug in my app, sometimes when I parse data from a JSON object (the php script is on my server) i get the values i need, but sometimes it gives randomly null?

Here an example (it is in dutch but that shouldn't matter):
/images/forum/upload/2015-05-13/8de383b10cf05ca25c4c3c4770a0c981.jpg

And here is the bug:
/images/forum/upload/2015-05-13/3e5f9d7f2b88831a8323d86f4652c49e.jpg

My php file:


<?php


require_once "class_db.php";

$db = new DB();
 
$id = $_POST['id'];
 
$response = array();
 
$response['topic'] = array();


try{
$query = $db->_handler->prepare("SELECT * FROM topics WHERE id = ?");
$query->bindParam(1, $id);
$query->execute();
$result = $query->fetch();

$id2 = $result['id'];
$titel = $result['titel'];
$tekst = $result['tekst'];
$maker = $result['maker'];

$topics['id'] = $id2;
$topics['titel'] = $titel;
$topics['tekst'] = $tekst;
$topics['maker'] = $maker;

array_push($response['topic'], $topics);


$response["success"] = 1;
$response["message"] = "Succesvol";
}
catch(PDOException $e){
$response["success"] = 0;
$response["message"] = "FOUT";
}

/*
echo "<pre>";
print_r($response);
echo "</pre>";

echo "<hr>";
*/

echo json_encode($response);



 ?>





This is my HTTP Request code (java):

int success;
            List params = new ArrayList();
            params.add(new BasicNameValuePair("id", id_intent));

            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_products, "POST", params);

            // Check your log cat for JSON reponse
            Log.d("All Topics: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);



                    // looping through All Products
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_PID);
                        String titel = c.getString(TAG_TITEL);
                        String tekst = c.getString(TAG_TEKST);
                        String maker = c.getString(TAG_MAKER);

                        // creating new HashMap
                        HashMap map = new HashMap();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, id);
                        map.put(TAG_TITEL, titel);
                        map.put(TAG_TEKST, tekst);
                        map.put(TAG_MAKER, maker);

                        // adding HashList to ArrayList
                        productsList.add(map);



                    }
                } else {
                /*
                    // no products found
                    // Launch Add New product Activity
                    Intent i = new Intent(getApplicationContext(),
                            NewProductActivity.class);
                    // Closing all previous activities
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    */
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }




And this is the JSONParser class:

package com.example.robbe.myapplication;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

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;

}
}




Thank you for your help! 

Kind regards!