【问题标题】:Android How to create a json from array and send to php serverAndroid 如何从数组创建 json 并发送到 php 服务器
【发布时间】:2015-10-18 13:20:41
【问题描述】:

我正在开发一个生成安装应用程序列表并创建 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 服务器上获取它!

请帮忙! 谢谢

【问题讨论】:

  • 你在哪里创建 json

标签: php android json arraylist


【解决方案1】:

它会正常工作的。

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。

【讨论】:

    【解决方案2】:

    你试过了吗:

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

    并使其成为字符串:

    json.toString();
    

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 1970-01-01
      • 2013-01-30
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 2019-10-03
      • 1970-01-01
      相关资源
      最近更新 更多