2017-08-27 68 views
0

我有一個瓶頸服務器應用程序,它成功返回一個文本字符串響應。 在我的android應用程序中它返回響應代碼200,但是我似乎無法找到在連接響應中通過燒瓶發送的文本的位置,在Android中檢索發送的字符串的最佳方法是什麼?Android從響應正文使用HttpURLConnection獲取字符串

下面是Android代碼使得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 
    } 
} 

而下面是瓶的應用程序返回的字符串

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

回答

0

通過明確JSONifying我的瓶的應用程序的響應,然後解析找到答案它作爲這個問題的第一個答案呢here

相關問題