Android ArrayList 到PHP

Android ArrayList <String []>到PHP

问题描述:

I need my app to send an ArrayList<String[]> to php, I have this to call the service:

ArrayList<String[]> Items = new ArrayList<String[]>();
(...)
JSONObject JSONSend= new JSONObject();
JSONSend.put("Items", Items );

HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000);
HttpResponse response;
HttpPost post = new HttpPost(SERVICE);
post.setHeader("json", JSONSend.toString());
StringEntity se = new StringEntity(JSONSend.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
post.setEntity(se);
response = client.execute(post);

and on the PHP service:

$data = file_get_contents('php://input');
$json = json_decode($data);
$Items = $json->{'Items'};
error_log($Items);

and the php is returning this:

[[Ljava.lang.String;@413e2fd0, [Ljava.lang.String;@413a2940, [Ljava.lang.String;@4139df18, [Ljava.lang.String;@4141b5b0, [Ljava.lang.String;@413931c8, [Ljava.lang.String;@41348b40, [Ljava.lang.String;@41393928]

The type of the $ServMade is string, so how can I take care of the data as an array on php? Am I missing something?

我需要我的应用程序向php发送 ArrayList&lt; String []&gt; code>, 我有这个来调用服务: p>

  ArrayList&lt; String []&gt;  Items = new ArrayList&lt; String []&gt;(); 
(...)
JSONObject JSONSend = new JSONObject(); 
JSONSend.put(“Items”,Items); 
 
HttpConnectionParams.setConnectionTimeout(client。  getParams(),10000); 
HttpResponse响应; 
HttpPost post = new HttpPost(SERVICE); 
post.setHeader(“json”,JSONSend.toString()); 
StringEntity se = new StringEntity(JSONSend.toString())  ; 
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,“application / json”)); 
post.setEntity(se); 
response = client.execute(post); 
  code>  pre> \  n 
 

和PHP服务: p>

  $ data = file_get_contents('php:// input'); 
 $ json = json_decode($ data)  ; 
 $ Items = $ json-&gt; {'Items'}; 
error_log($ Items); 
  code>  pre> 
 
 

并且php正在返回: p>

  [[Ljava.lang.String; @ 413e2fd0,[Ljava.lang.String; @ 413a2940,[Ljava.lang.String; @ 4139df18,[Ljava.lang.String;  @ 4141b5b0,[Ljava.lang.String; @ 413931c8,[Ljava.lang.String; @ 41348b40,[Ljava.lang.String; @ 41393928] 
  code>  pre> 
 
 

$ S的类型 ervMade是字符串,那么如何在php上将数据作为一个数组来处理呢? 我错过了什么吗? p> div>

Try the following when generating json string in android:

JSONObject JSONSend= new JSONObject();
JSONArray ja = null;
JSONArray tmp = new JSONArray();
for(String s[] : Items){
    ja = new JSONArray();
    for(int i = 0; i<s.length; i++)
        ja.put(s[i]);
    tmp.put(ja);
}
JSONSend.put("Items", tmp);

The problem here is that You call .toString() (within Java) on an array of strings (String[]) that while NOT overrided returns exactly what You get in the JSON: [Ljava.lang.String;@413e2fd0].

Don't know exactly the JSONObject but I guess You should transform the ArrayList<String[]> (so the array of arrays of strings) to something different for JSONObject to handle it correctly.

Also, in PHP, You do not have to call $Items = $json->{'Items'}; - it is okay to call $Items = $json->Items;.