Android 在.getJSONArray(“message”)中,JSONObject指向JSONArray错误;
我正在尝试将JSONObject转换为JSONArray。我找到了很多例子,尝试了很多东西,但都没有效果 现在我尝试通过POST将JSON编码的数组发送到PHP脚本。但是,如果我尝试将JSONObject转换为JSONArray,则会生成错误。演员阵容如下:Android 在.getJSONArray(“message”)中,JSONObject指向JSONArray错误;,android,json,Android,Json,我正在尝试将JSONObject转换为JSONArray。我找到了很多例子,尝试了很多东西,但都没有效果 现在我尝试通过POST将JSON编码的数组发送到PHP脚本。但是,如果我尝试将JSONObject转换为JSONArray,则会生成错误。演员阵容如下: JSONArray jsonArr = jsonParent.getJSONArray("message"); LogCat输出: 08-17 19:00:32.324:E/SMS接收器(17846):异常 smsReceiverorg.
JSONArray jsonArr = jsonParent.getJSONArray("message");
LogCat输出:
08-17 19:00:32.324:E/SMS接收器(17846):异常
smsReceiverorg.json.JSONException:消息没有值
以下是我想做的事情的所有代码:
// Generating the Objects
JSONObject jsonChild= new JSONObject();
JSONObject jsonParent= new JSONObject();
try {
jsonChild.put("with_img", false);
jsonChild.put("message", message);
jsonChild.put("img", ""); // base64 codiertes Bild
jsonChild.put("number", senderNum);
jsonChild.put("time", time); // UNIX Timestamp
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// here enter code here expire the problem
JSONArray jsonArr = jsonParent.getJSONArray("message");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/ticker.php");
StringEntity se = new StringEntity(jsonArr.toString());
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.addHeader("Accept", "application/json");
httppost.addHeader("Content-type", "application/json; charset=UTF-8");
httppost.setEntity(se);
HttpEntity entitySend = httppost.getEntity();
String htmlSend = EntityUtils.toString(entitySend);
Log.d("Sended:", ""+ htmlSend);
try {
HttpResponse response = httpclient.execute(httppost);
// Just for reading the server response.
HttpEntity entity = response.getEntity();
String htmlResponse = EntityUtils.toString(entity);
Log.d("Received:", ""+ htmlResponse);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
您的
jsonParent
对象根本不包含名为message
的值。如果需要更多帮助,请粘贴json数据的内容
现在我尝试通过POST将JSON编码的数组发送到PHP脚本。但是,如果我尝试将JSONObject转换为JSONArray,则会生成错误。演员阵容如下:
JSONArray jsonArr = jsonParent.getJSONArray("message");
JSONArray jsonArr=jsonParent.getJSONArray(“消息”)
您没有在该行中将JSONObject
强制转换为JSONArray
。您正在尝试获取jsonParent
中名为“message”的数组。基本上,如果您的JSONObject包含:
{
...
"message" : [
{
"attr1" : "value1",
"attr2" : "value2",
...
},
{
...
}
...
]
...
}
他们jsonArr
将为您返回该消息数组
如果您想将JSONObject
添加到JSONArray
中,他们将创建一个JSONArray
并将其添加到其中
JSONArray jsonArray = new JSONArray();
jsonArray.put(jsonChild);
我相信,你想做的是:
// Generating one Object
JSONObject jsonThingy = new JSONObject();
try {
jsonThingy.put("with_img", false);
jsonThingy.put("message", message);
jsonThingy.put("img", ""); // base64 codiertes Bild
jsonThingy.put("number", senderNum);
jsonThingy.put("time", time); // UNIX Timestamp
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringEntity se = new StringEntity(jsonThingy.toString());
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/ticker.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("message", se));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
//生成一个对象
JSONObject jsonThingy=新的JSONObject();
试试{
jsonThingy.put(“with_img”,false);
jsonThingy.put(“消息”,消息);
jsonThingy.put(“img”,”);//base64 codiertes Bild
jsonThingy.put(“数字”,senderNum);
jsonThingy.put(“time”,time);//UNIX时间戳
}捕获(JSONException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
StringEntity se=新的StringEntity(jsonThingy.toString());
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://example.com/ticker.php");
试一试{
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“消息”,se));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}