-1

End of input at character 1 of

im making a listview and using JSONObject for array purposes but im getting this error i dont know how to fix it i dont even know where my exception takes place when i run my program it simply end and doesnt not show any result this is my java file i hope someone can find the exception

@Override
            protected ArrayList<TodayDetails> doInBackground(Void... params) {
                    try {
                            // Create a new HTTP Client
                            DefaultHttpClient defaultClient = new DefaultHttpClient();
                            // Setup the get request
                            HttpGet httpGetRequest = new HttpGet("http://api.androidhive.info/contacts");

                            // Execute the request in the client
                            HttpResponse httpResponse = defaultClient
                                            .execute(httpGetRequest);
                            // Grab the response
                            BufferedReader reader = new BufferedReader(
                                            new InputStreamReader(httpResponse.getEntity()
                                                            .getContent(), "UTF-8"));
                            String result = reader.readLine();
                            JSONObject object = new JSONObject(result);
                            JSONArray Jarray = object.getJSONArray("contact");
                            for (int i = 0; i < Jarray.length(); i++) {
                               JSONObject Jasonobject = Jarray.getJSONObject(i);

                                    today_details = new TodayDetails();
                                    today_details.setId(Jasonobject.getInt("id"));
                                    today_details.setName(Jasonobject.getString("name"));
                                    today_details.setEmail(Jasonobject.getString("email"));
                                    today_details.setAddress(Jasonobject.getString("address"));
                                    today_details.setGender(Jasonobject.getString("gender"));
                                    today_details.setPhone(Jasonobject.getInt("phone"));
                                    today_details.setHome(Jasonobject.getInt("home"));
                                    today_details.setMobile(Jasonobject.getInt("mobile"));
                                    today_details.setOffice(Jasonobject.getInt("office"));

                                    search_results.add(today_details);
                       }
        } catch(Exception e){
           e.printStackTrace();
        }
        return search_results;
    }
    protected void onPostExecute(ArrayList<TodayDetails> search_results) {
        if(WeatherToday.this.pd != null){
            WeatherToday.this.pd.dismiss();
        }
        ArrayList<TodayDetails> today_details = search_results;
        list_view_main = (ListView)findViewById(R.id.todaylist);
        list_view_main.setAdapter(new TodayListAdapter(getApplicationContext(), today_details));
        }
    };
}
4

2 回答 2

2

SANITARY CHECK When working with JSON

Check for an error on the JSON side. This is a useful link for json validation.

JsonlintTry this link to validate your JSON,JUST COPY/PASTE YOUR JSON and check if its valid or not

If its valid then post it over here , may be then will be able to help you more.

If Its not Valid...Well then everything will just be fine :-)

CODE CHECK

  1. Add the complete json by reading it line by line into a StringBuilder
  2. Call toString and pass the resulting JSOn as a String to the JSONObject obj=new JsonObject(String) function.
    1. So Now if you get an exception then something bad in your json check it via the above mention link.
    2. If it works fine , then iterate the jsonObject for the array , array of object or whatever.

I hope you'll find this useful

Update

Using the Gson library is quite useful as json gets bigger.It handles every thing for you.Abstract in nature, and provides some static method for you to retrieve the json or make one.

Here's a gist of a programs that i made for json parsing using Gson library.

Gson demo programs

Help articles

Blog help

Gson Official

于 2013-04-18T05:26:56.467 回答
1

The first thing I can see is that you want "contacts" not "contact":

 JSONArray Jarray = object.getJSONArray("contact");

From:

{
"contacts": [
    {
            "id": "c200",

And none of that weather stuff is in that JSON either. So obviously there are many things that can fail.

EDIT: I see you got rid of the weather bits. Heh

EDIT 2: However, it seems like whatever is printing that logcat output DOES care about the weather.

EDIT 3: You're reading just one line of the JSON, which is {. That's not proper JSON. Read the full output.

   String result = reader.readLine();
于 2013-04-18T04:12:16.647 回答