0

hello everyone I want to send two parameters to server

  1. android id
  2. installed app information

I wrote a code to know installed app and print in logcat and also android_id but when I try to send installed app information on server I have a error

code

    public class MainActivity extends Activity {
    String AppName;
    String android_id;
    PackageInfo packageInfo;
    String installedapps;
    StringBuilder sb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        android_id = Secure.getString(MainActivity.this.getContentResolver(),
                Secure.ANDROID_ID);

        getInstalledApps();

    }

    public void getInstalledApps() {
        List<PackageInfo> PackList = getPackageManager()
                .getInstalledPackages(0);

        sb = new StringBuilder();

        for (int i = 0; i < PackList.size(); i++) {

            PackageInfo PackInfo = PackList.get(i);

            if (((PackInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) != true) {
                AppName = PackInfo.applicationInfo.loadLabel(
                        getPackageManager()).toString();
                Log.d("install ", AppName);
                sb.append(AppName + "\"");
            }

        }
        Toast.makeText(getApplicationContext(), sb.toString(),
                Toast.LENGTH_LONG).show();

        Log.d("install232222222 ", sb.toString());

        connectWithHttpGet(android_id, sb.toString());

    }

    private void connectWithHttpGet(String diviceID, String InstalledApps) {
        class HttpGetAsyncTask extends AsyncTask<String, Void, String> {

            @Override
            protected String doInBackground(String... params) {
                String deviceId = params[0];
                String list = params[1];
                System.out.println("diviceID is :" + deviceId
                        + " InstalledApps is :" + list);
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(
                        "http://************/api/***********.aspx?deviceId="
                                + deviceId + "&list=" + list);

                try {

                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    System.out.println("httpResponse");
                    InputStream inputStream = httpResponse.getEntity()
                            .getContent();
                    InputStreamReader inputStreamReader = new InputStreamReader(
                            inputStream);
                    BufferedReader bufferedReader = new BufferedReader(
                            inputStreamReader);
                    StringBuilder stringBuilder = new StringBuilder();

                    String bufferedStrChunk = null;
                    while ((bufferedStrChunk = bufferedReader.readLine()) != null) {
                        stringBuilder.append(bufferedStrChunk);
                    }
                    System.out.println("Returning value of doInBackground :"
                            + stringBuilder.toString());
                    return stringBuilder.toString();

                } catch (ClientProtocolException cpe) {
                    System.out
                            .println("Exception generates caz of httpResponse :"
                                    + cpe);
                    cpe.printStackTrace();
                } catch (IOException ioe) {
                    System.out
                            .println("Second exception generates caz of httpResponse :"
                                    + ioe);
                    ioe.printStackTrace();
                }

                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);

                if (result.equals("working")) {
                    Toast.makeText(getApplicationContext(),
                            "HTTP GET is working...", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Invalid...",
                            Toast.LENGTH_LONG).show();
                }
            }
        }

        HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();

        httpGetAsyncTask.execute(diviceID, InstalledApps);

    }
}

In toast message I can show installed app list ( Log.d("install232222222 ", sb.toString());) but I can not send this to server.

If I have change connectWithHttpGet(android_id, sb.toString()); to connectWithHttpGet(android_id, "Hello"); then I can send android_id and hello on server but my problem is sb.toString()

If anyone knows solution my problem please help me

thank you

dinesh sharma
  • 3,312
  • 1
  • 22
  • 32

2 Answers2

0

Why you are using

 sb.append(AppName + "\"");  

Check without that line

Add this line

  AppName.replace("\n", "");
Ameer
  • 2,709
  • 1
  • 28
  • 44
0

Http Get request has a maxlength restriction, is your request exceeding that ?

link

Community
  • 1
  • 1
gvmani
  • 1,580
  • 1
  • 12
  • 20