在Android中org.json.JSONObject不能转化为JSONArray转化为、org、Android、json

由网友(㊣數123)分享简介:当我尝试在我的本地主机它工作的发现。这是提供通过我的本地JSON的。在这个网址Ÿ它错误 http://api.androidhive.info/contacts/ [{ID:1,FIRST_NAME:人,姓氏:女人,用户名:人,密码:4f70432e636970de9929bcc6f1b72412电子邮件:man@...

当我尝试在我的本地主机它工作的发现。这是提供通过我的本地JSON的。

在这个网址

Ÿ它错误 http://api.androidhive.info/contacts/

  [{
     ID:1,
     FIRST_NAME:人,
     姓氏:女人,
     用户名:人,
     密码:4f70432e6369
70de9929bcc6f1b72412
     电子邮件:man@gmail.com
     URL:HTTP: /  /本地主机 / adchara1  /
 },{
     ID:6,
     FIRST_NAME:第一,
     姓氏:去年
名称,
     用户名:用户,
     密码:1a1dc91c907325c69271ddf0c944bc72
     电子邮件:0,
     URL:HT
TP: /  / api.androidhive.info  /触点 /
 },{
     ID:7,
     如first_name:1,
     姓氏:2,
     用户名:us45
     密码:33d8f54e33896a5722
7b18642979e558
     电子邮件:first@gmail.com
     URL:HTTP: /  / ugirusgiarto.word press.com  / 2011 
/ 10  / 27  / JSON-PHP-MySQL的与 -  AsyncTask的-progressdialog  /
 },{
     ID:9,
     FIRST_NAME:名字,
     姓氏:去年
名称,
     用户名:用户名,
     密码:dc647eb65e6711e155375218212b3964
     电子邮件:女人@克
ail.com
     URL:HTTP: /  / mobile.cs.fsu.edu  /解析JSON的对象 - 在-的AsyncTask  /
 }]
 

X

MainActivity

 公共类MainActivity延伸活动{
    TextView的文本1,文本;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);

        新的任务()执行()。
    }

    一流的任务扩展的AsyncTask<字符串,字符串,太虚> {
        私人ProgressDialog progressDialog =新ProgressDialog(
                MainActivity.this);
        InputStream的是= NULL;
        字符串结果=;

        在preExecute保护无效(){
            progressDialog.setMessage(下载数据......);
            progressDialog.show();
            progressDialog.setOnCancelListener(新OnCancelListener(){
                公共无效OnCancel的(DialogInterface为arg0){
                    task.this.cancel(真正的);
                }
            });
        }

        @覆盖
        保护无效doInBackground(字符串... PARAMS){
//字符串url_select =htt​​p://192.168.10.111/adchara1/;
             字符串url_select =htt​​p://api.androidhive.info/contacts/;

            HttpClient的HttpClient的=新DefaultHttpClient();
            HttpPost httpPost =新HttpPost(url_select);

            ArrayList的<的NameValuePair>参数=新的ArrayList<的NameValuePair>();

            尝试 {
                httpPost.setEntity(新UrlEn codedFormEntity(参数));

                HTT presponse HTT presponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = HTT presponse.getEntity();

                //读取内容
                是= httpEntity.getContent();

            }赶上(例外五){

                Log.e(log_tag,错误的HTTP连接+ e.toString());
            }
            尝试 {
                的BufferedReader BR =新的BufferedReader(
                        新的InputStreamReader(是));
                StringBuilder的SB =新的StringBuilder();
                串线=;
                而((行= br.readLine())!= NULL){
                    sb.append(行+ N);
                }
                is.close();
                结果= sb.toString();

            }赶上(例外五){
                // TODO:处理异常
                Log.e(log_tag,错误转换结果+ e.toString());
            }

            返回null;

        }

        保护无效onPostExecute(空V){

            // ambil数据达里语的Json数据库
            尝试 {
                JSONArray Jarray =新JSONArray(结果);
                的for(int i = 0; I< Jarray.length();我++){
                    JSONObject的Jasonobject = NULL;
                    _1 =(TextView中)findViewById(R.id.txt1);
                    Jasonobject = Jarray.getJSONObject(ⅰ);

                    //获取屏幕上的输出
                    字符串ID = Jasonobject.getString(ID);
                    字符串名称= Jasonobject.getString(姓名);
                    字符串email = Jasonobject.getString(电子邮件);
                    字符串的地址= Jasonobject.getString(地址);
                    字符串性别= Jasonobject.getString(性别);
                    text_1.append( N+编号+ t  t+姓名+ t  t  t的
                            +电子邮件+ t  t  t  t+地址+ t  t  t  t+性别
                            + t  t  t  t+ N);

                }
                this.progressDialog.dismiss();

            }赶上(例外五){
                // TODO:处理异常
                Log.e(log_tag,错误分析数据+ e.toString());
            }
        }
    }

    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }
}
 
JSONArray和JSONObject的区别

错误

 错误解析数据org.json.JSONException:值{
    人脉:[{
        ID:C200,
        性别:男,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:ravi@gmail.com
        名:拉维玉田
    },{
        ID:C201,
        性别:男,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:johnny_depp@gmail.com
        名:约翰尼·德普
    },{
        ID:C202,
        性别:男,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:leonardo_dicaprio@gmail.com
        名:莱昂纳多·迪卡普里奥
    },{
        ID:C203,
        性别:男,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:john_wayne@gmail.com
        名:约翰·韦恩
    },{
        ID:C204,
        性别女,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:angelina_jolie@gmail.com
        名:安吉丽娜·朱莉
    },{
        ID:C205,
        性别女,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:dido@gmail.com
        名:恶作剧
    },{
        ID:C206,
        性别女,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:adele@gmail.com
        名:阿黛尔
    },{
        ID:C207,
        性别:男,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:hugh_jackman@gmail.com
        名:休·杰克曼
    },{
        ID:C208,
        性别:男,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:will_smith@gmail.com
        名:威尔·史密斯
    },{
        ID:C209,
        性别:男,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:clint_eastwood@gmail.com
        名:克林特·伊斯特伍德
    },{
        ID:C2010
        性别:男,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:barack_obama@gmail.com
        名:巴拉克·奥巴马
    },{
        ID:C2011
        性别女,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:kate_winslet@gmail.com
        名:凯特·温斯莱特
    },{
        ID:c2012
        性别:男,
        电话: {
            写字楼:00 000000,
            家:00 000000,
            手机:+91 0000000000
        },
        地址:XX-XX-XXXX,X  - 街道,X  - 国家,
        电子邮件:eminem@gmail.com
        名:阿姆
    }]
}
型的org.json.JSONObject不能转换到JSONArray
 

解决方案

试试这样

 的JSONObject对象=新的JSONObject的(结果);
JSONArray Jarray = object.getJSONArray(接触);

的for(int i = 0; I< Jarray.length();我++){
   的JSONObject Jasonobject = Jarray.getJSONObject(ⅰ);
 

When I try in my localhost it work find. this is the JSON that provide by my localhost.

Y it error in this url http://api.androidhive.info/contacts/

         [{
     "id": "1",
     "first_name": "man",
     "last_name": "woman",
     "username": "man",
     "password": "4f70432e6369
70de9929bcc6f1b72412",
     "email": "man@gmail.com",
     "url": "http://localhost/adchara1/"
 }, {
     "id": "6",
     "first_name": "first",
     "last_name": "Last 
Name",
     "username": "user",
     "password": "1a1dc91c907325c69271ddf0c944bc72",
     "email": "0",
     "url": "ht
tp://api.androidhive.info/contacts/"
 }, {
     "id": "7",
     "first_name": "1",
     "last_name": "2",
     "username": "us45",
     "password": "33d8f54e33896a5722
7b18642979e558",
     "email": "first@gmail.com",
     "url": "http://ugirusgiarto.wordpress.com/2011
/10/27/json-php-mysql-with-asynctask-progressdialog/"
 }, {
     "id": "9",
     "first_name": "First Name",
     "last_name": "Last 
Name",
     "username": "Username",
     "password": "dc647eb65e6711e155375218212b3964",
     "email": "woman@gm
ail.com",
     "url": "http://mobile.cs.fsu.edu/parse-json-objects-in-asynctask/"
 }]

x

MainActivity

public class MainActivity extends Activity {
    TextView text_1, text_2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new task().execute();
    }

    class task extends AsyncTask<String, String, Void> {
        private ProgressDialog progressDialog = new ProgressDialog(
                MainActivity.this);
        InputStream is = null;
        String result = "";

        protected void onPreExecute() {
            progressDialog.setMessage("Download data...");
            progressDialog.show();
            progressDialog.setOnCancelListener(new OnCancelListener() {
                public void onCancel(DialogInterface arg0) {
                    task.this.cancel(true);
                }
            });
        }

        @Override
        protected Void doInBackground(String... params) {
//          String url_select = "http://192.168.10.111/adchara1/";
             String url_select = "http://api.androidhive.info/contacts/";

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url_select);

            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(param));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();

                // read content
                is = httpEntity.getContent();

            } catch (Exception e) {

                Log.e("log_tag", "Error in http connection " + e.toString());
            }
            try {
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line = "";
                while ((line = br.readLine()) != null) {
                    sb.append(line + "n");
                }
                is.close();
                result = sb.toString();

            } catch (Exception e) {
                // TODO: handle exception
                Log.e("log_tag", "Error converting result " + e.toString());
            }

            return null;

        }

        protected void onPostExecute(Void v) {

            // ambil data dari Json database
            try {
                JSONArray Jarray = new JSONArray(result);
                for (int i = 0; i < Jarray.length(); i++) {
                    JSONObject Jasonobject = null;
                    text_1 = (TextView) findViewById(R.id.txt1);
                    Jasonobject = Jarray.getJSONObject(i);

                    // get an output on the screen
                    String id = Jasonobject.getString("id");
                    String name = Jasonobject.getString("name");
                    String email = Jasonobject.getString("email");
                    String address = Jasonobject.getString("address");
                    String gender = Jasonobject.getString("gender");
                    text_1.append("n" + id + "tt" + name + "ttt"
                            + email + "tttt" + address + "tttt" + gender
                            + "tttt" + "n");

                }
                this.progressDialog.dismiss();

            } catch (Exception e) {
                // TODO: handle exception
                Log.e("log_tag", "Error parsing data " + e.toString());
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

ERROR

Error parsing data org.json.JSONException: Value {
    "contacts": [{
        "id": "c200",
        "gender": "male",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "ravi@gmail.com",
        "name": "Ravi Tamada"
    }, {
        "id": "c201",
        "gender": "male",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "johnny_depp@gmail.com",
        "name": "Johnny Depp"
    }, {
        "id": "c202",
        "gender": "male",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "leonardo_dicaprio@gmail.com",
        "name": "Leonardo Dicaprio"
    }, {
        "id": "c203",
        "gender": "male",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "john_wayne@gmail.com",
        "name": "John Wayne"
    }, {
        "id": "c204",
        "gender": "female",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "angelina_jolie@gmail.com",
        "name": "Angelina Jolie"
    }, {
        "id": "c205",
        "gender": "female",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "dido@gmail.com",
        "name": "Dido"
    }, {
        "id": "c206",
        "gender": "female",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "adele@gmail.com",
        "name": "Adele"
    }, {
        "id": "c207",
        "gender": "male",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "hugh_jackman@gmail.com",
        "name": "Hugh Jackman"
    }, {
        "id": "c208",
        "gender": "male",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "will_smith@gmail.com",
        "name": "Will Smith"
    }, {
        "id": "c209",
        "gender": "male",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "clint_eastwood@gmail.com",
        "name": "Clint Eastwood"
    }, {
        "id": "c2010",
        "gender": "male",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "barack_obama@gmail.com",
        "name": "Barack Obama"
    }, {
        "id": "c2011",
        "gender": "female",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "kate_winslet@gmail.com",
        "name": "Kate Winslet"
    }, {
        "id": "c2012",
        "gender": "male",
        "phone": {
            "office": "00 000000",
            "home": "00 000000",
            "mobile": "+91 0000000000"
        },
        "address": "xx-xx-xxxx,x - street, x - country",
        "email": "eminem@gmail.com",
        "name": "Eminem"
    }]
}
of type org.json.JSONObject cannot be converted to JSONArray

解决方案

try like this

JSONObject object = new JSONObject(result);
JSONArray Jarray = object.getJSONArray("contacts");

for (int i = 0; i < Jarray.length(); i++) {
   JSONObject Jasonobject = Jarray.getJSONObject(i);

阅读全文

相关推荐

最新文章