Android中使用Apache HttpClient
Activity.xml
<?xml version=”1.0” encoding=”utf-8”?>
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="访问页面" android:id="@+id/button" android:onClick="accessSecret" />
<Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:id="@+id/button2" android:onClick="showLogin" />
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:text="Hello World!" />
<WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" android:layout_gravity="center_horizontal" />
</LinearLayout>
login.xml
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码 " />
<EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView response;
HttpClient httpClient;
WebView webView;
StringBuilder info = new StringBuilder();
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {
webView.loadDataWithBaseURL(null,info.toString(),”text/html”,”utf-8”,null);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
httpClient = new DefaultHttpClient();
response = (TextView) findViewById(R.id.textView);
webView = (WebView) findViewById(R.id.webView);
}
public void accessSecret(View view) {
response.setText("");
new Thread(){
@Override
public void run() {
HttpGet get = new HttpGet("http://helloworlds.top/Test/login.jsp");
try {
HttpResponse httpResponse = httpClient.execute(get);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
String line = null;
while ((line = bufferedReader.readLine()) != null) {
Message msg = new Message();
msg.what = 0x123;
msg.obj = line;
handler.sendMessage(msg);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public void showLogin(View view) {
final View loginDialog = getLayoutInflater().inflate(R.layout.login, null);
new AlertDialog.Builder(MainActivity.this)
.setTitle("登录系统")
.setView(loginDialog)
.setPositiveButton("登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final String name = ((EditText) loginDialog.findViewById(R.id.username)).getText().toString();
final String pswd = ((EditText) loginDialog.findViewById(R.id.password)).getText().toString();
new Thread(){
@Override
public void run() {
HttpPost post = new HttpPost("http://www.helloworlds.top/Test/login.jsp");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name",name));
params.add(new BasicNameValuePair("pass",pswd));
try {
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
String msg = EntityUtils.toString(response.getEntity());
Looper.prepare();
Message message = new Message();
message.obj = msg;
message.what = 0x123;
handler.sendMessage(message);
Looper.loop();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
})
.setNegativeButton("取消",null)
.show();
}
}
还没有评论,来说两句吧...