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('\"', '\'')+"\"";すると同じ例外が発生しました

ちなみに、解析後に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 に答える