Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSON解析器错误。此类型的值_Java_Android_Json - Fatal编程技术网

Java JSON解析器错误。此类型的值

Java JSON解析器错误。此类型的值,java,android,json,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.Ac

我对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();
            }


         }
    }

}
所以,很明显,我的问题是。为什么要返回此错误。 其次,为了帮助我自己弄清楚这些东西,我是否可以记录JSON结果原始数据以查看我的应用程序正在接收什么

这是LogCat输出:

在我的MainActivity窗口中,它在以下行中断:


谢谢。

解析数据org.json.jsonException时出错:此类型的java.lang.String值无法转换为JSONObject当您尝试将字符串转换为JSONObject且字符串不是json格式时,会发生此错误….JSONObject json=jParser.getJSONFromUrlurl;当然,url字符串确实验证json格式…在getJSONFromUrl方法定义内…第一个打印方法返回。。。i、 e.jObj.toString;并检查字符串是否为有效的json?您可以使用jsonlint.com字符串验证为有效的json,因此这不是问题所在。代码在以下行中断:android=json.getJSONArrayTAG_CLIENTS;好的,首先告诉我…异常发生在哪一行。。。?那是什么声明?该行必须位于创建jsonobject的位置。@AngadTiwari断点位于android=json.getJSONArrayTAG_CLIENTS;我用它的打印屏幕更新了我的问题。
    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();
                }


             }
        }

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