Parsing Json url using async

前端 未结 5 837
花落未央
花落未央 2021-01-25 23:53

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.BufferedRea         


        
相关标签:
5条回答
  • It is not related to JSON parsing.

      private static String url = " http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/courses";
    

    you have to remove the initial space from the url string

    0 讨论(0)
  • 2021-01-26 00:17

    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:

    private static String url = "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.

    0 讨论(0)
  • 2021-01-26 00:19
    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;
    
    public class MainActivity extends Activity
    {
    
            ArrayList<String> items = new ArrayList<String>();
            static InputStream is = null;
            private static String url = "http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/courses";
            JSONArray people = null;
            private static final String TAG_COURSES = "codeField";
            private static final String TAG_SEMESTER="semesterField";
            static JSONObject jObj = null;
            static String json = "";
    
            @Override
            public void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                new MyTasks().execute();
            }
    
    
            private class MyTasks extends AsyncTask<URL, Void, JSONObject> 
            {
    
                @Override
                protected JSONObject doInBackground(URL... urls) 
                {
                    try 
                    {
                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet = new HttpGet(url);
                        HttpResponse httpResponse = httpClient.execute(httpGet);
    
                        HttpEntity httpEntity = httpResponse.getEntity();
                        is = httpEntity.getContent();
    
                    } 
                    catch (UnsupportedEncodingException e) 
                    {
                        e.printStackTrace();
                    } 
                    catch (ClientProtocolException e) 
                    {
                        e.printStackTrace();
                    } catch (IOException e) 
                    {
                        e.printStackTrace();
                    }
                  try 
                  {
                    InputStream inputStream = is;
                    GZIPInputStream input = new GZIPInputStream(inputStream);
                    InputStreamReader reader = new InputStreamReader(input);
                    BufferedReader in = new BufferedReader(reader);
                    StringBuilder sb = new StringBuilder();
                    String line = 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 
                {
    
                    JSONArray people = new JSONArray(json);
    
                    for (int i = 0; i < people.length(); i++) 
                    {
                        JSONObject p = people.getJSONObject(i);
                        String Courses = 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" })
                protected void onPostExecute(JSONObject json) 
                {
                    ListView myListView = (ListView)findViewById(R.id.peopleList);
                    myListView.setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, items));
            }
            }
    }
    

    0 讨论(0)
  • 2021-01-26 00:22

    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.....

    0 讨论(0)
  • 2021-01-26 00:27

    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;
    
    
    
    public class People extends Activity{
    
            ArrayList<String> items = new ArrayList<String>();
            static InputStream is = null;
            //private static String url = "";
            //private static String url = "http:...";
            private static String url = "http....";
            //URL requestUrl = new URL(url);
            JSONArray people = null;
            private static final String TAG_COURSES = "Courses";
            static JSONObject jObj = null;
            static String json = "";
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.people);
                new MyTasks().execute();
            }
    
    
            private class MyTasks extends AsyncTask<URL, Void, JSONObject> {
    
                @Override
                protected JSONObject doInBackground(URL... urls) {
                   // return loadJSON(url);
                    try {
                        // defaultHttpClient
                        DefaultHttpClient httpClient = new DefaultHttpClient();
                        //HttpPost httpPost = new HttpPost(url);
                        HttpGet httpGet = new HttpGet(url);
                        HttpResponse httpResponse = httpClient.execute(httpGet);
    
                        HttpEntity httpEntity = 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);*/
                    InputStream inputStream = is;
                    GZIPInputStream input = new GZIPInputStream(inputStream);
                    InputStreamReader reader = new InputStreamReader(input);
                    BufferedReader in = new BufferedReader(reader);
                    StringBuilder sb = new StringBuilder();
                    String line = 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 object
    
                try {
    
                    JSONArray people = new JSONArray(json);
                    //JSONArray people = new JSONArray(json);
    
                    for (int i = 0; i < people.length(); i++) {
                        //System.out.println(courses.getJSONObject(i).toString());
                        JSONObject p = people.getJSONObject(i);
    
                        // Storing each json item in variable
                        String person_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 String
                return jObj;
                }
    
                protected void onPostExecute(JSONObject json) {
                    ListView myListView = (ListView)findViewById(R.id.peopleList);
                    myListView.setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, items));
            }
            }
    
    0 讨论(0)
提交回复
热议问题