Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7813221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:46:34+00:00 2026-06-02T04:46:34+00:00

I have a JSON file (towns.json) sitting on my server and in my app

  • 0

I have a JSON file (towns.json) sitting on my server and in my app I want the data to be read from there.
So I figured out I should use the AsyncTask to not block the UI thread. I am doing it as follows:

    private class GetTowns extends AsyncTask<String, String, Void> {
    protected void onPreExecute() {}

    @Override
    protected Void doInBackground(String... params) {
        String readTown = readFeed(ScreenStart.this, "http://server.com/towns.json");
        try {
            JSONArray jsonArray = new JSONArray(readTown);
            town = new ObjectTown[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                town[i] = new ObjectTown();
                town[i].setId(Integer.parseInt(jsonObject.getString("id")));
                town[i].setName(jsonObject.getString("name"));
                town[i].setLat(Double.parseDouble(jsonObject.getString("latitude")));
                town[i].setLon(Double.parseDouble(jsonObject.getString("longitude")));
            }
        } catch (Exception e) {
            Log.i("Catch the exception", e + "");
        }
        return null;
    }

    protected void onPostExecute(Void v) {}
}

And the readFeed() function here:

public static String readFeed(Context context, String str) {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(str);

    try {
        HttpResponse response = client.execute(httpGet);

        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content, "ISO-8859-1"));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } 
    } catch (Exception e) {
        e.printStackTrace();
    }
    return builder.toString();
}

It works sometimes… but once in a while, the doInBackground() throws this exception:

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

What exactly am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T04:46:35+00:00Added an answer on June 2, 2026 at 4:46 am

    It looks like server is not returning data, have you checked it your string is non empty?. To turn HttpEntity into string you can use:

    String jsonStr = EntityUtils.toString(httpEntity);
    

    I suppose you should also check if entity is different from null before using it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON file and i have to read data from this file
I have an NSArray of JSON file locations. There are stored in the user's/app's
I have a json file exported from phpmyadmin, it looks like this(utf-8 file): [{user_email:
I have a JSON file which contains HTML content. I want to load it
I have a JSON file and want to parse value of variable second value
I have a json file that I'm trying to get values out of. One
I have a .json file sitting at the root of my site. The name
I have a json file of data and the only item for time i
I have a JSON file, and i need to extract values from it and
I have a JSON file which contains data like the following: {"posts": [ {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.