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