Send the contents of Android ArrayList to PHP

前端 未结 2 827
夕颜
夕颜 2021-01-24 02:34

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

相关标签:
2条回答
  • 2021-01-24 02:40

    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.

    0 讨论(0)
  • 2021-01-24 02:42

    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 讨论(0)
提交回复
热议问题