Android如何从数组创建一个json并发送到php服务器

时间:2015-07-28 06:15:27

标签: php android json arraylist

我正在开发一个应用程序,它生成安装应用程序列表并创建一个json并将json发送到我的php服务器。在php服务器上,我必须得到所有数据并按照它进行处理。

这是生成已安装应用程序列表的代码:

List<PackageInfo> apps = getPackageManager().getInstalledPackages(PackageManager.GET_META_DATA);

    ArrayList<String[]> aux = new ArrayList();

    for (int i = 0; i < apps.size(); i++)
    {
        if (apps.get(i).versionCode != 0 && ((apps.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1))
        {
            String[] temp = new String[2];

            String name = apps.get(i).packageName;
            String versionName = apps.get(i).versionName;
            temp[0] = name;
            temp[1] = versionName;
            aux.add(temp);
        }
    }

这是将它以json格式发送到php服务器的代码:

try
        {
JSONObject obj = new JSONObject();
        obj.put("Applications", aux);
            URL url = new URL(IPClass.SERVERPath + "update.php");

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("POST");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
            //OutputStreamWriter writer = new OutputStreamWriter(os);

            writer.write(String.valueOf(aux));
            writer.flush();
            writer.close();
            os.close();


            InputStream input = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            StringBuilder result = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            Log.d("doInBackground(Resp)", result.toString());

        }
        catch(Exception ex)
        {
            Log.d("Ex", ex.toString());
        }

问题是json没有正确生成,其次我怎么能在php服务器上获取它!

请帮忙! 感谢

2 个答案:

答案 0 :(得分:3)

它会正常工作。

JSONArray installedList = new JSONArray();
for (int i = 0; i < apps.size(); i++)
{
    if (apps.get(i).versionCode != 0 && ((apps.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 1))
    {
        String name = apps.get(i).packageName;
        String versionName = apps.get(i).versionName;
        JSONObject installedPackage = new JSONObject();
        installedPackage.put("name", name);
        installedPackage.put("versionName", versionName);
        installedList.put(installedPackage);
    }
}
String dataToSend = installedList.toString();

添加此行不会神奇地将您的数组转换为JSON格式。

conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

您必须创建JSONArray或JSONObject。我建议你用JSONObject创建JSONArray。

答案 1 :(得分:0)

你有没有尝试过:

JSONObject json = new JSONObject();
json.putString("key", "value");

并使其成为字符串:

json.toString();