2014-06-18 17 views
1

我無法使用android中的多部分實體將JSONArray字符串發送到php服務器。我曾嘗試以下,但它不工作:如何將JSONArray字符串發送到使用Android中的多部分實體的PHP服務器

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,BOUNDARY,Charset.defaultCharset()); 
entity.addPart("invite_friend", new StringBody(friendsArray)); 

在PHP服務器端,它也應該像:

'invite_friend' => array 
    (
     0 => '800' 
     1 => '794' 
    ) 

可以做些什麼,請提供建議。

+0

它不是一個'JSONArray' –

+0

是指這樣的回答:http://stackoverflow.com/questions/18050533/how-to-send-file-in-json-on-android/18050865#18050865 –

+0

那「friendsArray」包含字符串值對中的[「800」,「794」]:「invite_friend」:[「800」,「794」]。如何使用Multipart發送此[「800」,「794」]「數據。 – user1182217

回答

1

你可以做這樣的事情。

// Prepare Category Array 
for (String mFrndsID : friendsArray) { 
    reqEntity.addPart("invite_friend[]", new StringBody(mFrndsID)); 
} 

只要在數組標籤和paas值中加上[]就可以了。 這裏invite_friend是數組標籤。你可以通過使用循環在你的標籤中傳遞你的值。它將作爲服務器上的數組發佈。

參考這個答案詳細

How to send the string array of values in one key word using post method to the server

這可能會幫助你

+0

這按預期工作。謝謝。 – user1182217

+0

很高興幫助.. –

0

試試這個代碼發送多個值對服務器端腳本。

String strUrl = "http://****/****.php"; 
url = new URL(strUrl); 

HttpURLConnection connection = (HttpURLConnection) url 
     .openConnection(); 
connection.setRequestMethod("POST"); 
connection.setDoOutput(true); 
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
     connection.getOutputStream()); 

outputStreamWriter.write("user_names_giver=" +user_names_giver + "&tcredit="+tcredit+"&user_name_receiver="+user_name_receiver);     
outputStreamWriter.flush(); 
outputStreamWriter.close(); 

InputStream iStream = connection.getInputStream(); 
BufferedReader reader = new BufferedReader(new 
InputStreamReader(iStream)); 

StringBuffer sb = new StringBuffer(); 

String line = ""; 

while((line = reader.readLine()) != null) 
{ 
    sb.append(line); 
} 

reader.close(); 
iStream.close(); 
相關問題