1

I am working on an application that generates list of install applications and create a json and send the json to my php server. At php server the I have to get all data and process it acordingly.

This is the code that generates list of installed applications:

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

This is the code to send it in json form to php server:

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

The problem is that the json is not getting generated properly and second how can i get it on php server!

Any help please! Thanks

1
  • Where you are creating json Commented Jul 28, 2015 at 6:20

2 Answers 2

3

It will work fine.

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

Adding this line won't magically turn your array to JSON format.

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

You have to create JSONArray or JSONObject. I recommend you create JSONArray with JSONObject.

Sign up to request clarification or add additional context in comments.

Comments

0

Did you try:

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

And to make it String:

json.toString();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.