【发布时间】:2014-08-17 17:07:46
【问题描述】:
我正在尝试将 JSONObject 转换为 JSONArray。我找到了很多例子并尝试了很多东西,但没有任何效果。
所以现在我尝试通过 POST 将 JSON 编码的数组发送到 PHP 脚本。但是,如果我尝试将 JSONObject 转换为 JSONArray,则会产生错误。演员表看起来像:
JSONArray jsonArr = jsonParent.getJSONArray("message");
LogCat 输出:
08-17 19:00:32.324:E/SmsReceiver(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
}
【问题讨论】: