JSON Parser错误。价值这种类型

时间:2014-10-21 11:51:07

标签: java android json

我对android开发完全陌生,因此我的问题可能格式不正确。

我在MainActivity.java中有以下内容:

package learn2crack.listview;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;



import learn2crack.listview.library.JSONParser;



public class MainActivity extends Activity {
    ListView list;
    TextView name;
    TextView address;
    TextView status;
    Button Btngetdata;
    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

    //URL to get JSON Array
    private static String url = "http://portal.ontimebemanning.no/api/json/clients/";

    //JSON Node Names 
    private static final String TAG_CLIENTS = "clients";
    private static final String JSON_NAME = "firm";
    private static final String JSON_ADDRESS = "address";
    private static final String JSON_STATUS = "status";

    JSONArray android = null;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        oslist = new ArrayList<HashMap<String, String>>();



        Btngetdata = (Button)findViewById(R.id.getdata);
        Btngetdata.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                 new JSONParse().execute();


            }
        });


    }



    private class JSONParse extends AsyncTask<String, String, JSONObject> {
         private ProgressDialog pDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
             name = (TextView)findViewById(R.id.name);
             address = (TextView)findViewById(R.id.address);
             status = (TextView)findViewById(R.id.status);
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Getting Data ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();



        }

        @Override
        protected JSONObject doInBackground(String... args) {

            JSONParser jParser = new JSONParser();

            // Getting JSON from URL
            JSONObject json = jParser.getJSONFromUrl(url);
            return json;
        }
         @Override
         protected void onPostExecute(JSONObject json) {
             pDialog.dismiss();
             try {
                    // Getting JSON Array from URL

                    android = json.getJSONArray(TAG_CLIENTS);
                    for(int i = 0; i < android.length(); i++){
                    JSONObject c = android.getJSONObject(i);

                    // Storing  JSON item in a Variable
                    String name = c.getString(JSON_NAME);
                    String address = c.getString(JSON_ADDRESS);
                    String status = c.getString(JSON_STATUS);




                    // Adding value HashMap key => value


                    HashMap<String, String> map = new HashMap<String, String>();

                    map.put(JSON_NAME, name);
                    map.put(JSON_ADDRESS, address);
                    map.put(JSON_STATUS, status);

                    oslist.add(map);
                    list=(ListView)findViewById(R.id.list);





                    ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                            R.layout.list_v,
                            new String[] { JSON_NAME,JSON_ADDRESS, JSON_STATUS }, new int[] {
                                    R.id.name,R.id.address, R.id.status});

                    list.setAdapter(adapter);
                    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();

                        }
                    });

                    }
            } catch (JSONException e) {
                e.printStackTrace();
            }


         }
    }

}

在JSONParser.java中:

    package learn2crack.listview;

    import java.util.ArrayList;
    import java.util.HashMap;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.TextView;
    import android.widget.Toast;



    import learn2crack.listview.library.JSONParser;



    public class MainActivity extends Activity {
        ListView list;
        TextView name;
        TextView address;
        TextView status;
        Button Btngetdata;
        ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

        //URL to get JSON Array
        private static String url = "HIDDEN_FOR_SECURITY_PURPOSES";

        //JSON Node Names 
        private static final String TAG_CLIENTS = "clients";
        private static final String JSON_NAME = "firm";
        private static final String JSON_ADDRESS = "address";
        private static final String JSON_STATUS = "status";
        package learn2crack.listview.library;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            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, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            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 {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

        JSONArray android = null;




        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);
            oslist = new ArrayList<HashMap<String, String>>();



            Btngetdata = (Button)findViewById(R.id.getdata);
            Btngetdata.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                     new JSONParse().execute();


                }
            });


        }



        private class JSONParse extends AsyncTask<String, String, JSONObject> {
             private ProgressDialog pDialog;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                 name = (TextView)findViewById(R.id.name);
                 address = (TextView)findViewById(R.id.address);
                 status = (TextView)findViewById(R.id.status);
                pDialog = new ProgressDialog(MainActivity.this);
                pDialog.setMessage("Getting Data ...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();



            }

            @Override
            protected JSONObject doInBackground(String... args) {

                JSONParser jParser = new JSONParser();

                // Getting JSON from URL
                JSONObject json = jParser.getJSONFromUrl(url);
                return json;
            }
             @Override
             protected void onPostExecute(JSONObject json) {
                 pDialog.dismiss();
                 try {
                        // Getting JSON Array from URL

                        android = json.getJSONArray(TAG_CLIENTS);
                        for(int i = 0; i < android.length(); i++){
                        JSONObject c = android.getJSONObject(i);

                        // Storing  JSON item in a Variable
                        String name = c.getString(JSON_NAME);
                        String address = c.getString(JSON_ADDRESS);
                        String status = c.getString(JSON_STATUS);




                        // Adding value HashMap key => value


                        HashMap<String, String> map = new HashMap<String, String>();

                        map.put(JSON_NAME, name);
                        map.put(JSON_ADDRESS, address);
                        map.put(JSON_STATUS, status);

                        oslist.add(map);
                        list=(ListView)findViewById(R.id.list);





                        ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist,
                                R.layout.list_v,
                                new String[] { JSON_NAME,JSON_ADDRESS, JSON_STATUS }, new int[] {
                                        R.id.name,R.id.address, R.id.status});

                        list.setAdapter(adapter);
                        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                            @Override
                            public void onItemClick(AdapterView<?> parent, View view,
                                                    int position, long id) {
                                Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("name"), Toast.LENGTH_SHORT).show();

                            }
                        });

                        }
                } catch (JSONException e) {
                    e.printStackTrace();
                }


             }
        }

    }

当我调试我的应用程序时,它会因以下错误而停止:

解析数据时出错org.json.jsonException:Value类型java.lang.String无法转换为JSONObject

到目前为止,我还没试过用谷歌搜索它。

url返回的数据应该是有效的json。 来自URL的结果示例:

{"database":"clients","clients":[{"id":"1","firm":"Ringnes NT","address":"Postboks 7152 Majorstua","address2":"CostCenter:222"}]}

所以,显然,我的问题是。为什么我会收到此错误。 其次,为了帮助我自己弄清楚这些东西,我还能记录JSON结果原始数据以查看我的应用程序收到的内容吗?

这是LogCat输出: enter image description here

在我的MainActivity窗口中,它在这一行中断: enter image description here

感谢。

0 个答案:

没有答案