2013-03-20 69 views
0

我想從Android設備發送POST請求到基於Django的後端。我想這個方法使用這些方法,發送POST請求到基於Django的後端

方法1

private String GetPickUpDetails(String pick_up_id2) { 

     StringBuilder response = new StringBuilder(); 
     String stringUrl=Constants.PICKUP_DETAILS+pick_up_id2+"/"; 
     URL url; 
     try { 
      url = new URL(stringUrl); 

      HttpURLConnection httpconn = (HttpURLConnection)url.openConnection(); 
      if (httpconn.getResponseCode() == HttpURLConnection.HTTP_OK) 
      { 
       BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()),8192); 
       String strLine = null; 
       while ((strLine = input.readLine()) != null) 
       { 
        response.append(strLine); 
       } 
       input.close(); 
      } 
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.out.println("This si the response march API "+response.toString()+" for url"+stringUrl); 
     return response.toString(); 


    } 

響應

03-20 12:32:32.120: I/System.out(7442): This si the response march API <!doctype html> <!--[if IE 7]> <html class="no-js ie7 oldie" lang="en"> <![endif]--> <!--[if IE 8]> <html class="no-js ie8 oldie" lang="en"> <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]--> <head> <meta charset="utf-8"> <title>ECOM</title>   <meta http-equiv="X-UA-Compatible" content="IE=Edge;chrome=1" > <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">   <meta name="description" content=""> <meta name="author" content="">  <meta name="apple-mobile-web-app-capable" content="yes" />  <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />   <meta property="og:image" content="http://www.domain.com/images/logo.png"/> <meta property="og:title" content="-- description here --"/> <meta property="og:url" content="http://www.domain.com"/>   <link rel="shortcut icon" href="/static/assets/img/favicon.ico" /> <link rel="apple-touch-icon" href="/static/assets/img/apple-touch-icon.png"> <link rel="apple-touch-icon" sizes="72x72" href="/static/assets/img/apple-touch-icon-72x72.png"> <link rel="apple-touch-icon" sizes="114x114" href="/static/assets/img/apple-touch-icon-114x114.png">    <link rel="stylesheet" href="/static/assets/css/styles.css" />     <script src="/static/assets/js/libs/modernizr.custom.60077.js"></script>   <link rel="stylesheet" href="/static/assets/css/popup.css" />    <script src="/static/assets/js/jquery-1.7.2.min.js"></script>   <script src="/static/assets/authentication/js/popup.js"></script> </head><body><div id="backgroundPopup"></div>  <div class="header" style="text-align:center; padding-top:10px;">  <img src="/static/assets/img/Ecomlogo.png"/> </div> <div class="container-fluid" roll="main" id="main"> <div class="span6"><a href="/track_me/scan_open/1/" class="forgotpass pull-right">Track Shipment</a>  <div class="login">  <div class="title">   Login     </div>    <div class="content-login">   <form action="." method="POST">   <div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='xpfLvzIES8w6qzpi7kCFp0crWx9aZHGD' /></div>    <input type= "text" name ="username" placeholder="Username" class="user-name"/>   <input type= "Password" name="password" placeholder="Password"/>   <input type="submit" class="button-login"/>   <a href="#" class="forgotpass pull-right" id="forgotpass">Forgot Your Password?</a>   </form>  </div>  </div> </div> <div class="span6">  <div class="login">  <div class="title">   Ecom Express News     </div>  <div class="content-login">     </div>  </div> </div> </div>  <!-- modal --> <div class="modal hide modal-add-revlet" id="add-revlet"> <div class="modal-header">  <a class="close" data-dismiss="modal">×</a>  <h3>Record</h3> </div> <div class="modal-body">   </div> </div><!--modal end--> <div id="popupContact">    <a id="popupContactClose">x</a> </div>  <!-- uncomment pre-deloy --> <!--<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>--> <script>window.jQuery || document.write('<script src="assets/js/libs/jquery-1.7.1.min.js"><\/script>')</script> <!-- bootstrap --> <script src="/static/assets/js/bootstrap-transition.js"></script> <script src="/static/assets/js/bootstrap-alert.js"></script> <script src="/static/assets/js/bootstrap-modal.js"></script> <script src="/static/assets/js/bootstrap-dropdown.js"></script> <script src="/static/assets/js/bootstrap-scrollspy.js"></script> <script src="/static/assets/js/bootstrap-tab.js"></script> <script src="/static/assets/js/bootstrap-tooltip.js"></script> <script src="/static/assets/js/bootstrap-popover.js"></script> <script src="/static/assets/js/bootstrap-button. 

方法2

try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
      nameValuePairs.add(new BasicNameValuePair("scanned", jsonScanned.toString())); 
      nameValuePairs.add(new BasicNameValuePair("unscanned",jsonUnscanned.toString())); 
      nameValuePairs.add(new BasicNameValuePair("pickupid", pick_up_id)); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      response = httpclient.execute(httppost); 

     } catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
     } 

     if (response.getStatusLine().getStatusCode() == 200) 
     { 
      HttpEntity entity = response.getEntity(); 
      try { 
       json = EntityUtils.toString(entity); 
      } catch (ParseException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     System.out.println("this is response"+json); 
     int iTmp=response.getStatusLine().getStatusCode(); 

     return response.toString(); 

響應

[email protected] 

就進一步學習我收到錯誤403的上述請求。

請指出我在這個方法中犯的錯誤。任何其他方法以及片段也是受歡迎的。

編輯1 我得到的HTTP響應作爲代碼的HttpEntity entity = response.getEntity(); 200.The值[email protected]

回答

1

你不說有什麼錯與第一種方法。

第二,如果你得到403這可能是因爲跨站請求僞造保護(CSRF)。有關如何處理,請參見the docs

+0

我已經添加了第一種方法得到的迴應。 http://stackoverflow.com/questions/15497833/json-response-from-django-in-android請參考這個問題也 – onkar 2013-03-20 07:51:32