读取jSON响应 [英] read jSON response
问题描述
首次通话的网址
public static final String JSON_RESPONSE_STRING =http://192.168.2.5:8000/mobile/;
二次通话的网址
public static final String PICKUP_DETAILS =http://192.168.2.5:8000/mobile/shippments/;
使用的常用方法
public static String GetJSONResponse(String stringUrl)throws IOException
{
StringBuilder response = new StringBuilder();
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();
}
return response.toString();
}
django url模式
urlpatterns = patterns('mobile_api.views',
url(r'^ $','login_authenticate'),
url (r'^ shippments /(?P id = \w +)','pickup_details'),
url(r'^ report / post /','pickup_report')
)$ b $我在我的代码中使用了两次这个方法。这个代码第一次工作得很好,解析json响应。
当我在其他地方使用这种方法时,我称之为
pick_up_id = getIntent()。getStringExtra(pickup_id);
String stringUrl = Constants.PICKUP_DETAILS +555;
String response = null;
try {
response = PickupList.GetJSONResponse(stringUrl);
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
// String response = GetPickUpDetails(pick_up_id);
System.out.println(second call response+ response);
并获得此回复
03-22 12:19:37.979:I / System.out(20703):第二个呼叫响应<!doctype html> <! - [if IE 7]> < html class =no-js ie7 oldielang =en>百分比抑制率ENDIF] - GT!; <! - [if IE 8]> < html class =no-js ie8 oldielang =en>百分比抑制率ENDIF] - GT!; <! - [if gt IE 8]><! - > < html class =no-jslang =en> <! - <![ENDIF] - GT; < HEAD> < meta charset =utf-8> <标题> ECOM< /标题> < meta http-equiv =X-UA-Compatiblecontent =IE = Edge; chrome = 1> < meta name =viewportcontent =width = device-width,initial-scale = 1.0,maximum-scale = 1.0> < meta name =descriptioncontent => < meta name =authorcontent => < meta name =apple-mobile-web-app-capablecontent =yes/> < meta name =apple-mobile-web-app-status-bar-stylecontent =black-semiconductor/> < meta property =og:imagecontent =http://www.domain.com/images/logo.png/> < meta property =og:titlecontent = - description here - /> < meta property =og:urlcontent =http://www.domain.com/> < link rel =快捷图标href =/ static / assets / img / favicon.ico/> < link rel =apple-touch-iconhref =/ static / assets / img / apple-touch-icon.png> < link rel =apple-touch-iconsizes =72x72href =/ static / assets / img / apple-touch-icon-72x72.png> < link rel =apple-touch-iconsizes =114x114href =/ static / assets / img / apple-touch-icon-114x114.png> < link rel =stylesheethref =/ static / assets / css / styles.css/> < script src =/ static / assets / js / libs / modernizr.custom.60077.js>< / script> < link rel =stylesheethref =/ 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 =headerstyle =text-align:center; padding-top:10px;> < img src =/ static / assets / img / Ecomlogo.png/> < / DIV> < div class =container-fluidroll =mainid =main> < div class =span6>< a href =/ track_me / scan_open / 1 /class =forgotpass pull-right> Track Shipment< / a> < div class =login> < div class =title>登录< / div> < div class =content-login> < form action =。方法= POST > < div style ='display:none'>< input type ='hidden'name ='csrfmiddlewaretoken'value ='p0SPV6QzpX2nP2p588V5lDU9s9LLWZDO'/>< / div> < input type =textname =usernameplaceholder =Usernameclass =user-name/> < input type =Passwordname =passwordplaceholder =Password/> < input type =submitclass =button-login/> < a href =#class =forgotpass pull-rightid =togotpass>忘记密码?< / a> < /形式> < / DIV> < / DIV> < / DIV> < div class =span6> < div class =login> < div class =title> Ecom Express新闻< / div> < div class =content-login> < / DIV> < / DIV> < / DIV> < / DIV> <! - 模态 - > < div class =modal hide modal-add-revletid =add-revlet> < div class =modal-header> < a class =closedata-dismiss =modal>×< / a> < H3>记录和LT; / H3> < / DIV> < div class =modal-body> < / DIV> < / div><! - 模态结束 - > < 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> <! - 引导 - > < 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.js>< / script>
PS 此外,如果代码有任何问题,为什么它工作的第一个电话。
注1 这两个响应都是我已经验证过的几个json对象.my后端是 dJango 。在浏览器上点击URL我得到.json格式的正确答案
解决方案检查 csrf_exempt
或正确的访问权限的应用程序
的URLI am trying to read a json response from my python based backend. I am using this code for accessing the information.
URL for first call
public static final String JSON_RESPONSE_STRING="http://192.168.2.5:8000/mobile/";
URL forsecond call
public static final String PICKUP_DETAILS="http://192.168.2.5:8000/mobile/shippments/";
common method using
public static String GetJSONResponse(String stringUrl) throws IOException
{
StringBuilder response = new StringBuilder();
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();
}
return response.toString();
}
django url patterns
urlpatterns = patterns('mobile_api.views',
url(r'^$','login_authenticate'),
url(r'^shippments/(?P<id>\w+)','pickup_details'),
url(r'^report/post/','pickup_report')
)
I am using this method twice in my code. For the first time this code is working absolutely fine and parsing the json response .
When I am using this method at other place I am call it
pick_up_id=getIntent().getStringExtra("pickup_id");
String stringUrl=Constants.PICKUP_DETAILS+"555";
String response = null;
try {
response = PickupList.GetJSONResponse(stringUrl);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//String response=GetPickUpDetails(pick_up_id);
System.out.println("second call response"+response);
and getting this response
03-22 12:19:37.979: I/System.out(20703): second call response<!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='p0SPV6QzpX2nP2p588V5lDU9s9LLWZDO' /></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.js"></script>
P.S Also, if there is any problem with the code then why is it working for first call.
NOTE 1 Both the response are json objects I have verified several times.my backend is dJango. On hitting the url on browser I am getting the correct response in .json format
解决方案 check for csrf_exempt
or proper permission for access to the url from app
这篇关于读取jSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!