将Android ArrayList 的内容发送到PHP [英] Send the contents of Android ArrayList to PHP

查看:32
本文介绍了将Android ArrayList 的内容发送到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 PHP 开发人员(中级),在家练习一些 Android 的东西.

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

我已经创建了一个数组列表,它将获取到我的 Android 应用程序中的一个 sqlite 数据库并填充一个 ListView.现在我正在尝试更进一步.

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.

我想将该数组列表内容发送到我的 PHP 服务器,在那里我可以将给定的数据存储到 mysql 中并取回我的应用程序.

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?

推荐答案

为此,您必须将数据发布到 php 服务器上,然后获取该数据并存储到您的数据库中.

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

这里我附上一个例子,它在服务器上发送数据并在 json 中得到响应.

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();

这里的 sample[] 是一个字段,我在其中指定要在服务器上发送的数组值.从服务器端,您必须获取 sample[] 字段.

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();

}

这篇关于将Android ArrayList 的内容发送到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆