【发布时间】:2011-10-19 07:44:05
【问题描述】:
我需要一些帮助来从我的 android 应用程序发送 HttpUrlConnection。到现在为止,我都是用基本的Http Client 来做这件事的。但问题是,当我从服务器接收到一个大流时,我的应用程序会因outofmemory 异常而崩溃。这就是为什么我进行了一项研究并发现HttpUrlConnection 让我可以将流分成几部分。那么任何人都可以帮助我发送我的参数并从服务器获取响应吗?
我之前使用的代码是这样的:
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://www.rpc.your_nightmare.com");
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
String deviceId = tm.getDeviceId();
String resolution = Integer.toString(getWindow().getWindowManager().getDefaultDisplay().getWidth())+ "x" +
Integer.toString(getWindow().getWindowManager().getDefaultDisplay().getHeight());
String version = "Android " + Build.VERSION.RELEASE;
String locale = getResources().getConfiguration().locale.toString();
String clientApiVersion = null;
PackageManager pm = this.getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(this.getPackageName(), 0);
clientApiVersion = packageInfo.versionName;
hash = getAuthHash();
String timestampSQL = "SELECT dbTimestamp FROM users";
Cursor cursor = systemDbHelper.executeSQLQuery(timestampSQL);
if(cursor.getCount()==0){
Log.i("Cursor","TimeStamp Cursor Empty!");
} else if(cursor.getCount()>0){
cursor.moveToFirst();
timeStamp = cursor.getString(cursor.getColumnIndex("dbTimestamp"));
}
TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
phoneNumber = tMgr.getLine1Number();
Log.i("Phone","Phone Number : "+phoneNumber);
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("debug_data","1"));
postParameters.add(new BasicNameValuePair("client_auth_hash", hash));
postParameters.add(new BasicNameValuePair("timestamp", timeStamp));
postParameters.add(new BasicNameValuePair("mobile_phone", phoneNumber));
postParameters.add(new BasicNameValuePair("deactivate_collections",Integer.toString(index)));
postParameters.add(new BasicNameValuePair("client_api_ver", clientApiVersion));
postParameters.add(new BasicNameValuePair("set_locale", locale));
postParameters.add(new BasicNameValuePair("device_os_type", version));
postParameters.add(new BasicNameValuePair("device_sync_type", "14"));
postParameters.add(new BasicNameValuePair("device_identification_string", version));
postParameters.add(new BasicNameValuePair("device_identificator", deviceId));
postParameters.add(new BasicNameValuePair("device_resolution", resolution));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);
Log.w("Response ","Status line : "+ response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
InputStream stream2 = entity.getContent();
int nRead;
byte[] data = new byte[8*1024];
while ((nRead = stream2.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
而不是像这样处理它:
InputStream stream = new ByteArrayInputStream(buffer, 0, temp.length);
Log.i("Temp","Temp : "+temp.length);
Log.i("index","index : "+index);
responseBody = convertStreamToString(stream);
Log.i("responseBody","responseBody : "+responseBody);
//calculations
【问题讨论】:
-
我看到你仍然面临同样的问题,所以我认为你错过了我们之前提供的一些东西,或者错误地使用它。那么您能否提供完整的代码,以便我们进行检查。你在尝试什么。
-
问题是响应太大,我无法将其转换为字符串并解析它。我需要找到一种方法将其分解,正如我在 android 文档中看到的那样,httpurlconnection 是一种更好的方法,但我很难理解如何发布同步参数等。
-
多大的反应?有很多人正在使用与我们建议的服务器响应相同的技术。
-
其实每次都不一样...有时我只能得到1-2MB,在其他情况下我可以得到100MB,这取决于用户想要什么。我可以告诉你如果你愿意,我的整个代码
-
这里是代码如果你想看看:pastebin.com/1DLReRbm
标签: android httpclient httpurlconnection