无法将JsonObject转换为JsonArray

时间:2015-07-18 10:15:02

标签: java

 @Override
    public void onCreate(Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode is most commonly used to catch accidental disk or network access on the application's main thread
                .penaltyLog().build());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client_http);

        Fooditem = (EditText) findViewById(R.id.editText1);
        submit = (Button) findViewById(R.id.submitbutton);
        tv = (TextView) findViewById(R.id.showresult);

        // define the action when user clicks on submit button
        submit.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                // declare parameters that are passed to PHP script i.e. the name "birthyear" and its value submitted by user
                ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();

                // define the parameter
                postParameters.add(new BasicNameValuePair("id",
                        Fooditem.getText().toString()));
                String response = null;

                // call executeHttpPost method passing necessary parameters
                try {
                    response = JSONCLient.executeHttpPost(
                            "http://api.searchcompany.us/1.0/company/282433", // your ip address if using localhost server
                            //"main site path here",  // in case of a remote server
                            postParameters);

                    String result = response.toString();

                    try{
                        returnString = "";

                        JSONArray jArray1 = new JSONArray(result);
                        for(int i=0;i<jArray1.length();i++){
                            JSONObject json_data1 = jArray1.getJSONObject(i);
                            tv.setText(json_data1.toString());
                            Log.i("log_tag", "id: " + json_data1.getString("id") +
                                            ", name: " + json_data1.getString("name") +
                                            ", state: " + json_data1.getString("state") +
                                            ", city: " + json_data1.getString("name") +
                                            ", zip: " + json_data1.getString("state") +
                                            ", url: " + json_data1.getString("url")

                            );
                            returnString += "\nid: "+ json_data1.getString("id")
                                    + "\nname: "+ json_data1.getString("name")
                                    + "\nstate: " + json_data1.getString("state")
                                    + "\ncity: "+ json_data1.getString("city")
                                    + "\nzip: " + json_data1.getString("zip")
                                    + "\nurl: " + json_data1.getString("url")
                            ;
                        }
                    }
                    catch(JSONException e){
                        Log.e("log_tag", "Error parsing data "+e.toString());
                    }

                    try{
                        tv.setText(returnString);
                    }
                    catch(Exception e){
                        Log.e("log_tag","Error in Display!" + e.toString());;
                    }
                }
                catch ( Exception e) {
                    Log.e("log_tag","Error in http connection!!" + e.toString());
                }
            }
        });
    }

请检查代码并帮我解决这个问题..我哪里出错了?

1 个答案:

答案 0 :(得分:1)

在点击此网址http://api.searchcompany.us/1.0/company/282433时,这就是我得到的: -

{
    "id": 282433,
    "name": "Apple Valley Townhouses",
    "state": "Arkansas",
    "city": "Sherwood",
    "zip": "72120-3854",
    "url": "http://searchcompany.us/company/282433"
}

上述内容不是JSONArray,而是JSONObject。所以不要这样做: -

     JSONArray jArray1 = new JSONArray(result);
        for(int i=0;i<jArray1.length();i++){}

尝试这样做: -

JSONObject json_data1 = new JSONObject(result);
Log.i("log_tag", "id: " + json_data1.getString("id") + ", name: " + json_data1.getString("name") +", state: " + json_data1.getString("state") + ", city: " + json_data1.getString("name") +", zip: " + json_data1.getString("state") +", url: " + json_data1.getString("url")
);