JSON响应从Django的Andr​​oid中 [英] JSON Response from Django in Android

查看:242
本文介绍了JSON响应从Django的Andr​​oid中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Django的基于Web的后台Android中的JSON响应。 我得到的响应是

I am trying to get a jSON response in Android from my Django based web backend. The response I am getting is

03-19 16: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.

Android的请求

Android Request

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


    }

Django的浏览code

Django Views code

def pickup_details(request,id):
        awb_dict={}
    if request.POST:
        print "this is post request",id 
        shipment_records=[]
        shippments=Shipment.objects.filter(pickup_id=id)

        for obj in shippments:
            awb=obj.airwaybill_number
            order_number=obj.order_number
            pincode=obj.pincode
            dest_sc=obj.service_centre
            type=obj.product_type
            if(type == "ccd"):
                col_val=obj.collectable_value
            else:
                col_val=""    
            status=obj.status
            city=obj.destination_city
            act_wt=obj.actual_weight
            dest_SC="hi"
            dest_SC=obj.service_centre
            #dest_SC=String(dest_SC).replace('-',' ')   
            record={"awb":awb,"ordernumber":order_number,"pincode":pincode,"weight":act_wt,"status":status,"city":city,"type":type,"col_value":col_val,"dest_SC":city}
            shipment_records.append(record)

        awb_dict["count"]=len(shipment_records)
        awb_dict["shipments"]=shipment_records
    #else:
         #pass

        return HttpResponse(simplejson.dumps(awb_dict) ) 

我不是那么肯定,为什么我没有得到预期的JSON响应。经手工打的网址我得到JSON响应预期

I am not so sure, why I am not getting the expected jSON response. Upon manually hitting the url I am getting the JSON response as expected

修改1 我已经试过这种方式也

EDIT 1 I have tried this approach also

try{
            URL 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();
        }
        return response.toString();
}

编辑2 我得到的错误是 HTTP 403请求试着发送请求的值没有达到API调用

EDIT 2 The error I am getting is HTTP 403 Request while trying to the sent the request as the value is not reaching the API call

修改3 我得到的HTTP响应, HttpEntity实体200.The值= response.getEntity(); 为code是 org.apache .http.conn.BasicMangedEntity @ 4055505d0

EDIT 3 I am getting the HTTP response as 200.The value of HttpEntity entity = response.getEntity(); for the code is org.apache.http.conn.BasicMangedEntity@4055505d0

推荐答案

该响应重定向我到自定义登录页面,我用了 @csrf_exempt 。被要求为$ C $下再登录信息来处理信息。我增加了登录参数与我的电话,得到所需的响应

The response was redirecting me to the custom login-page where I had used @csrf_exempt. Further login details were required for code to process the info. I added the login parameter along with my call and the desired response was obtained

这篇关于JSON响应从Django的Andr​​oid中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆