gpt4 book ai didi

android - 将字符串解析为 JSONObject 的异常(在本地服务器上有效,但在在线服务器上无效)android

转载 作者:太空狗 更新时间:2023-10-29 15:18:24 24 4
gpt4 key购买 nike

代码:

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 只是为了在解析后对 JSONObject 进行排序。

也许问题是因为在本地我使用的是 php 5.3,而在线服务器使用的是 php 5.2?这些版本的 header 之间有什么区别吗?我如何验证这一点?

最佳答案

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();
}

关于android - 将字符串解析为 JSONObject 的异常(在本地服务器上有效,但在在线服务器上无效)android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10390413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com