3

编码:

public JSONArray getJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
    try {
        // defaultHttpClient
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        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, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        is.close();
        json = sb.toString();       
        Log.e("JSON", json);
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        JSONObject json_data = new JSONObject(json);
        JSONArray hashMap_names = json_data.names();
        JSONArray hashMap_names2 = new JSONArray();
        Map hashMap = new HashMap(json_data.length());
        for (int i=0; i!=hashMap_names.length(); i++){
            //Object obj = chaves.next();
        hashMap.put(String.valueOf(i),json_data.get(String.valueOf(i)));
            hashMap_names2.put(String.valueOf(i));
        }
        JSONObject hashMap_obj = new JSONObject (hashMap);
        jArr = hashMap_obj.toJSONArray(hashMap_names2);
        Log.e("JSON Parser", "succesful parsing data " + jArr.toString());
    } catch (Exception e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
        jArr = null;
    }

    return jArr;

}

在 stringbuilder.toString() 之后,json 具有以下值:

{"0":"27124","1":"Adsad adadda daddadadad ","2":"asdasdas@gmail.com","3":"732bcv874uhfebfehuif9724uhife","4":"wasd","5":"","6":"M","7":"","8":"","9":"","10":"","11":"","12":"06\/05\/1989","13":"","14":"","15":"","16":"","17":"","18":"","19":"","20":"BR","21":"","22":"0","23":"","24":"","25":"","26":"Y","27":"Y","28":"Y","29":"N","30":"0","31":"30\/04\/2012 16:48:20","32":"17\/04\/2012 01:09:27","33":"367","34":"50","35":"0","36":"79","37":"34","38":"","39":"17\/04\/2012 01:16:54","40":"3649","41":[null,null,null,null,null,null,null,null,null,null,null]}

在我看来,这是一个完美格式化的 JSON 文本。

但是当我尝试创建一个新的 JSONObject(json) 时,得到异常

Error parsing data org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONArray

但我只在使用在线服务器时收到此错误。如果我使用本地的(xammp),json 被解析为 JSONObject 并且应用程序可以工作。

我试过设置json = "{'0':'1212','1':'username','2':'email','3':'pass'}";,它奏效了!但是在使用时json = "\""+json.replace('\"', '\'')+"\"";遇到了同样的异常

顺便说一句,我使用 hashmap 只是为了在解析后对 de JSONObject 进行排序。

也许问题是因为在本地我使用的是 php 5.3,而在线服务器使用的是 php 5.2?这些版本的标头之间有什么区别吗?我怎么能验证这个?

4

1 回答 1

0
HttpURLConnection connection;
                    OutputStreamWriter request = null;
                    URL url = null;

                     String parameters = "parameter1="+parameter;

                    try {
                        url = new URL("http://myserver.com/samplejson.php");

                        connection = (HttpURLConnection) url.openConnection();
                        connection.setDoOutput(true);
                        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                        connection.setRequestMethod("POST");

                        request = new OutputStreamWriter(connection.getOutputStream());
                        request.write(parameters);
                        request.flush();
                        request.close();
                        String line = "";
                        InputStreamReader isr = new InputStreamReader(
                                connection.getInputStream());
                        BufferedReader reader = new BufferedReader(isr);
                        StringBuilder sb = new StringBuilder();
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }

                        response = sb.toString();
                         ///you will get response here..

                        isr.close();
                        reader.close();

                    } catch (MalformedURLException e) {

                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
于 2012-12-19T09:43:00.833 回答