如图,安卓运行截图,如何获得有用数据yes
很多种方式呀,简单的就是字符串截取处理,
public static void main(String[] args){
String html = "<html><body>yes</body></html>";
int s = html.indexOf("<body>");
int e = html.indexOf("</body>");
String str = html.substring(s+"<body>".length(), e);
System.out.println(str);
}
更好的做法是通过调用web接口,获得数据。如果一定这么做,可以通过正则表达式或者html解析器进行解析获得数据。
http://my.oschina.net/sjr/blog/126725
你应该返回json数据,而不是html页面数据,解析json就方便多了
<%@page import="java.io.PrintWriter"%>
<%@page import="java.io.OutputStream"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.test1.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
这是我服务端的代码返回源码!我其实只想让他返回index代表的yes
安卓关键源码:
username = name.getText().toString();
password = pass.getText().toString();
// HttpPost连接对象
try {
HttpPost httpRequest = new HttpPost(URL);
// 使用NameValuePair来保存要传递的Post参数
List params = new ArrayList();
// 添加要传递的参数
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.v("debug", params.toString());
// 设置字符集
HttpEntity httpentity;
httpentity = new UrlEncodedFormEntity(params, "UTF-8");
// 请求httpRequest
httpRequest.setEntity(httpentity);
// 取得默认的HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
// HttpStatus.SC_OK表示连接成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的字符串
String strResult1 = EntityUtils.toString(
httpResponse.getEntity(), "UTF-8");
Toast.makeText(MainActivity.this, strResult1, 2).show();
Log.v("debug", strResult1);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
一般都是通过webservice传递数据,类型一般都是json啦