因此,我正在尝试向网站发送帖子请求,以便搜索特定内容,例如游戏网站上的游戏。问题是每当我执行帖子时,它似乎只返回主网站的帖子值放入搜索栏,而不是结果页面。我打印出整个日志,但是它非常长,所以我只是把它放在我放置字符串的部分,但它仍然是主页。
public class Search extends Fragment {
/*Fragment Variables*/
private String[] results = new String[]{};
private EditText searchEdit;
private View view;
/*AsyncTask Variables*/
private Document doc;
private String urlString = "http://www.vogella.com";
private String title;
private String gamespotSearchId = "siteSearch";
private List<NameValuePair> list = new ArrayList<NameValuePair>(1);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_search, container, false);
searchEdit = (EditText) view.findViewById(R.id.searchEditText);
new BackGroundTask().execute();
return view;
}
class BackGroundTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
HttpPost httpPost = new HttpPost("http://www.gamespot.com");
list.add(new BasicNameValuePair("q", "final fantasy"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(list));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpPost);
// writing response to log
readStream(response.getEntity().getContent());
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// Making HTTP Request
return null;
}
private void readStream(InputStream inputStream) {
BufferedReader buf = null;
try{
buf = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while((line = buf.readLine())!= null){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(buf != null){
try{
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
protected void onPostExecute(String result) {
Log.d("onPostExecute", "just set the text view");
super.onPostExecute(result);
}
}
}
日志
I/System.out( 7052): </div>
I/System.out( 7052): </div>
I/System.out( 7052): </div>
I/System.out( 7052): </section>
I/System.out( 7052): <div class="container">
I/System.out( 7052): <div class="masthead-container masthead-container--flex masthead- container--buffer">
I/System.out( 7052): <!-- Logo -->
I/System.out( 7052): <h1 id="masthead-logo" class="masthead-logo">
I/System.out( 7052): <a href=/ data-click-track-id=coreHeaderFrontDoor class="logo-site">
I/System.out( 7052): <span class="logo-site--short"></span>
I/System.out( 7052): <span class="logo-site--full"></span>
I/System.out( 7052): </a>
I/System.out( 7052): </h1>
I/System.out( 7052):
I/System.out( 7052): <!-- Search -->
I/System.out( 7052): <form action="/search/" method="get" id="siteSearch" class="masthead-search search">
I/System.out( 7052): <fieldset>
I/System.out( 7052): <i class="icon icon-search"></i>
I/System.out( 7052): <input type="text" id="search-main" name="q"
I/System.out( 7052): placeholder="Search GameSpot"
I/System.out( 7052): autocomplete="off" class="search-input ready" data-search-index="game" value="final fantasy" />
I/System.out( 7052): </fieldset>
I/System.out( 7052): </form>
I/System.out( 7052):
I/System.out( 7052): <nav id="masthead-nav" role="navigation" class="masthead-nav">
I/System.out( 7052): <ul class="nav-bar">
I/System.out( 7052): <li class="nav-bar__item dropnav js-dropnav">
I/System.out( 7052): <a href=/new-games/ data-click-track-id=coreHeaderNewGames >Games<i class="icon icon-caret-down"></i></a>
I/System.out( 7052): <i class="icon icon-caret-down js-dropnav-toggle"></i>
I/System.out( 7052):
I/System.out( 7052): <ul class="dropnav-menu dropdown-menu dropnav-menu--has-sidePanel dropnav-menu--has-sidePanel--right dropnav-menu--games arrow--top dropnav-menu--has-borderRadius js-dropnav-active">
I/System.out( 7052): <li class="dropnav-menu__item dropnav-menu__active">
I/System.out( 7052): <a href="/new-games/">New Games</a>
I/System.out( 7052): <ul class="dropnav-submenu">
I/System.out( 7052): <li class="dropnav-submenu__item dropnav-submenu__viewAll">
答案 0 :(得分:0)
我使用Chrome PostMan Rest Client做同样的事情,我得到了相同的结果。
我怀疑Gamespot是否希望人们像这样抓取他们的页面,也许他们会被用户代理标题或类似的东西过滤掉。
看看他们的HTML,无论如何我都不想尝试。
答案 1 :(得分:0)
通过Java http post
打印PHP echo消息是有效的HttpClient client = new DefaultHttpClient();
HttpPost postMethod = new HttpPost("http://192.195.68.83/Upload/index.php");
ArrayList<NameValuePair> arrayList = new ArrayList<NameValuePair>();
arrayList.add(new BasicNameValuePair("cmd", "1"));
arrayList.add(new BasicNameValuePair("comment", "This is comment"));
postMethod.setEntity(new UrlEncodedFormEntity(arrayList));
HttpResponse response = client.execute(postMethod);
HttpEntity httpEntity = response.getEntity();
String state = EntityUtils.toString(httpEntity);
Log.i("chauster",state);