我试图从我的基于Django的Web后端获取Android中的JsON响应.
我得到的回应是
03-19 16:32:32.120: I/System.out(7442): This si the response march API <!DOCTYPE HTML> <!--[if IE 7]> <HTML lang="en"> <![endif]--> <!--[if IE 8]> <HTML lang="en"> <![endif]--> <!--[if gt IE 8]><!--> <HTML 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 > <img src="/static/assets/img/Ecomlogo.png"/> </div> <div roll="main" ID="main"> <div ><a href="/track_me/scan_open/1/" >Track Shipment</a> <div > <div > Login </div> <div > <form action="." method="POST"> <div style='display:none'><input type='hIDden' name='csrfmIDdlewaretoken' value='xpfLvzIES8w6qzpi7kcfp0crWx9aZHGD' /></div> <input type= "text" name ="username" placeholder="Username" /> <input type= "Password" name="password" placeholder="Password"/> <input type="submit" /> <a href="#" ID="forgotpass">Forgot Your Password?</a> </form> </div> </div> </div> <div > <div > <div > Ecom Express News </div> <div > </div> </div> </div> </div> <!-- modal --> <div ID="add-revlet"> <div > <a data-dismiss="modal">×</a> <h3>Record</h3> </div> <div > </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请求
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 VIEws代码
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响应.在手动点击url后,我按预期获得了JsON响应
编辑1
我也试过这种方法
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调用
编辑3
我得到的http响应为200.httpentity实体的值= response.getEntity();代码是org.apache.http.conn.BasicMangedEntity@4055505d0
解决方法:
响应是将我重定向到我使用@csrf_exempt的自定义登录页面.处理信息的代码需要进一步的登录详细信息.我添加了login参数以及我的调用,并获得了所需的响应
总结以上是内存溢出为你收集整理的java – JSON在Android中的Django回应全部内容,希望文章能够帮你解决java – JSON在Android中的Django回应所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)