0

I'm trying retrive server json data.

My code: MainActivity

String url = "http://........com/...../...."
String[] params = new String[]{url};
JSONParser jsonParser = new JSONParser();
try {
String response = jsonParser.execute(url).get();
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObje = (JSONObject) jsonArray.get(0);
}

JSONParser.JAVA

HttpURLConnection connection = null;
BufferedReader reader = null;
String responseText="";
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            //GIVE ERROR THIS LINE!
            InputStream stream = connection.getInputStream();
            //ERROR LINE!

            reader = new BufferedReader(new InputStreamReader(stream));

Error: org.json.JSONException: End of input at character 0 of

Json file:

{"PersonelID":2,"PersonelAdıSoyadı":"New Driver","PersonelTelefon":"","PersonelMail":"driver@mail.com","PersonelPassword":null,"PersonelTipi":1,"AracID":0,"SirketID":20,"SuccessCode":1}
  • if the value of your variable "response" contains this string "{"PersonelID":2,"PersonelAdıSoyadı":"New Driver",..." you get the error because that is not a JSON Array! Do not use `JSONArray` use `JSONObject` instead – Barns Jan 15 '18 at 22:41
  • Can show us the full exception? – Mordag Jan 15 '18 at 22:46

3 Answers3

0

just call this method by passing input stream

    private String readInputStream(InputStream inputStream) throws IOException {
    Log.d(TAG, "readInputStream()");
    StringBuilder stringBuilder = new StringBuilder();

    BufferedReader bufferedReader = null;

    try {
        if (inputStream != null) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
            bufferedReader = new BufferedReader(inputStreamReader);
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuilder.append(line);
                line = bufferedReader.readLine();

            }

            return stringBuilder.toString();

        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        if (bufferedReader != null) {
            bufferedReader.close();
        }

    }

    return "RESULT_EMPTY";

}

,then JSONObject jsonObject = new JSONObject(result);

hemen
  • 1,460
  • 2
  • 16
  • 35
0

the reason for the error is because you are expecting a JSONArray which in this form

["item1","item2,"item3"]

while JSONObject is in this form {"key": "value"} so in your example, you are getting JSONObject, not a JSONArray. so change your code to

String response = jsonParser.execute(url).get();

JSONObject jsonObject = new JSONObject(resonse);
String PersonelMail= jsonObject.getString("PersonelMail");
 //...... the same for  the other values

see this tutorial for more about JSON parsing . also check my answer on how to use Gson a library created by Google to map JSON easily.

Bishoy Kamel
  • 2,327
  • 2
  • 17
  • 29
0

My codes is true. Problem is very different places.

I'm adding internet permission to Android manifest, problem solved.

Thank you all.