我有一个Web服务(.svc),我试图使用StackOverflow上其他地方找到的代码捕获SOAP请求.
问题是HttpContext.Currentnull,所以我无法访问Request.InputString.
为什么这是空的,怎么解决?
XmlDocument xmlSoapRequest = new XmlDocument();
Stream receiveStream = HttpContext.Current.Request.InputStream;
receiveStream.Position = 0;
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
xmlSoapRequest.Load(readStream);
}
Run Code Online (Sandbox Code Playgroud) 我有一个TCP套接字连接,在Android 2.3上运行良好但现在面临Android 4.1上的一些问题.问题是InputStream.read()方法总是返回-1(没有阻塞),就像关闭连接一样.
创建套接字:
SocketFactory socketFactory = SocketFactory.getDefault();
Socket socket = socketFactory.createSocket("c.whatsapp.net", 5222);
socket.setSoTimeout(3*60*1000);
socket.setTcpNoDelay(true);
Run Code Online (Sandbox Code Playgroud)
检索输入和输出流并编写一些初始数据:
InputStream inputStream = new BufferedInputStream(socket.getInputStream());
OutputStream outputStream = new BufferedOutputStream(socket.getOutputStream());
outputStream.write(87);
outputStream.write(65);
outputStream.write(1);
outputStream.write(2);
outputStream.flush();
Run Code Online (Sandbox Code Playgroud)
然后,这个条件总是不受阻塞地通过:
int c = inputStream.read();
if (c < 0) {
Log.d(TAG, "End of stream");
}
Run Code Online (Sandbox Code Playgroud)
此代码在后台线程中运行.它正在开发姜饼.
试图使用InputStreamReader和OutputStreamWriter而不是直接流 - 没有效果.
我正在尝试从套接字读取项目,我注意到如果套接字流上没有任何内容,它将保留在读取并备份我的应用程序.我想知道是否有一种方法可以设置读取超时或在套接字中没有任何时间后终止连接.
例如,我有以下代码
Source.fromFile(new File( path), "UTF-8").getLines()
Run Code Online (Sandbox Code Playgroud)
它抛出异常
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:319)
Run Code Online (Sandbox Code Playgroud)
我不在乎是否读取了一些行,但是如何跳过无效的字符并继续读取行?
如何使用Dropbox API将文件(图形,音频和视频文件)上传到Dropbox?我按照Dropbox SDK Android页面上的教程进行操作,可以使样本生效.但是现在我想要上传一个实际的File对象而不是String,而是在苦苦挣扎.
示例代码没有任何问题,看起来像这样:
String fileContents = "Hello World!";
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContents.getBytes());
try {
Entry newEntry = mDBApi.putFile("/testing_123456.txt", inputStream, fileContents.length(), null, null);
} catch (DropboxUnlinkedException e) {
Log.e("DbExampleLog", "User has unlinked.");
} catch (DropboxException e) {
Log.e("DbExampleLog", "Something went wrong while uploading.");
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试更改它并使用以下代码上传实际文件时:
File tmpFile = new File(fullPath, "IMG_2012-03-12_10-22-09_thumb.jpg");
// convert File to byte[]
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(tmpFile);
bos.close();
oos.close();
byte[] bytes = bos.toByteArray();
ByteArrayInputStream inputStream = …Run Code Online (Sandbox Code Playgroud) 我有一个我想要写入HttpServletResponse的InputStream.这种方法由于使用byte []而花费的时间太长
InputStream is = getInputStream();
int contentLength = getContentLength();
byte[] data = new byte[contentLength];
is.read(data);
//response here is the HttpServletResponse object
response.setContentLength(contentLength);
response.write(data);
Run Code Online (Sandbox Code Playgroud)
在速度和效率方面,我想知道什么是最好的方法.
我目前正在开发一个Android应用程序,它从提供JSON数据的API获取数据.我将JSON数据的项目存储为字符串,导致出现一些奇怪的字符(例如'Â').我理解这与字符集有关,因此我将InputStreamReader设置为"UTF-8",但它似乎没有解决问题.
URL hukd = new URL(requestUrl);
URLConnection tc = hukd.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(tc.getInputStream(), "UTF-8"));
String line = in.readLine();
Log.d("line :", line);
JSONObject obj = new JSONObject(line);
JSONArray ja = obj.getJSONObject("deals").getJSONArray("items");
for (int i = 0; i < ja.length(); i++) { // each deal
JSONObject jo = (JSONObject) ja.get(i);
// make the deal
Deal d = new Deal(jo.getString("title"),
jo.getString("description"),
jo.getString("price"),jo.getString("temperature"),
jo.getJSONObject("merchant").getString("name"),
jo.getString("deal_image"),
jo.getString("deal_image_highres"));
listItems.add(d);
Log.d("Deal:", d.toString());
}
Run Code Online (Sandbox Code Playgroud)
Log.d"line"给出
01-30 19:56:01.909: D/line :(610): {"deals":{"items":[{"title":"Absolute steal ** Harmony one remote …Run Code Online (Sandbox Code Playgroud) 如何读取Android应用程序中的文本文件:
"1.something written
2.in this file
3.is to be read by
4.the InputStream
..."
Run Code Online (Sandbox Code Playgroud)
所以我可以返回一个字符串,如:
"something written\nin this file\nis to be read by\nthe InputStream"
Run Code Online (Sandbox Code Playgroud)
我想到的是(伪代码):
make an inputstream
is = getAssest().open("textfile.txt"); //in try and catch
for loop{
string = is.read() and if it equals "." (i.e. from 1., 2., 3. etc) add "/n" ...
}
Run Code Online (Sandbox Code Playgroud) 我一直在阅读InputStream,FileInputStream,ByteArrayInputStream以及它们的使用方式似乎非常清楚(输出流也是如此).
我正在努力的是理解FilterInputStream和FilterOutputStream的用法:
inputstream ×10
java ×7
android ×4
outputstream ×2
sockets ×2
android-file ×1
dropbox ×1
dropbox-api ×1
httpcontext ×1
java-ee ×1
json ×1
scala ×1
servlets ×1
stream ×1
streamreader ×1
timeout ×1
utf-8 ×1
wcf ×1
web-services ×1
zip ×1