开发者

Send the contents of Android ArrayList to PHP

开发者 https://www.devze.com 2023-02-16 04:23 出处:网络
I am working as a PHP developer (intermediate level) and do practice some android stuff at home. I have created and array list which will fetch into an sqlite db inside my Android app and populate a

I am working as a PHP developer (intermediate level) and do practice some android stuff at home.

I have created and array list which will fetch into an sqlite db inside my Android app and populate a ListView. Now I am trying to take that one 开发者_运维百科level further.

I want to send that array list content to my PHP server where I can store the data given into mysql and fetch back to my app.

How would I go about achieving this?


You can make use of JSON or XML for sending data from android to php server. On the PHP side, all you need is the built-in json_decode, which will deserialize your json and return an object or an associative array.


For that you have to post your data on php server, then fetch that data and store into your database .

Here i attach one example which send data on server and getting response in json .

                    HttpPost postMethod = new HttpPost("Your Url");
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();


                    // test is a one array list

                    for(int i=0;i<test.size();i++)
                    {
                        nameValuePairs.add(new BasicNameValuePair("sample[]", Integer.toString(test.get(i))));
                    }

                    postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    DefaultHttpClient hc = new DefaultHttpClient();

                    HttpResponse response = hc.execute(postMethod);
                    HttpEntity entity = response.getEntity();

                    // If the response does not enclose an entity, there is no need
                    // to worry about connection release

                    if (entity != null) 
                    {
                        InputStream inStream = entity.getContent();
                        result= convertStreamToString(inStream);
                        jsonObject = new JSONObject(result);
                        responseHandler.sendEmptyMessage(0);
                    }
                }
                catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }.start();

Here sample[] is a field in which i assign array value to send on server . From server side you have to fetch the sample[] field .


public static String convertStreamToString(InputStream is)
{
   BufferedReader reader = new BufferedReader(new InputStreamReader(is));
   StringBuilder sb = new StringBuilder();

   String line = null;
   try 
   {
       while ((line = reader.readLine()) != null) 
       {
           sb.append(line + "\n");
       }
   } 
   catch (IOException e) 
   {
       e.printStackTrace();
   } 
   finally 
   {
       try 
       {
           is.close();
       } 
       catch (IOException e) 
       {
           e.printStackTrace();
       }
   }
   return sb.toString();

}

0

精彩评论

暂无评论...
验证码 换一张
取 消