-2
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
params.add(new BasicNameValuePair("id", android_id));
JSONObject json = jParser.makeHttpRequest("http://cp1.be/scriptsandroid/dbvoorbeeld2.php", "POST", params);

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

    if (success == 1) {
    }
} catch (JSONException e) {
    e.printStackTrace();
}

So like the title says that is the error I'm getting. I've been searching for the answer but did not find it yet. Thanks in advance.

My php file:

<?php
if(isset($_POST['id'])) {
    $key = $_POST['id'];
    require_once("db_licentieserver.php");

    $response["success"] = 1;

    echo json_encode($response);

} else {
    $response["success"] = 0;
    echo json_encode($response);
}
?>

Imported this in my java project:

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
Medes
  • 48
  • 6
  • 2
    You haven't posted your JSON string – Sufian Nov 17 '14 at 10:41
  • If you don't post the JSON which couldn't be parsed, how can we tell why it failed? As I said before, post your JSON which caused `org.json.JSONException: end of input at character 0`. Or there isn't any JSON? – Sufian Nov 17 '14 at 10:46
  • I echo the json so it can be parsed. No? – Medes Nov 17 '14 at 10:48
  • Maybe someone will be able to tell what caused that 'invisible' JSON to crash your code. – Sufian Nov 17 '14 at 10:49
  • so what do I need to do to get this working? :) I mean like what do I need to change realy since I'm not a pro in this – Medes Nov 17 '14 at 10:51
  • 1
    "End of input at character 0" -- just think on that a minute -- what is it saying???? It means that the input is zero length. (Try actually ***READING*** exception messages.) – Hot Licks Nov 17 '14 at 12:28

1 Answers1

1

What the exception is saying is that it has tried to parse a JSON document that has zero length.

The most plausible explanation is that the POST request that you sent has failed with an HTTP error of some kind. You are probably attempting to read the "output" stream, but the real explanation is in the "error" stream.

so what do I need to do to get this working?

Unfortunately, I can't figure out from your Java code snippet what type jParser is and where the makeHttpRequest method is implemented. (I have an inkling that it is something that you wrote yourself, or copied from a tutorial ...) Without seeing that code, it is hard to suggest how to fix the problem.

Hint: just the simple class name (JSONParser per your update) is NOT sufficient to figure out which JSONParser class you are using.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • I imported some things, I'll update post – Medes Nov 17 '14 at 11:00
  • 1
    @MichaelVandenZegel please show us lines where you declare and initialise `jParser`. – Sufian Nov 17 '14 at 11:06
  • JSONParser jParser = new JSONParser(); – Medes Nov 17 '14 at 11:08
  • @MichaelVandenZegel strange. I hit the URL with params and it returned `{"success":1}`. I see no reason why it should give you that exception. Are you sure the error is on your line where you call `makeHttpRequest()`? – Sufian Nov 17 '14 at 11:19
  • No that does well I suppose. This errors tho: int success = json.getInt("success"); Allthought that can be because of the makeHttpRequest(). I'll check that just now. – Medes Nov 17 '14 at 11:35
  • @MichaelVandenZegel Try doing `json.toString()` before `getInt()` and update your question. – Sufian Nov 17 '14 at 11:43
  • You still haven't given me enough information to figure out which JSONParser class you are using. – Stephen C Nov 17 '14 at 13:13
  • @StephenC I think he likes abstraction too much ;) – Sufian Nov 17 '14 at 13:48