I stuck in middle of this situation,Please help me out.
The Problem is: My question is that I want to send files (multiple)
to android app using web service. When i run the bellow code it's not
showing output on emulator and Mobile ,instead of it's showing the
blank page .I have installed PDF Reader also .Can any one suggest me
how to solve my issue .Please help me to complete my task .
I tried it with below code.Main Class from which web service is
created
MultipleFilesImpl :
I tried it with below code.Main Class from which web service is
created
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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/test.pdf");
// 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;
}
}
2nd class....
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.Remote;
public interface MultipleFiles extends Remote {
public FileData[] sendPDFs() throws FileNotFoundException,
IOException,
Exception;
}
3rd class:
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;
}
}
new class name:
public class Base64Coder {
public static byte[] decode(String fileData) {
// TODO Auto-generated method stub
return null;
}
public static char[] encode(byte[] data) {
// TODO Auto-generated method stub
return null;
}
}
after that i wrote client side code like this :
public class PDFActivity extends Activity {
private final String METHOD_NAME = "sendPDFs";
private final String NAMESPACE = "http://tbt.com/";
private final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
private final String URL = "http://192.168.1.4:8086/Dr.Mobi_Server/
services/MultipleFilesImpl";
// 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.pdf);
TextView textViewOne = (TextView)
findViewById(R.id.textViewOne);
FileOutputStream outputStream = null;
try {
SoapObject soapObject = new SoapObject(NAMESPACE,
METHOD_NAME);
SoapSerializationEnvelope envelope = new
SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.setOutputSoapObject(soapObject);
HttpTransportSE androidHttpTransport = new
HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject result = (SoapObject) envelope.bodyIn;
for (int i = 0; i < result.getPropertyCount(); i++) {
SoapObject object = (SoapObject)
result.getProperty(i);
Log.i("File Data",
object.getProperty("fileData").toString());
Log.i("File Name",
object.getProperty("fileName").toString());
File pdfFiles = new
File(object.getProperty("fileName").toString());
outputStream = openFileOutput(pdfFiles.toString(),
MODE_PRIVATE);
outputStream.write(Base64Coder.decode(object.getProperty(
"fileData").toString()));
}
outputStream.flush();
outputStream.close();
Log.d("File Creation Message", "Files Created
Succesfully");
} catch (SoapFault fault) {
fault.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream = null;
}
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en