2017-03-13 131 views
0

我想从我的android应用程序发送一个字符串到服务器。该字符串包含:用户名,密码和以64为基础编码的图像。它们之间是空格(“”)。我正在使用heroku来存储服务器,并使用postgreSQL数据库。我有一个名为users的表格,列表为:userid,password, encoded image,它们都是格式文本。从Android发送一个字符串到一个Spring服务器

当我创建我给userid and the password一个新的用户,列encodedimage是空的。我想在将图像上传到服务器并编辑encodedimage列时更新表格。

这里是如何从我的android发送字符串:

request=Utils.name+" "+Utils.password; 
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
      image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); 
      request = request+ " " + Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT); 
try { 
     URL url = new URL(params[0]); 
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setReadTimeout(15000); 
     urlConnection.setConnectTimeout(15000); 
     urlConnection.setRequestMethod("POST"); 
     urlConnection.setDoOutput(true); 
     urlConnection.setChunkedStreamingMode(0); 

     OutputStream outputStream = urlConnection.getOutputStream(); 
     OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8"); 
     BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter); 
     bufferedWriter.write(request); 

     bufferedWriter.flush(); 
     bufferedWriter.close(); 
     outputStreamWriter.close(); 
     outputStream.close(); 

     int response=urlConnection.getResponseCode(); 
     urlConnection.disconnect(); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

如果我从cmd更新我的桌子它的工作原理,但是从码事实并非如此。

这里是我的控制器:

@Controller 
public class UploadController implements Constant { 
    @RequestMapping(value = "/upload-image", method = RequestMethod.POST) 
    public void handleUploadImageRequest(@RequestBody String request) { 
     String[] details = request.split(" "); 
     String name = details[0]; 
     String password = details[1]; 
     byte[] decodedImage = Base64.getDecoder().decode(details[2]); 
     if (decodedImage.length > 0) { 
      try { 
       Image image = ImageIO.read(new ByteArrayInputStream(decodedImage)); 
       Connection connection = null; 
       Statement statement = null; 
       String updateUSER = "UPDATE " + TABLE_USERS + " SET " + COLUMN_ENCODEDIMAGE + "='" + details[2] 
         + "' WHERE " + COLUMN_USERID + "='" + name + "' AND '" + COLUMN_PASSWORD + "='" + password + "';"; 
       try { 
        connection = DatabaseUtils.getConnection(); 
        statement = connection.createStatement(); 
        statement.executeUpdate(updateUSER); 
        statement.close(); 
        connection.close(); 
       } catch (SQLException e) { 
        e.printStackTrace(); 
       } 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

在Heroku的日志中,我发现:

org.springframework.http.converter.HttpMessageNotReadableException:需要请求主体缺少的:public void com.rares。 controllers.UploadController.handleUploadImageRequest(java.lang.String)

android代码是否正常?问题在哪里,或者我该怎么做。请不要将我指向已弃用的方法。 我

回答

0

错误张贴。您的请求应该看起来像这样:

request = "username=" + userName 
     + "&password=" + passWord 
     + "&image=" + base64String. 

然后这些值应该是url编码呢。

+0

但我知道如何与拆分获得控制器中的值(”“) – ProgIsMetal

+0

你能和你的方法编辑我的控制器吗?我应该用请求参数来做吗?为什么要求身体现在不是有用的? – ProgIsMetal

+0

如你所见,你不会再发送空格。所以不要在空间上分裂。 – greenapps

相关问题