Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android HttpUrlConnection如何将响应读入片段_Java_Android_Response_Httpurlconnection - Fatal编程技术网

Java Android HttpUrlConnection如何将响应读入片段

Java Android HttpUrlConnection如何将响应读入片段,java,android,response,httpurlconnection,Java,Android,Response,Httpurlconnection,实际上,我需要更多关于如何从Android SDK中的HttpUrlConnection类读取响应的信息。我试图从web服务器读取响应,但当响应太大时,我的应用程序会抛出一个OutOfMemoryException。因此,欢迎任何关于如何将整个响应分解成片段的来源/帮助/建议 当我做一项研究时,我发现我应该这样设置:((HttpURLConnection)connection)但我的问题是我不知道如何读取这个压缩流。所以如果有人能引导我走上正确的道路,我会非常高兴 谢谢 示例代码: Teleph

实际上,我需要更多关于如何从Android SDK中的
HttpUrlConnection
类读取响应的信息。我试图从web服务器读取响应,但当响应太大时,我的应用程序会抛出一个
OutOfMemoryException
。因此,欢迎任何关于如何将整个响应分解成片段的来源/帮助/建议

当我做一项研究时,我发现我应该这样设置:
((HttpURLConnection)connection)但我的问题是我不知道如何读取这个压缩流。所以如果有人能引导我走上正确的道路,我会非常高兴

谢谢

示例代码:

TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            String deviceId = tm.getDeviceId();
            Log.w("device_identificator","device_identificator : "+deviceId);
            String resolution = Integer.toString(getWindow().getWindowManager().getDefaultDisplay().getWidth())+ "x" +
                                         Integer.toString(getWindow().getWindowManager().getDefaultDisplay().getHeight());
            Log.w("device_resolution","device_resolution : "+resolution);
            String version = "Android " + Build.VERSION.RELEASE;
            Log.w("device_os_type","device_os_type : "+version);
            Log.w("device_identification_string","device_identification_string : "+version);
            String locale = getResources().getConfiguration().locale.toString();
            Log.w("set_locale","set_locale : "+locale);
            String clientApiVersion = null;

            PackageManager pm = this.getPackageManager();
            PackageInfo packageInfo;

            packageInfo = pm.getPackageInfo(this.getPackageName(), 0);

            clientApiVersion = packageInfo.versionName;
            Log.w("client_api_ver","client_api_ver : "+clientApiVersion);

            long timestamp = System.currentTimeMillis()/1000;
            String timeStamp = Long.toString(timestamp);


            String url = "http://www.rpc.shutdown.com";
            String charset = "UTF-8";
            String usernameHash = hashUser(username,password);
            String passwordHash = hashPass(username,password);


            String query = String.format("username_hash=%s&password_hash=%s&new_auth_data=%s&debug_data=%s&client_api_ver=%s&set_locale=%s&timestamp=%s&"+
                     "device_os_type=%s&mobile_imei=%s&device_sync_type=%s&device_identification_string=%s&device_identificator=%s&device_resolution=%s", 
                     URLEncoder.encode(usernameHash, charset), 
                     URLEncoder.encode(passwordHash, charset),
                     URLEncoder.encode("1", charset),
                     URLEncoder.encode("1", charset),
                     URLEncoder.encode(clientApiVersion, charset),
                     URLEncoder.encode(locale, charset),
                     URLEncoder.encode(timeStamp, charset),
                     URLEncoder.encode(version, charset),
                     URLEncoder.encode(deviceId, charset),
                     URLEncoder.encode("14", charset),
                     URLEncoder.encode(version, charset),
                     URLEncoder.encode(deviceId, charset),
                     URLEncoder.encode(resolution, charset));

            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setDoOutput(true); // Triggers POST.
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Charset", charset);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);
            OutputStream output = null;
            try {
                 output = connection.getOutputStream();
                 output.write(query.getBytes(charset));
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                 if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
            }

            int status = ((HttpURLConnection) connection).getResponseCode();
            Log.i("","Status : "+status);

            for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
                Log.i("Headers","Headers : "+header.getKey() + "=" + header.getValue());
            }

            InputStream response = connection.getInputStream();
            Log.i("","Response : "+response.toString());
            int bytesRead = -1;
            byte[] buffer = new byte[8*1024];
            while ((bytesRead = response.read(buffer)) >= 0) {
              String line = new String(buffer, "UTF-8");
              Log.i("","line : "+line);
              handleDataFromSync(buffer);
            }
TelephonyManager tm=(TelephonyManager)getSystemService(电话服务);
字符串deviceId=tm.getDeviceId();
Log.w(“设备标识器”,“设备标识器:”+deviceId);
字符串分辨率=整数.toString(getWindow().getWindowManager().getDefaultDisplay().getWidth())+“x”+
整型.toString(getWindow().getWindowManager().getDefaultDisplay().getHeight());
Log.w(“设备分辨率”,“设备分辨率:”+分辨率);
String version=“Android”+Build.version.RELEASE;
Log.w(“设备类型”,“设备类型:+版本”);
Log.w(“设备标识字符串”,“设备标识字符串:+版本”);
字符串locale=getResources().getConfiguration().locale.toString();
w(“设置语言环境”,“设置语言环境:“+locale”);
字符串clientapivision=null;
PackageManager pm=this.getPackageManager();
PackageInfo PackageInfo;
packageInfo=pm.getPackageInfo(this.getPackageName(),0);
clientapivision=packageInfo.versionName;
Log.w(“客户端api版本”,“客户端api版本:+ClientAPI版本”);
长时间戳=System.currentTimeMillis()/1000;
字符串时间戳=Long.toString(时间戳);
字符串url=”http://www.rpc.shutdown.com";
字符串charset=“UTF-8”;
字符串usernameHash=hashUser(用户名、密码);
字符串passwordHash=hashPass(用户名、密码);
String query=String.format(“用户名\u散列=%s和密码\u散列=%s&新的\u身份验证\u数据=%s&调试\u数据=%s&客户端\u api\u版本=%s&设置\u区域设置=%s&时间戳=%s&”+
“设备\u操作系统\u类型=%s&移动\u imei=%s&设备\u同步\u类型=%s&设备\u标识\u字符串=%s&设备\u标识器=%s&设备\u分辨率=%s”,
encode(usernameHash,charset),
encode(密码哈希,字符集),
URLEncoder.encode(“1”,字符集),
URLEncoder.encode(“1”,字符集),
URLEncoder.encode(客户端版本,字符集),
URLEncoder.encode(区域设置,字符集),
URLEncoder.encode(时间戳,字符集),
URLEncoder.encode(版本,字符集),
URLEncoder.encode(设备ID,字符集),
URLEncoder.encode(“14”,字符集),
URLEncoder.encode(版本,字符集),
URLEncoder.encode(设备ID,字符集),
encode(分辨率,字符集));
HttpURLConnection连接=(HttpURLConnection)新URL(URL).openConnection();
connection.setDoOutput(true);//触发后。
connection.setRequestMethod(“POST”);
setRequestProperty(“连接”,“保持活动”);
setRequestProperty(“Charset”,Charset);
connection.setRequestProperty(“内容类型”,“应用程序/x-www-form-urlencoded;字符集=“+charset”);
OutputStream输出=null;
试一试{
output=connection.getOutputStream();
output.write(query.getBytes(charset));
}捕获(IOE异常){
e、 printStackTrace();
}最后{
如果(output!=null)尝试{output.close();}catch(IOException logOrIgnore){}
}
int status=((HttpURLConnection)connection.getResponseCode();
Log.i(“,”状态:“+状态);
for(条目标题:connection.getHeaderFields().entrySet()){
Log.i(“Headers”,“Headers:”+header.getKey()+“=”+header.getValue());
}
InputStream响应=connection.getInputStream();
Log.i(“,”Response:“+Response.toString());
int字节读取=-1;
字节[]缓冲区=新字节[8*1024];
而((bytesRead=response.read(buffer))>=0){
字符串行=新字符串(缓冲区,“UTF-8”);
Log.i(“,”行:“+行);
handleDataFromSync(缓冲区);
}

只需分配一个字节缓冲区,该缓冲区可以保存少量数据并从输入流读取到该缓冲区(使用该方法)。比如:

InputStream is = connection.getInputStream();

int bytesRead = -1;
byte[] buffer = new byte[1024];
while ((bytesRead = is.read(buffer)) >= 0) {
  // process the buffer, "bytesRead" have been read, no more, no less
}

要读取响应标头,请尝试使用:

String sHeaderValue = connection.getHeaderField("Your_Header_Key");

你能发布一些示例代码吗。这样可以更容易地解释在您的特定情况下该做什么。我刚刚添加了用于发送参数和连接到服务器并实际显示响应正文的代码。该代码在该行上通过一个
FileNotFoundException
传递给我:
InputStream response=connection.getInputStream()。你知道为什么吗?也许你提供的URL不正确(在浏览器中试试)。此外,粘贴的代码不从URL读取,而是写入URL(将数据发送到服务器,但不接收任何数据)。我的代码从服务器读取响应。是的,实际上我必须发布一些数据,然后根据我发送的数据从服务器读取响应。我刚刚编辑了我的代码,我可以看出我没有收到整个输入流。我只收到8*1024,而不是什么。这