2017-08-27 47 views
0

私は、文字列レスポンスを正常に返すフラスコサーバアプリケーションを持っています。 私のアンドロイドアプリケーションでは、応答コード200を返しますが、私の接続応答でフラスコから送信されたテキストを取得する場所を見つけることができないようですが、Androidで送信された文字列を取得する最良の方法は何ですか?Android HttpURLConnectionを使用してレスポンスボディから文字列を取得する

は、以下のPOST

try { 

       // open a URL connection to the Servlet 
       InputStream inputStream = this.getContentResolver().openInputStream(sourceFileUri); 
       URL url = new URL(uploadServerUri); 

       // Open a HTTP connection to the URL 
       conn = (HttpURLConnection) url.openConnection(); 
       conn.setDoInput(true); // Allow Inputs 
       conn.setDoOutput(true); // Allow Outputs 
       conn.setUseCaches(false); // Don't use a Cached Copy 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Connection", "Keep-Alive"); 
       conn.setRequestProperty("ENCTYPE", "multipart/form-data"); 
       conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 
       conn.setRequestProperty("uploaded_file", fileName); 

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

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

         dos.writeBytes(lineEnd); 

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

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

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

       while (bytesRead > 0) { 

        dos.write(buffer, 0, bufferSize); 
        bytesAvailable = inputStream.available(); 
        bufferSize = Math.min(bytesAvailable, maxBufferSize); 
        bytesRead = inputStream.read(buffer, 0, bufferSize); 

       } 

       // send multipart form data necesssary after file data... 
       dos.writeBytes(lineEnd); 
       dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

       // Responses from the server (code and message) 

       serverResponseCode = conn.getResponseCode(); 
       String serverResponseMessage = conn.getResponseMessage(); 

       Log.i("uploadFile", "HTTP Response is : " 
         + serverResponseMessage + ": " + serverResponseCode); 

       if(serverResponseCode == 200){ 

        runOnUiThread(new Runnable() { 
         public void run() { 

          String msg = "File Upload Completed."; 

          messageText.setText(msg); 
          Toast.makeText(MainActivity.this, "File Upload Complete.", 
            Toast.LENGTH_SHORT).show(); 
         } 
        }); 
       } 

       //close the streams // 
       inputStream.close(); 
       dos.flush(); 
       dos.close(); 

      } catch (MalformedURLException ex) { 

       dialog.dismiss(); 
       ex.printStackTrace(); 

       runOnUiThread(new Runnable() { 
        public void run() { 
         messageText.setText("MalformedURLException Exception : check script url."); 
         Toast.makeText(MainActivity.this, "MalformedURLException", 
           Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       Log.e("Upload file to server", "error: " + ex.getMessage(), ex); 
      } catch (Exception e) { 

       dialog.dismiss(); 
       e.printStackTrace(); 

       runOnUiThread(new Runnable() { 
        public void run() { 
         messageText.setText("Got Exception : see logcat "); 
         Toast.makeText(MainActivity.this, "Got Exception : see logcat ", 
           Toast.LENGTH_SHORT).show(); 
        } 
       }); 
       Log.e("Upload Exception", "Exception : " 
         + e.getMessage(), e); 
      } 
      dialog.dismiss(); 
      return serverResponseCode; 

     } // End else block 
    } 
} 

を作るAndroidのコードで、次のように明示的に私のフラスコアプリの応答をJSONifyingして、解析することによって答えを見つけ、文字列

return Response(text, mimetype="text/html") 

答えて

0

を返すフラスコアプリですこの質問の最初の答えとしてはhere

関連する問題