2017-03-13 167 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

相關問題