大家好我想向服务器发送两个参数
我写了一个代码来了解已安装的应用程序并在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()
如果有人知道解决我的问题请帮帮我
谢谢
答案 0 :(得分:0)
为什么使用
sb.append(AppName + "\"");
不带该行检查
添加此行
AppName.replace("\n", "");
答案 1 :(得分:0)
Http Get请求有一个maxlength限制,你的请求超过了吗?