I am trying to make my Android project work with LAMP server, but JSON request keep returning null. LAMP server is working properly, my request is reaching php files on it.
Here is my code:
class GetEntryData extends AsyncTask<String, String, String> {
private static JSONParser jsonParser;
private List<NameValuePair> entry_data;
private static String server_address;
private static String server_db;
private static String db_table;
private static int id;
public static String TAG = "LogDebug";
private static final String TAG_SUCCESS = "success";
public GetEntryData(String server_address, String server_db, String db_table, int id) {
this.server_address = server_address;
this.server_db = server_db;
this.db_table = db_table;
this.id = id;
}
@Override
protected String doInBackground(String[] params) {
Log.d(TAG, "Sending JSON request");
jsonParser = new JSONParser();
int success;
entry_data = new ArrayList<NameValuePair>();
entry_data.add(new BasicNameValuePair("id", String.valueOf(id)));
Log.d(TAG, "server address: " + server_address + " enrty data: " + entry_data.toString());
JSONObject json = jsonParser.makeHttpRequest(server_address, "GET", entry_data);
try {
Log.d(TAG, json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
JSONArray productObj = json.getJSONArray(db_table);
JSONObject entry = productObj.getJSONObject(0);
return entry.toString();
} else {
return null;
}
} catch (NullPointerException e) {
return e.getMessage();
}catch (JSONException e){
return e.getMessage();
}
}
@Override
protected void onPostExecute(String result) {
Log.d(TAG, "Entry data was written with the result: " + result);
}
}
My JSON class:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
try {
if(method == "POST"){
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"){
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 + "
");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}}
And my php code:
<?php
$response = array();
require 'db_connect.php';
$db = new DB_CONNECT();
if (isset($_GET["id"])) {
$pid = $_GET['id'];
$result = mysqli_query("SELECT *FROM marker_data WHERE id = $id");
if (!empty($result)) {
if (mysqli_num_rows($result) > 0) {
$result = mysqli_fetch_array($result);
$entry = array();
$entry["id"] = $result["id"];
$entry["title"] = $result["title"];
$entry["address"] = $result["address"];
$entry["image"] = $result["image"];
$entry["working_hours"] = $result["working_hours"];
$entry["product_range"] = $result["product_range"];
$entry["comments"] = $result["comments"];
$entry["confirmation_status"] = $result["confirmation_status"];
$entry["latitude"] = $result["product_range"];
$entry["longitude"] = $result["longitude"];
$response["entry"] = array();
array_push($response["entry"], $entry);
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No entry found";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "No entry found";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
db_connect.php:
<?php
class DB_CONNECT {
function __construct() {
$this->connect();
}
function __destruct() {
$this->close();
}
function connect() {
require 'db_config.php';
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysqli_error());
$db = mysqli_select_db(DB_DATABASE) or die(mysqli_error()) or die(mysqli_error());
return $con;
}
function close() {
mysqli_close();
}
}
?>
What I get in Logcat is: Error parsing data org.json.JSONException: End of input at character 0 of Entry data was written with the result: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
There is an entry with id = 1 (which I am trying to get), and all the variable names (like server, database, php files and etc) are set properly.