Skip to content Skip to sidebar Skip to footer

Parsing Json Url Using Async

I get exceptions when running this code. I want to parse the url which is an array of json objects: package com.example.compsci_734t; import java.io.BufferedReader; import java.io

Solution 1:

The error is explanatory itself:

java.lang.IllegalArgumentException: Illegal character in scheme at index 0

You need to remove the beginning space character in your URL:

privatestaticStringurl="http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/courses";

Update:

After you've updated your question, now the problem seems in this line:

courses = json.getJSONArray(TAG_COURSES);

because you haven't initialized or set the value of courses that's why its giving NullPointerException error.

Solution 2:

It is not related to JSON parsing.

privatestaticStringurl=" http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/courses";

you have to remove the initial space from the url string

Solution 3:

Hi its due to some problem in the webservice maybe due toh the encoding problem its not your or android's problem. Just as you get response in that string just replace the character you get with "".

response= response.replace("","");

and then do your parsing after this. It worked for me. I hope it works for you also.....

Solution 4:

I have solved it. Thanks for all your help. I also converted the parsed stuff into a list view. This is my working code:

package com.example.compsci_734t;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;





import android.app.Activity;
import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;



publicclassPeopleextendsActivity{

        ArrayList<String> items = newArrayList<String>();
        staticInputStreamis=null;
        //private static String url = "";//private static String url = "http:...";privatestaticStringurl="http....";
        //URL requestUrl = new URL(url);JSONArraypeople=null;
        privatestaticfinalStringTAG_COURSES="Courses";
        staticJSONObjectjObj=null;
        staticStringjson="";

        @OverridepublicvoidonCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.people);
            newMyTasks().execute();
        }


        privateclassMyTasksextendsAsyncTask<URL, Void, JSONObject> {

            @Overrideprotected JSONObject doInBackground(URL... urls) {
               // return loadJSON(url);try {
                    // defaultHttpClientDefaultHttpClienthttpClient=newDefaultHttpClient();
                    //HttpPost httpPost = new HttpPost(url);HttpGethttpGet=newHttpGet(url);
                    HttpResponsehttpResponse= httpClient.execute(httpGet);

                    HttpEntityhttpEntity= 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, "UTF-8"), 8);*/InputStreaminputStream= is;
                GZIPInputStreaminput=newGZIPInputStream(inputStream);
                InputStreamReaderreader=newInputStreamReader(input);
                BufferedReaderin=newBufferedReader(reader);
                StringBuildersb=newStringBuilder();
                Stringline=null;
                while ((line = in.readLine()) != null) {
                    sb.append(line);
                   //System.out.println(line);
                }
                is.close();
                json = sb.toString();
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }

            // try parse the string to a JSON objecttry {

                JSONArraypeople=newJSONArray(json);
                //JSONArray people = new JSONArray(json);for (inti=0; i < people.length(); i++) {
                    //System.out.println(courses.getJSONObject(i).toString());JSONObjectp= people.getJSONObject(i);

                    // Storing each json item in variableStringperson_id= p.getString("someString1");


                    items.add(person_id);

                    /*Log.v("--", "People: \n" + "\n UPI: " + person_id);*/
                }


                //jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            } 

            // return JSON Stringreturn jObj;
            }

            protectedvoidonPostExecute(JSONObject json) {
                ListViewmyListView= (ListView)findViewById(R.id.peopleList);
                myListView.setAdapter(newArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, items));
        }
        }

Solution 5:

  1. I have solved it.....tRy it really work

package com.example.json_test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.ArrayList;
import java.util.zip.GZIPInputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

publicclassMainActivityextendsActivity
{

        ArrayList<String> items = newArrayList<String>();
        staticInputStreamis=null;
        privatestaticStringurl="http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/courses";
        JSONArraypeople=null;
        privatestaticfinalStringTAG_COURSES="codeField";
        privatestaticfinal String TAG_SEMESTER="semesterField";
        staticJSONObjectjObj=null;
        staticStringjson="";

        @OverridepublicvoidonCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            newMyTasks().execute();
        }


        privateclassMyTasksextendsAsyncTask<URL, Void, JSONObject> 
        {

            @Overrideprotected JSONObject doInBackground(URL... urls) 
            {
                try 
                {
                    DefaultHttpClienthttpClient=newDefaultHttpClient();
                    HttpGethttpGet=newHttpGet(url);
                    HttpResponsehttpResponse= httpClient.execute(httpGet);

                    HttpEntityhttpEntity= httpResponse.getEntity();
                    is = httpEntity.getContent();

                } 
                catch (UnsupportedEncodingException e) 
                {
                    e.printStackTrace();
                } 
                catch (ClientProtocolException e) 
                {
                    e.printStackTrace();
                } catch (IOException e) 
                {
                    e.printStackTrace();
                }
              try 
              {
                InputStreaminputStream= is;
                GZIPInputStreaminput=newGZIPInputStream(inputStream);
                InputStreamReaderreader=newInputStreamReader(input);
                BufferedReaderin=newBufferedReader(reader);
                StringBuildersb=newStringBuilder();
                Stringline=null;

                while ((line = in.readLine()) != null) 
                {
                    sb.append(line);
                }
                is.close();
                json = sb.toString();
            } catch (Exception e) 
            {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }
            try 
            {

                JSONArraypeople=newJSONArray(json);

                for (inti=0; i < people.length(); i++) 
                {
                    JSONObjectp= people.getJSONObject(i);
                    StringCourses= p.getString(TAG_COURSES);
                    String semester=p.getString(TAG_SEMESTER);

                    items.add(Courses);
                    items.add(semester);
                }

            } catch (JSONException e) 
            {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            } 
            return jObj;
            }

            @SuppressWarnings({ "unchecked", "rawtypes" })protectedvoidonPostExecute(JSONObject json) 
            {
                ListViewmyListView= (ListView)findViewById(R.id.peopleList);
                myListView.setAdapter(newArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, items));
        }
        }
}

Post a Comment for "Parsing Json Url Using Async"