java - 向基于 Django 的后端发送 POST 请求

标签 java android python django django-views

我正在尝试从 Android 设备向基于 Django 的后端发送 POST 请求。我正在使用这些方法尝试这种方法,

方法 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(); 

回应

org.apache.http.message.BasicHttpResponse@40525230

在进一步学习时,我收到上述请求的错误 403。

请指出我在这种方法中犯下的错误。也欢迎任何其他方法和代码 fragment 。

编辑 1 我得到的 HTTP 响应为 200。 HttpEntity entity = response.getEntity(); 的值代码是org.apache.http.conn.BasicMangedEntity@4055505d0

最佳答案

你没有说第一种方法有什么问题。

对于第二个,如果您收到 403,可能是因为跨站点请求伪造保护 (CSRF)。请参阅the docs该怎么办。

关于java - 向基于 Django 的后端发送 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15517745/

相关文章:

java - 迁移 androidX 后无法实例化 fragment com.google.android.gms.maps.SupportMapFragment

android - 如何为 crashlytics 定义 list 文件的路径

python - 如何在没有 for 循环的情况下提取 theano 张量中的成对对角线?

java - <#assign x=${size}> 它没有给出正确的结果

java - 如何启用或禁用JavaFX表单中的按钮?

java - 绑定(bind) XML POJO 类数据类型 - 应用程序设计

android - 使用 Phonegap 在本地存储数据库的最佳方式

java - 如何使 actionListener 方法动态化

python - 是否可以从命令行找到 python 包的路径?

python - 我们如何在 Open CV 中创建一个轨迹栏,以便我可以使用它跳到视频的特定部分?