最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Android中,通过response.getEntity()去获得HttpClient返回的HttpResponse,只获得部分的HTML内容

Android crifan 5838浏览 0评论

【问题】

通过下面代码:

    	HttpGet request = new HttpGet(strSongUrl);
    	HttpClient httpClient = new DefaultHttpClient();
    	
        try {
            HttpResponse response = httpClient.execute(request);
            if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
            	String respHtml = EntityUtils.toString(response.getEntity());
            	//etUrlOrId.setText(respHtml);
            	//<p class="mid_tit">我的爱与你分享</p><p></p>
            	Pattern titleP = Pattern.compile("<p\\s+class=\"mid_tit\">(.+?)</p>");
            	Matcher matchedTitle = titleP.matcher(respHtml);
            	Boolean foundTitle = matchedTitle.matches();
            	if(foundTitle){
            		String title = matchedTitle.group(1);
            	}
            	else
            	{
            		Toast.makeText(getApplicationContext(), "找不到Songtaste歌曲的标题!", Toast.LENGTH_SHORT).show();
            	}
            	//System.out.println(response);
            }
        } catch (ClientProtocolException cpe) {
            // TODO Auto-generated catch block
        	cpe.printStackTrace();    
        } catch (IOException ioe) {
            // TODO Auto-generated catch block
        	ioe.printStackTrace();
        }

结果获得的respHtml,经过调试发现,只有其中,正常网页html的一部分。

【解决过程】

1.参考了:

HttpPost returns only part of response body

其说,只是调试器的问题,只显示了部分的html内容。

但是我这里,的确是由于后面的正则去匹配,的确找不到对应的内容的,所以,至少我这里,貌似的确只返回了部分的html。

2.再去把代码改一下:

            	//Pattern titleP = Pattern.compile("<p\\s+class=\"mid_tit\">(.+?)</p>");
            	Pattern titleP = Pattern.compile("mid_tit");

看看能否匹配到,结果还是无法匹配到。

3.现已确定,已获得的部分的html中,包含了saveSongName,所以改为:

            	//Pattern titleP = Pattern.compile("<p\\s+class=\"mid_tit\">(.+?)</p>");
            	//Pattern titleP = Pattern.compile("mid_tit");
            	Pattern titleP = Pattern.compile("saveSongName");

看看能否匹配到。再匹配不到,则就是正则部分的代码的问题了。

结果的确还是搜不到。

4.后来才想起来,matches是匹配,不是查找。所以改为:

            	Pattern titleP = Pattern.compile("saveSongName");
            	Matcher matchedTitle = titleP.matcher(respHtml);
            	//Boolean foundTitle = matchedTitle.matches();
            	Boolean foundTitle = matchedTitle.find();
            	if(foundTitle){
            		String title = matchedTitle.group();
            		etUrlOrId.setText(title);
            	}

去试试,结果是可以匹配到的。

5.所以再改为原先的:

    	HttpGet request = new HttpGet(strSongUrl);
    	HttpClient httpClient = new DefaultHttpClient();
    	
        try {
            HttpResponse response = httpClient.execute(request);
            if(response.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
            	String respHtml = EntityUtils.toString(response.getEntity());
            	//etUrlOrId.setText(respHtml);
            	//<p class="mid_tit">我的爱与你分享</p><p></p>
            	Pattern titleP = Pattern.compile("<p\\s+class=\"mid_tit\">(.+?)</p>");
            	//Pattern titleP = Pattern.compile("mid_tit");
            	//Pattern titleP = Pattern.compile("saveSongName");
            	Matcher matchedTitle = titleP.matcher(respHtml);
            	//Boolean foundTitle = matchedTitle.matches();
            	Boolean foundTitle = matchedTitle.find();
            	if(foundTitle){
            		String title = matchedTitle.group(1);
            		etUrlOrId.setText(title);
            	}
            	else
            	{
            		Toast.makeText(getApplicationContext(), "找不到Songtaste歌曲的标题!", Toast.LENGTH_SHORT).show();
            	}
            	//System.out.println(response);
            }
        } catch (ClientProtocolException cpe) {
            // TODO Auto-generated catch block
        	cpe.printStackTrace();    
        } catch (IOException ioe) {
            // TODO Auto-generated catch block
        	ioe.printStackTrace();
        }

然后最后终于是可以获得我们所需要的值了。不过暂时是乱码:

can found title but messy code

 

【总结】

1.对于Java中的正则,真的很烂:

  • Java 7之前,不支持named group
  • 此处对于正则,是先去find,然后不是获得对应的结果,还需要另外的matchedTitle.group(1);才能获得结果。
    • 此种脑残式的API,不知道哪个挫人设计的。。。

2.上述的返回的html内容,的确是完整的,只是调试器Debugger,无法完整显示而已。

3. 现在,接下来,还是要去搞定返回的html是乱码的问题:需要解码为对应,Unicode类型的字符才行。

转载请注明:在路上 » 【已解决】Android中,通过response.getEntity()去获得HttpClient返回的HttpResponse,只获得部分的HTML内容

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.152 seconds, using 22.13MB memory