我正在尝试从Google Flight页面获取JSON响应。我查看了以下教程,该教程解释了如何获取它link。所以我在Android中创建了一个HttpPost请求并添加了所有必需的标头我从谷歌浏览器获取模拟请求(我正在检查我的Chrome浏览器中的请求/响应)。我的问题是如何获得JSON响应。到目前为止我的代码。
public class SearchScreen extends Activity {
private static final String URL = "http://www.google.com/flights/rpc";
//"www.google.com/flights/#search;f=SJC;t=EWR,JFK,LGA;d=2012-04-05;r=2012-04-12"
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_screen);
new AirplaneDetailsAsyncTask().execute(URL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.search_screen, menu);
return true;
}
private class AirplaneDetailsAsyncTask extends
AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response;
HttpPost post = new HttpPost(urls[0]);
try {
post.setEntity(new StringEntity(streamToString(getAssets().open("json_payload.txt"))));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
post.addHeader("Host", "www.google.com");
post.addHeader("Content-Type", "application/json; charset=utf-8");
post.addHeader("X-GWT-Permutation",
"C8210E5F468630F84E578D8EDE10A1A0");
post.addHeader("X-GWT-Module-Base",
"http://www.google.com/flights/static/");
post.addHeader("Referer", "http://www.google.com/flights/");
post.addHeader(
"Cookie",
"PREF=ID=e2a92b3380d7d27f:U=3d554774092713bb:LD=en:TM=1358479701:LM=1380698072:DV=ItTa9H05UtgwsKnDG0OzXrRI8wzwgvK6-YzRssPfBQAAACoAp5J-Wg59AQAAAA:GM=1:S=UCXKy-x_emr9K3SO; HSID=AgVJtn-g2CDMiOMYl; SSID=A4yKK8G7fF8e5M01T; APISID=s-DKCLDgya0AD2eY/ArIOuAT3DXpIkznme; SAP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gZJYVdwaTbnl1a6-BsXbUARSZIbQZ6plwUGjHGiWLvFoGY3l2YElBX9irN46Mts87A8kb2tSXuN6DOhRurdebChUkXYxcAt0OcoXIYTiTrQWr4QpPfrX1pt2imQqRTlj1E0t8nkg684tKbF6w2Fsul06Wlq1yIHFOdVffklF7BSSoFNS8oAAG-JfOYJu_-FuRQQbXlBazJUsa_DO-w");
post.addHeader("Pragma", "no-cache");
//post.addHeader("Content-Length","1255");
post.addHeader("Cache-Control", "no-cache");
String entityBody;
try {
response = client.execute(post);
int respnseCode = response.getStatusLine().getStatusCode();
switch (respnseCode) {
case 200:
HttpEntity entity = response.getEntity();
if (entity != null) {
entityBody = EntityUtils.toString(entity);
Log.d("Entity", entityBody);
}
break;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private String streamToString(InputStream inputStream ){
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return total.toString();
}
}
}
以下是我收到的回复字符串
航班 - Google搜索 var JS_ILT = new Date(); #gbar,#guser {font-size:13px; padding-top:1px!important;} #gbar {height:22px} #guser {padding-bottom:7px!important; text-align:right} .gbh,.gbd {border-top:1px solid#c9d7f1; font-size:1px} .gbh {height:0; position:absolute; top:24px; width:100%} @ media all {.gb1 {height:22px; margin-right :.5em; vertical-align:top} #gbar {float:left}} a.gb1,a.gb4 {text-decoration:underline!important} a.gb1,a.gb4 {color:#00c!important}。 gbi .gb4 {color:#dd8e27!important} .gbf .gb4 {color:#900!important} var JS_INI =“[,[,[,[14,1088,1115,1179,1171,1166,1178,1118,1117,1064,1143,1105,1185,1106,1133,1023, 1109,1174,1060,1043,1163,1161,1154,1159,1202,1150,1160,1156,1172,15,10]],[,\ 42USD \ 42,\ 42 $ \ 42,\ 42 \ u00a4# ,## 0 \ 42,\ 42 \ u00a4#,## 0.00 \ 42,\ 42#,## 0 \ 42],[,2,1609.344,\ 42#,## 0 \ 42,\ 42#, ## 0.0 \ 42] \ 42google行程\ 42,\ 42typeId:72275 \ 42,\ 42ADS25WNrXN2r90yVrxoc2cGLL7byq1lyPXBcDPuISN0-8h2S_kUgpIUaPCwDlQoEipKwyJ7E7ooMJtl_HjfJJOv5EIoI8BrHKVaEvnBdZYk9ur5urOwqXwZoqiE3ZtMlhVA8dXL1Rd4Hia5rkMFBC6JVRnxLjW5avoIXio1_-S1mV7t4b20FEDBflib8wQ \ 42,\ 42.com \ 42,\ 42en \ 42,1 ,,,,,, \ 42https://accounts.google.com/ServiceLogin?continue \ u003d_CONTINUE_ \ 42],[,\ 42LAX \ 42,\ 42洛杉矶\ 42,33.9425,-118.40806,[\ 42LAX \ 42,\ 42VNY \ 42,\ 42WHP \ 42,\ 42QLO \ 42,\ 42CCD \ 42,\ 42JWC \ 42],[\ 42LAX \ 42],\ 42美国\ 42,\ 42CA \ 42,\ 42US \ 42,\ 42California \ 42,\ 42 / //lh3.ggpht.com/SJl6YCFFaYdELEiAZhbxZkMpyc65BFrlJd6G-m803O0n5EVMFDZmYibv1ie8KQ\42],[[,\42LAX\42,\42洛杉矶国际\ 42,\ 42洛杉矶\ 42,\ 42LAX \ 42,\ 42美国\ 42,33.94 25,-118.40806,\ 42CA \ 42,\ 42US \ 42]],[[\ 42ONEWORLD \ 42,\ 42Oneworld \ 42],[,\ 42SKYTEAM \ 42,\ 42SkyTeam \ 42],[,\ 42STAR_ALLIANCE \ 42 ,\ 42Star Alliance \ 42]] ,, [[,\ 42_web \ 42,\ 42Web \ 42],[,\ 42_fli \ 42,\ 42Flights \ 42],[,\ 42_mor \ 42,\ 42More \ 42], [,\ 42_few \ 42,\ 42Fewer \ 42],[,\ 42isch \ 42,\ 42Images \ 42],[,\ 42#地图\ 42,\ 42Maps \ 42],[,\ 42shop \ 42,\ 42Shopping \ 42],[,\ 42vid \ 42,\ 42Videos \ 42],[,\ 42nws \ 42,\ 42News \ 42],[,\ 42bks \ 42,\ 42Books \ 42],[,\ 42blg \ 42, \ 42Blogs \ 42],[,\ 42dsc \ 42,\ 42Discussions \ 42],[,\ 42rcp \ 42,\ 42Recipes \ 42],[,\ 42app \ 42,\ 42Applications \ 42],[,\ 42pts \ 42,\ 42Patents \ 42]],1]“; (function(){var gs = document.createElement('script'); var wmm = window.matchMedia; var hires = !! wmm&amp;&amp;!wmm('( - webkit-device-pixel-ratio:1.0) ')。匹配&amp;&amp;!wmm('( - moz-device-pixel-ratio:1.0)')。匹配; gs.src =!hires?'static / 3AB5E5242B57171E6FA8BDDC5CF6C944.cache.js':'static / CC76A5EF859E30018C45EE797352718D .cache.js '; gs.type =' 文本/ JavaScript的 '; gs.async = TRUE; document.getElementsByTagName(' 头“)[0] .appendChild(GS);})();
(function(){var script = document.createElement('script'); script.type ='text / javascript'; script.src ='https://ssl.gstatic.com/feedback/api.js'; document .body.appendChild(脚本);})();搜索图片邮件驱动器日历移动更多»Google Flight Search尚未针对您的浏览器进行优化。要获得最佳效果,请尝试使用Chrome,Firefox 3.5 +,Internet Explorer 8 +,Safari 4+。关闭
更新:指向rpc终点服务器后,我得到一个响应代码405方法不允许。