jeudi 13 novembre 2014

Data not being sent to servlet from Android via POST


Vote count:

0




I have an android application from where I need to send an image file to the servlet using POST multipart/form-data. The issue is that the servlet is being called and the response is being received on android phone. But no data is getting received on the server side. Not only the image, but other data such as 'Title' and 'Description' as well.


Can someone please tell me where the issue is?


Android code:



public String Sending(){
String iFileName = "card.png";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag="fSnd";
String s = null;
try
{
Log.e(Tag,"Starting Http File Sending to URL");

// Open a HTTP connection to the URL
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();

// Allow Inputs
conn.setDoInput(true);

// Allow Outputs
conn.setDoOutput(true);

// Don't use a cached copy.
conn.setUseCaches(false);

// Use a post method.
conn.setRequestMethod("POST");

conn.setRequestProperty("Connection", "Keep-Alive");

conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"title\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(Title);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);

dos.writeBytes("Content-Disposition: form-data; name=\"description\""+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(Description);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + lineEnd);

dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + iFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);

Log.e(Tag,"Headers are written");

// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();

int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[ ] buffer = new byte[bufferSize];

// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable,maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0,bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// close streams
fileInputStream.close();

dos.flush();

Log.e(Tag,"File Sent, Response: "+String.valueOf(conn.getResponseCode()));

InputStream is = conn.getInputStream();

// retrieve the response from server
int ch;

StringBuffer b =new StringBuffer();

while( ( ch = is.read() ) != -1 ) {
b.append( (char)ch );
}

s=b.toString();
Log.i("Response",s);
dos.close();

}

catch (MalformedURLException ex)
{
Log.e(Tag, "URL error: " + ex.getMessage(), ex);
}

catch (IOException ioe)
{
Log.e(Tag, "IO error: " + ioe.getMessage(), ioe);
}
return s;
}


Servlet code:



@WebServlet("/PreprocessingPath")
public class Preprocessing extends HttpServlet {
private static final long serialVersionUID = 1L;


public Preprocessing() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}

/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String method = request.getMethod();

doPost(request, response);
}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Hi from Post");

// get current date time with Date()
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
String timestamp = dateFormat.format(date);

// set the filename
String filename = "IMAGE_" + timestamp +".png";

String title = request.getParameter("title");

String description = request.getParameter("description");

System.out.println("Title: "+ title);
System.out.println("Description: "+ description);

PrintWriter writer = response.getWriter();
writer.println("Server says hello !");

byte[] byteArray = request.getParameter("filename").getBytes();

// save the image file on the server
FileOutputStream fos = new FileOutputStream("C:/Users/NAPSTER/ADT/ImagePreprocessing/WebContent/WEB-INF/"+filename);
try {
fos.write(byteArray);
}
finally {
fos.close();
}

PreProcessingTest.startPreprocessing(filename);
}


}


Console:



Hi from Post
Title: null
Description: null
Nov 14, 2014 1:06:15 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [com.patternrecognition.preprocessing.Preprocessing] in context with path [/ImagePreprocessing] threw exception
java.lang.NullPointerException
at com.patternrecognition.preprocessing.Preprocessing.doPost(Preprocessing.java:84)
at com.patternrecognition.preprocessing.Preprocessing.service(Preprocessing.java:56)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)


asked 46 secs ago







Data not being sent to servlet from Android via POST

Aucun commentaire:

Enregistrer un commentaire