0

当我通过 android 发送 json 对象时,服务器正在抛出NullPointerException。描述是这样的:设备拍照并将其转换为字节数组Base64.java,然后转换为字符串。然后将该字符串放入JSONObject并发送到服务器。但是服务器正在抛出NullPointerException. 我的服务器应用程序是用vaadin.

以下是android中用于将图像转换为的代码Byte[]

Bitmap bitmap = BitmapFactory.decodeFile(path);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, baos);
**byte[] ba = baos.toByteArray();
String bal = Base64.encodeBytes(ba);**

发送JSONObject到服务器的代码:

HttpClient client = new DefaultHttpClient();
JSONObject jsonObject=new JSONObject();
try {
    jsonObject.putOpt("imageName", imageNm);
    jsonObject.put("imageSend", bal);
} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

serverUrl = serverUrl+"UploadImage" ;
HttpPost post = new HttpPost(serverUrl);
HttpResponse httpResponse = null;
try {           
    StringEntity entity=new StringEntity(jsonObject.toString());
    entity.setContentType("application/json;char-set=UTF-8");
    entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;char-set=UTF-8"));
    post.setEntity(entity);
    httpResponse = client.execute(post);
    response = EntityUtils.toString(httpResponse.getEntity());
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

服务器上的异常

         [#|2012-12-18       T12:49:43.692+0530|INFO|glassfish         v3.0|javax.enterprise.system.std.com.sun.enterprise.v3.services.impl|_ThreadID=40;   _ThreadName=Thread-1;|Image name : null|#]

[#|2012-12-18T12:49:43.692+0530|SEVERE|glassfishv3.0|com.vaadin.Application|_ThreadID=40;   _ThreadName=Thread-1;|Terminal error:
 java.lang.NullPointerException
         at java.io.StringReader.<init>(StringReader.java:50)
       at org.json.JSONTokener.<init>(JSONTokener.java:83)
       at org.json.JSONObject.<init>(JSONObject.java:310)
        at handlers.SPTURIHandler.handleURI(SPTURIHandler.java:91)
         at com.vaadin.ui.Window.handleURI(Window.java:424)
         at        com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleURI(AbstractCommunicationManager.java:2274)
         at com.vaadin.terminal.gwt.server.CommunicationManager.handleURI(CommunicationManager.java:348)
    at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.handleURI(AbstractApplicationServlet.java:1093)
    at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:535)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
    at java.lang.Thread.run(Thread.java:679)

|#]

是不是代码有问题...

4

0 回答 0