我目前正在使用InpuStream从我的服务器获取JSON响应.
我需要做两件事:
在逐个使用这两种方法时,这完全没有问题.
使用GSON进行解析:
Gson gson = new Gson();
Reader reader = new InputStreamReader (myInputStream);
Result result = gson.FrmJson(reader, Result.class)
Run Code Online (Sandbox Code Playgroud)
并将复制到SDCard
FileOutputStream f (...) f.write (buffer)
Run Code Online (Sandbox Code Playgroud)
它们都经过了测试.
问题是一旦解析完成,我想写入SDCard并且它会中断.我知道我的InputStream已关闭,这就是问题所在.
这里有一些接近我的问题:如何缓存InputStream以供多次使用
有没有办法改进该解决方案并提供我们可以使用的东西?
我想要一个通用的,可重用的getPosition()方法,它会告诉我从流的起始点读取的字节数.理想情况下,我更喜欢使用它来处理所有的InputStream,因此当我从不同的源获取它们时,我不必包装它们中的每一个.
这样的野兽存在吗?如果没有,任何人都可以推荐现有的计数实施InputStream吗?
所以我的资源文件夹中有一个小的音频文件,我想打开一个InputStream写入缓冲区,然后写入临时文件,然后打开MediaPlayer播放该临时文件.问题是,当媒体播放器点击mp.Prepare()时,它不播放并且永远不会到达祝词.有没有人曾经这样做过?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
InputStream str;
try {
str = this.getAssets().open("onestop.mid");
Toast.makeText(this, "Successful Input Stream Opened.", Toast.LENGTH_SHORT).show();
takeInputStream(str);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}//end on create
public void takeInputStream(InputStream stream) throws IOException
{
//fileBeingBuffered = (FileInputStream) stream;
//Toast.makeText(this, "sucessful stream conversion.", Toast.LENGTH_SHORT).show();
try
{
convertedFile = File.createTempFile("convertedFile", ".dat", getDir("filez", 0));
Toast.makeText(this, "Successful file and folder creation.", Toast.LENGTH_SHORT).show();
out = new FileOutputStream(convertedFile);
Toast.makeText(this, "Success out set as output stream.", Toast.LENGTH_SHORT).show(); …Run Code Online (Sandbox Code Playgroud) 对于类似以下的代码:
InputStream is = new FileInputstream("test.xml");
Document doc = DocumentBuilder.parser(is);
Run Code Online (Sandbox Code Playgroud)
我的问题是我是否需要手动关闭流(调用is.close()).DocumentBuilder是否为我关闭了InputStream?
我正在寻找机会在java中打开一个网址.
URL url = new URL("http://maps.google.at/maps?saddr=4714&daddr=Marchtrenk&hl=de");
InputStream is = url.openConnection().getInputStream();
BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );
String line = null;
while( ( line = reader.readLine() ) != null ) {
System.out.println(line);
}
reader.close();
Run Code Online (Sandbox Code Playgroud)
我找到了那种方式.
我在程序中添加了它,发生了以下错误.
The method openConnection() is undefined for the type URL
Run Code Online (Sandbox Code Playgroud)
(通过url.openConnection())
我的问题是什么?
我使用带有servlet的tomcat-server,...
我开发了一个j2me应用程序,通过套接字连接到我的虚拟主机服务器.我使用自己的扩展lineReader类从服务器读取响应,该类扩展了基本的InputStreamReader.如果服务器发送5行回复,则读取服务器的语法逐行回复:
line=input.readLine();
line = line + "\n" + input.readLine();
line = line + "\n" + input.readLine();
line = line + "\n" + input.readLine();
line = line + "\n" + input.readLine();
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我可以编写这种语法,因为我知道有一定数量的回复.但是如果我不知道行数,并且想要一次读取整个inputStream,我应该如何修改当前readLine()函数.这是函数的代码:
public String readLine() throws IOException {
StringBuffer sb = new StringBuffer();
int c;
while ((c = read()) > 0 && c != '\n' && c != '\r' && c != -1) {
sb.append((char)c);
}
//By now, buf is empty.
if (c == '\r') {
//Dos, or Mac …Run Code Online (Sandbox Code Playgroud) 我试图在String中获取HttpServletRequest的主体.这样做的最佳方式是什么?
所以我应该使用Java将EBCDIC文件转换为ASCII.到目前为止,我有这个代码:
public class Migration {
InputStreamReader reader;
StringBuilder builder;
public Migration(){
try {
reader = new InputStreamReader(new FileInputStream("C:\\TI3\\Legacy Systemen\\Week 3\\Oefening 3\\inputfile.dat"),
java.nio.charset.Charset.forName("ibm500") );
} catch(FileNotFoundException e){
e.printStackTrace();
}
builder = new StringBuilder();
}
public void read() throws IOException {
int theInt;
while((theInt = reader.read()) != -1){
char theChar = (char) theInt;
builder.append(theChar);
}
reader.close();
}
@Override
public String toString(){
return builder.toString();
}
}
Run Code Online (Sandbox Code Playgroud)
文件描述如下:
02 KDGEX.
05 B1-LENGTH PIC S9(04) USAGE IS COMP.
05 B1-CODE PIC S9(04) USAGE IS COMP. …Run Code Online (Sandbox Code Playgroud) 我基本上试图在Android设备上托管服务器.客户端设备通过TCP连接到服务器并发送请求.服务器执行客户端请求的操作,然后将数据写回套接字.连接未被服务器终止,并且将通过套接字连续读取请求并回复.
注意:每个请求消息的前4个字节包含实际消息/请求的长度.
parseXmlInputAndExecuteCmd函数根据输入XML字符串的内容执行各种异步操作.这最终导致'allowResponse'变量的布尔值更改为true,并生成某个响应,该响应存储在名为'response'的String类型的变量中.一旦布尔值'allowResponse'变为true,线程就会恢复执行并将响应写回套接字的OutputStream
其中一些异步操作包括连接和断开与公司VPN的连接.这可能是错误的原因吗?
使用的一些类级变量是:
private volatile boolean allowResponse = false;
private String response;
Run Code Online (Sandbox Code Playgroud)
服务器代码:
private void startServer() {
try {
ServerSocket serverSocket = new ServerSocket(8080);
while (true) {
Socket connectionSocket = serverSocket.accept();
BufferedInputStream bufInpStream = new BufferedInputStream(connectionSocket.getInputStream());
BufferedOutputStream bufOutStream = new BufferedOutputStream(connectionSocket.getOutputStream());
ByteArrayOutputStream contentLengthBaos = new ByteArrayOutputStream();
int c;
int count = 0;
while ((c = bufInpStream.read()) != -1) {
contentLengthBaos.write(c);
count++;
if (count == 4) {
int contLen = getMessageLength(contentLengthBaos);
String content = getMessageContent(bufInpStream, contLen);
parseXmlInputAndExecuteCmd(content);
count = …Run Code Online (Sandbox Code Playgroud)