Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.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
异常解析字符串到JSONObject(在本地服务器上工作,但在在线服务器上不工作)android_Android_String_Json - Fatal编程技术网

异常解析字符串到JSONObject(在本地服务器上工作,但在在线服务器上不工作)android

异常解析字符串到JSONObject(在本地服务器上工作,但在在线服务器上不工作)android,android,string,json,Android,String,Json,守则: public JSONArray getJSONFromUrl(String url, List<NameValuePair> params) { // Making HTTP request try { // defaultHttpClient HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(u

守则:

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;

}
在我看来,wich是一个格式完美的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只是在解析后对dejsonObject进行排序


可能是因为我在本地使用的是PHP5.3,而在线服务器使用的是PHP5.2?这些版本的标题之间有什么区别?我如何验证这一点?

我建议用
json=EntityUtils.toString(httpEntity)替换所有疯狂的
InputStream
-和-
BufferedReader
内容
。这可能只是解决了您的编码问题(假设为UTF-8,忽略服务器所说的任何内容)首先,感谢您的帮助!我尝试了EntityUtils的东西,它在本地服务器上运行得很好,但是我在使用在线服务器时仍然遇到异常。但是logcat现在的消息是:错误解析数据org.json.JSONException:Valueï»类型java.lang.String不能转换为JSONObject。所以json之前有这样奇怪的问题re{“0”:“67987”,“1”:“ategdtg TADGTEG”,“2”:eytdgaetydg@yaeyedg.com“,…..},这可能是什么?在使用entityutils之前回顾异常,似乎在json:look上面存在一个null,“JSONException:Value of type…”。。。" . 也许通过删除这个,我就能解决这个问题?你能用当前代码和错误消息更新你的问题吗?这会让你更容易理解:)我会的。顺便说一句,theï»是一个BOM:想知道我是否可以使用BOMInputStream跳过它。这对我来说很有用,它可以得到响应。解析那个json对象。
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();
                    }
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();
                    }