2015-10-28 47 views

回答

0

使用HttpURLConnection獲得POST響應非常簡單。我不確定您的項目是否需要您實際上需要使用WebView來查看網頁本身,但如果您只需發出POST請求並獲取響應,我會在下面發佈一些示例代碼以幫助您開始。

在此代碼中,我將某個JSON作爲POST請求發送到服務器,然後檢索服務器作爲響應發回的JSON。

//Http connections and data streams 
URL url; 
HttpURLConnection httpURLConnection = null; 
OutputStreamWriter outputStreamWriter = null; 

try { 

    //open connection to the server 
     url = new URL("your_url_to_web_service"); 
     httpURLConnection = (HttpURLConnection) url.openConnection(); 

     //set request properties 
     httpURLConnection.setDoOutput(true); //defaults request method to POST 
     httpURLConnection.setDoInput(true); //allow input to this HttpURLConnection 
     httpURLConnection.setRequestProperty("Content-Type", "application/json"); //header params 
     httpURLConnection.setRequestProperty("Accept", "application/json"); //header params 
     httpURLConnection.setFixedLengthStreamingMode(jsonToSend.toString().getBytes().length); //header param "content-length" 

     //open output stream and POST our JSON data to server 
     outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream()); 
     outputStreamWriter.write(jsonToSend.toString()); 
     outputStreamWriter.flush(); //flush the stream when we're finished writing to make sure all bytes get to their destination 

     //prepare input buffer and get the http response from server 
     StringBuilder stringBuilder = new StringBuilder(); 
     int responseCode = httpURLConnection.getResponseCode(); 

     //Check to make sure we got a valid status response from the server, 
     //then get the server JSON response if we did. 
     if(responseCode == HttpURLConnection.HTTP_OK) { 

      //read in each line of the response to the input buffer 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(),"utf-8")); 
      String line; 
      while ((line = bufferedReader.readLine()) != null) { 
       stringBuilder.append(line).append("\n"); 
      } 

      bufferedReader.close(); //close out the input stream 

     try { 
      //Copy the JSON response to a local JSONArray 
      jsonResponse = new JSONArray(stringBuilder.toString()); 
     } catch (JSONException je) { 
      je.printStackTrace(); 
     } 

} catch (IOException ioe) { 
    ioe.printStackTrace(); 
} finally { 
    if(httpURLConnection != null) { 
     httpURLConnection.disconnect(); //close out our http connection 
    } 

    if(outputStreamWriter != null) { 
     try { 
      outputStreamWriter.close(); //close our output stream 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 
} 

//Return the JSON response from the server. 
return jsonResponse; 
+0

謝謝您的答覆@drschultz,但在我的Android應用程序,我不接受JSON數據之前的任何數據發送到服務器,但我目前在WebView中,網站沒有提供。示例如果我在我的網站中推送提交按鈕。 –

+0

@LocDaiLe - 但是當您在網站上推送提交按鈕時,它會發出POST請求,是嗎?所以你只需切斷中間人,直接通過代碼發送請求。您是否需要您的用戶出於某種原因實際查看網頁?或者你真的只是在擔心POST調用並獲得響應? – NoChinDeluxe

+0

是用戶需要查看網頁。因爲我想在我的網頁上使用登錄模塊,而不是在android應用上實現它。當用戶通過網頁登錄(使用電子郵件和密碼),然後我想獲得這個發佈數據,以供我的應用程序進一步使用。我想用這些數據直接從java代碼發出請求。我希望你明白我的意思。再次感謝。 PS看到我的問題中的鏈接在iOS中的相同問題,該鏈接中的解決方案就像我希望的那樣工作。 –