来自Android的Django的JSON响应

我试图从我的基于Django的Web后端获取Android中的jSON响应。 我得到的回应是

03-19 16:32:32.120: I/System.out(7442): This si the response march API      <!--   ECOM                   
x
<!----> window.jQuery || document.write('') <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响应为HttpEntity entity = response.getEntity();HttpEntity entity = response.getEntity(); 代码是org.apache.http.conn.BasicMangedEntity@4055505d0

响应是将我重定向到我使用@csrf_exempt的自定义登录页面。 处理信息的代码需要进一步的登录详细信息。 我添加了login参数以及我的调用,并获得了所需的响应

TL; DR
httpconn.setRequestProperty("Accept", "application/json");


HttpURLConnection的默认“Accept”HttpHeader值是text/html, image/gif, image/jpeg, *; q=.2, /; q=.2 text/html, image/gif, image/jpeg, *; q=.2, /; q=.2

当使用text/html ,django返回用HTML包装的JSON。 如果需要普通JSON,则应将Accept HttpHeader值设置为'application/json'