Send the contents of Android ArrayList to PHP

匿名 (未验证) 提交于 2019-12-03 02:34:02

问题:

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?

回答1:

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.



回答2:

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

}



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!