我是 Android 的新手,今天我遇到了一个问题。我想登录这个网站:
http://pedidos.pizzeriabritannia.com/index.asp?Opc=Pedir
并从其源代码中检索一些数据,但我当然不知道从哪里开始。你能给我一些建议或一些文献来处理这个问题吗?
谢谢小伙伴们!
我有以下代码:
public void postLoginData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
/* login.php returns true if username and password is equal to saranga */
HttpPost httppost = new HttpPost("http://pedidos.pizzeriabritannia.com/index.asp?Opc=Pedir");
try {
// Add user name and password
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", "942037851"));
nameValuePairs.add(new BasicNameValuePair("password", "xxxxxx"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
Log.w("SENCIDE", "Execute HTTP Post Request");
HttpResponse response = httpclient.execute(httppost);
String str = inputStreamToString(response.getEntity().getContent()).toString();
Log.w("SENCIDE", str);
if(str.toString().equalsIgnoreCase("true"))
{
Log.w("SENCIDE", "TRUE");
et1.setText("Login successful");
}else
{
Log.w("SENCIDE", "FALSE");
et1.setText("Login not successful");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}