【问题标题】:android send two parameters to serverandroid向服务器发送两个参数
【发布时间】:2014-02-03 11:50:10
【问题描述】:

大家好,我想向服务器发送两个参数

  1. android id
  2. 已安装的应用信息

我编写了一个代码来了解已安装的应用程序并在 logcat 和 android_id 中打印,但是当我尝试在服务器上发送已安装的应用程序信息时出现错误

代码

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

    }
}

在 toast 消息中,我可以显示已安装的应用程序列表 (Log.d("install232222222 ", sb.toString());),但我无法将其发送到服务器。

如果我将connectWithHttpGet(android_id, sb.toString()); 更改为connectWithHttpGet(android_id, "Hello");,那么我可以在服务器上发送android_id 和hello 但我的问题是sb.toString()

如果有人知道解决我的问题,请帮助我

谢谢

【问题讨论】:

    标签: java android web-services http-get


    【解决方案1】:

    Http Get 请求有最大长度限制,你的请求是否超过了这个限制?

    link

    【讨论】:

      【解决方案2】:

      你为什么使用

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

      检查没有那行

      添加这一行

        AppName.replace("\n", "");
      

      【讨论】:

      • 我删除了这一行并且它可以工作,但我如何才能将这个已安装的应用程序发送到一个列表中?
      猜你喜欢
      • 2011-12-07
      • 1970-01-01
      • 2014-12-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多