개발! 딱 깔끔하고 센스있게!

POST 안드로이드로 PHP 배열 보내기 본문

개발 스크랩 메모/PHP

POST 안드로이드로 PHP 배열 보내기

렉사이 2020. 11. 20. 16:40

post 를 통해서 php 디지털을 android 에서 php 서버로 보내려고 합니다.

다음 코드가 있습니다.

HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); StringEntity dades = new StringEntity(data); httppost.setEntity(dades); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); HttpEntity resEntity = response.getEntity(); return resEntity.getContent(); 

저는 php 그룹이 가능하다고 생각합니다.

stringEntity dades = 새로운 stringEntity (데이터);(데이터는 php 그룹)이다.

도와주실 분 있나요?

대답

public void postData() { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); try { // Add your data //you can add all the parameters your php needs in the BasicNameValuePair. //The first parameter refers to the name in the php field for example // $id=$_POST['id']; the second parameter is the value. List nameValuePairs = new ArrayList(2); nameValuePairs.add(new BasicNameValuePair("id", "12345")); nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); 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 }} 

위 코드 다음과 같이 보내기:[ID = 12345, Stringdata = anddev 멋있다!명

만약 네가 2층을 원한다면, 너는 이렇게 해야 한다.

Bundle b= new Bundle(); b.putString("id", "12345"); b.putString("stringdata", "Android is Cool"); nameValuePairs.add(new BasicNameValuePair("info", b.toString())); 

배열에 포함된 그룹을 만들 수 있습니다:

[info=Bundle[{id=12345, stringdata=Android is Cool}]] 

나는 이것이 네가 원하는 것이다.

Comments