通过 Web 服务将文件(至少 11 个文件)从文件夹发送到 Android 应用程序
我陷入了这种境地,请帮助我。
我的问题是我想使用网络服务将文件(总共 11 个 PDF 文件)发送到 Android 应用程序。
我用下面的代码尝试了它。创建 Web 服务的主类
public class MultipleFilesImpl implements MultipleFiles {
public FileData[] sendPDFs() {
FileData fileData = null;
// List<FileData> filesDetails = new ArrayList<FileData>();
File fileFolder = new File(
"C:/eclipse/workspace/AIPWebService/src/pdfs/");
// File fileTwo = new File(
// "C:/eclipse/workspace/AIPWebService/src/simple.pdf");
File sendFiles[] = fileFolder.listFiles();
// sendFiles[0] = fileOne;
// sendFiles[1] = fileTwo;
DataHandler handler = null;
char[] readLine = null;
byte[] data = null;
int offset = 0;
int numRead = 0;
InputStream stream = null;
FileOutputStream outputStream = null;
FileData[] filesData = null;
try {
System.out.println("Web Service Called Successfully");
for (int i = 0; i < sendFiles.length; i++) {
handler = new DataHandler(new FileDataSource(sendFiles[i]));
fileData = new FileData();
data = new byte[(int) sendFiles[i].length()];
stream = handler.getInputStream();
while (offset < data.length
&& (numRead = stream.read(data, offset, data.length
- offset)) >= 0) {
offset += numRead;
}
readLine = Base64Coder.encode(data);
offset = 0;
numRead = 0;
System.out.println("'Reading File............................");
System.out.println("\n");
System.out.println(readLine);
System.out.println("Data Reading Successful");
fileData.setFileName(sendFiles[i].getName());
fileData.setFileData(String.valueOf(readLine));
readLine = null;
System.out.println("Data from bean " + fileData.getFileData());
outputStream = new FileOutputStream("D:/"
+ sendFiles[i].getName());
outputStream.write(Base64Coder.decode(fileData.getFileData()));
outputStream.flush();
outputStream.close();
stream.close();
// FileData fileDetails = new FileData();
// fileDetails = fileData;
// filesDetails.add(fileData);
filesData = new FileData[(int) sendFiles[i].length()];
}
// return fileData;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return filesData;
}
}
还有接口 MultipleFiles:-
public interface MultipleFiles extends Remote {
public FileData[] sendPDFs() throws FileNotFoundException, IOException,
Exception;
}
这里我发送一个bean“文件数据”数组,具有属性即。文件数据与文件名。 FileData- 包含编码的文件数据。 FileName-编码的文件名。
The Bean:- (FileData)
public class FileData {
private String fileName;
private String fileData;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileData() {
return fileData;
}
public void setFileData(String string) {
this.fileData = string;
}
}
当尝试下面的代码时,android DDMS 会给出内存不足的异常:当我尝试发送两个文件时,仅创建第一个文件。
public class PDFActivity extends Activity {
private final String METHOD_NAME = "sendPDFs";
private final String NAMESPACE = "http://webservice.uks.com/";
private final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private final String URL = "http://192.168.1.123:8080/AIPWebService/services/MultipleFilesImpl";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textViewOne = (TextView) findViewById(R.id.textViewOne);
try {
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(soapObject);
textViewOne.setText("Web Service Started");
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
httpTransport.call(SOAP_ACTION, envelope);
// SoapObject result = (SoapObject) envelope.getResponse();
Object result = envelope.getResponse();
Log.i("Result", result.toString());
// String fileName = result.getProperty("fileName").toString();
// String fileData = result.getProperty("fileData").toString();
// Log.i("File Name", fileName);
// Log.i("File Data", fileData);
// File pdfFile = new File(fileName);
// FileOutputStream outputStream =
// openFileOutput(pdfFile.toString(),
// MODE_PRIVATE);
// outputStream.write(Base64Coder.decode(fileData));
Log.i("File", "File Created");
// textViewTwo.setText(result);
// Object result = envelope.getResponse();
// FileOutputStream outputStream = openFileOutput(name, mode)
} catch (Exception e) {
e.printStackTrace();
}
}
}
请帮助解释或更改我的代码。 提前致谢。
I stuck in middle of this situation,Please help me out.
My question is that I want to send files (Total 11 PDF Files) to android app using web service.
I tried it with below code.Main Class from which web service is created
public class MultipleFilesImpl implements MultipleFiles {
public FileData[] sendPDFs() {
FileData fileData = null;
// List<FileData> filesDetails = new ArrayList<FileData>();
File fileFolder = new File(
"C:/eclipse/workspace/AIPWebService/src/pdfs/");
// File fileTwo = new File(
// "C:/eclipse/workspace/AIPWebService/src/simple.pdf");
File sendFiles[] = fileFolder.listFiles();
// sendFiles[0] = fileOne;
// sendFiles[1] = fileTwo;
DataHandler handler = null;
char[] readLine = null;
byte[] data = null;
int offset = 0;
int numRead = 0;
InputStream stream = null;
FileOutputStream outputStream = null;
FileData[] filesData = null;
try {
System.out.println("Web Service Called Successfully");
for (int i = 0; i < sendFiles.length; i++) {
handler = new DataHandler(new FileDataSource(sendFiles[i]));
fileData = new FileData();
data = new byte[(int) sendFiles[i].length()];
stream = handler.getInputStream();
while (offset < data.length
&& (numRead = stream.read(data, offset, data.length
- offset)) >= 0) {
offset += numRead;
}
readLine = Base64Coder.encode(data);
offset = 0;
numRead = 0;
System.out.println("'Reading File............................");
System.out.println("\n");
System.out.println(readLine);
System.out.println("Data Reading Successful");
fileData.setFileName(sendFiles[i].getName());
fileData.setFileData(String.valueOf(readLine));
readLine = null;
System.out.println("Data from bean " + fileData.getFileData());
outputStream = new FileOutputStream("D:/"
+ sendFiles[i].getName());
outputStream.write(Base64Coder.decode(fileData.getFileData()));
outputStream.flush();
outputStream.close();
stream.close();
// FileData fileDetails = new FileData();
// fileDetails = fileData;
// filesDetails.add(fileData);
filesData = new FileData[(int) sendFiles[i].length()];
}
// return fileData;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return filesData;
}
}
Also The Interface MultipleFiles:-
public interface MultipleFiles extends Remote {
public FileData[] sendPDFs() throws FileNotFoundException, IOException,
Exception;
}
Here I am sending an array of bean "File Data",having properties viz. FileData & FileName.
FileData- contains file data in encoded.
FileName- encoded file name.
The Bean:- (FileData)
public class FileData {
private String fileName;
private String fileData;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileData() {
return fileData;
}
public void setFileData(String string) {
this.fileData = string;
}
}
The android DDMS gives out of memory exception when tried below code & when i tried to send two files then only first file is created.
public class PDFActivity extends Activity {
private final String METHOD_NAME = "sendPDFs";
private final String NAMESPACE = "http://webservice.uks.com/";
private final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private final String URL = "http://192.168.1.123:8080/AIPWebService/services/MultipleFilesImpl";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView textViewOne = (TextView) findViewById(R.id.textViewOne);
try {
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(soapObject);
textViewOne.setText("Web Service Started");
AndroidHttpTransport httpTransport = new AndroidHttpTransport(URL);
httpTransport.call(SOAP_ACTION, envelope);
// SoapObject result = (SoapObject) envelope.getResponse();
Object result = envelope.getResponse();
Log.i("Result", result.toString());
// String fileName = result.getProperty("fileName").toString();
// String fileData = result.getProperty("fileData").toString();
// Log.i("File Name", fileName);
// Log.i("File Data", fileData);
// File pdfFile = new File(fileName);
// FileOutputStream outputStream =
// openFileOutput(pdfFile.toString(),
// MODE_PRIVATE);
// outputStream.write(Base64Coder.decode(fileData));
Log.i("File", "File Created");
// textViewTwo.setText(result);
// Object result = envelope.getResponse();
// FileOutputStream outputStream = openFileOutput(name, mode)
} catch (Exception e) {
e.printStackTrace();
}
}
}
Please help with some explanation or changes in my code.
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己得到了这个问题的答案。
我使用 kSoap2 api 2.4 版本面临的主要问题是我无法获取复杂的对象(在我的例子中是 bean 数组)
然后我将我的代码从这个(使用版本 2.4)更改
为这个代码(使用版本 2.5) .1):-
因此我得到了解决方案。
主要帮助是通过此问题的第 24 期:- 如果您遇到此类问题,请阅读该内容。链接问题-24
Myself got the solution of this question.
The major problem I faced using kSoap2 api 2.4 version is that I can't able to get complex object (array of bean in my case)
Then I changed my code from this (using ver 2.4):-
to this code (using ver 2.5.1):-
Hence I got solution.
The main help is through the issue-24 of this :- Please read that if u are facing such problem.Link-Issue-24