Android中使用Apache HttpClient

淩亂°似流年 2022-09-22 14:53 117阅读 0赞
  • Activity.xml

    <?xml version=”1.0” encoding=”utf-8”?>

  1. <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="访问页面" android:id="@+id/button" android:onClick="accessSecret" />
  2. <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:id="@+id/button2" android:onClick="showLogin" />
  3. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" android:text="Hello World!" />
  4. <WebView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/webView" android:layout_gravity="center_horizontal" />
  5. </LinearLayout>
  • login.xml

    <?xml version=”1.0” encoding=”utf-8”?>








    1. <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal">
    2. <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码 " />
    3. <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" />
    4. </LinearLayout>

  • MainActivity.java

    public class MainActivity extends AppCompatActivity {

    1. TextView response;
    2. HttpClient httpClient;
    3. WebView webView;
    4. StringBuilder info = new StringBuilder();
    5. Handler handler = new Handler() {
    6. @Override
    7. public void handleMessage(Message msg) {
    8. if (msg.what == 0x123) {

    webView.loadDataWithBaseURL(null,info.toString(),”text/html”,”utf-8”,null);

    1. }
    2. }
    3. };
    4. @Override
    5. protected void onCreate(Bundle savedInstanceState) {
    6. super.onCreate(savedInstanceState);
    7. setContentView(R.layout.activity_main);
    8. httpClient = new DefaultHttpClient();
    9. response = (TextView) findViewById(R.id.textView);
    10. webView = (WebView) findViewById(R.id.webView);
    11. }
    12. public void accessSecret(View view) {
    13. response.setText("");
    14. new Thread(){
    15. @Override
    16. public void run() {
    17. HttpGet get = new HttpGet("http://helloworlds.top/Test/login.jsp");
    18. try {
    19. HttpResponse httpResponse = httpClient.execute(get);
    20. HttpEntity entity = httpResponse.getEntity();
    21. if (entity != null) {
    22. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
    23. String line = null;
    24. while ((line = bufferedReader.readLine()) != null) {
    25. Message msg = new Message();
    26. msg.what = 0x123;
    27. msg.obj = line;
    28. handler.sendMessage(msg);
    29. }
    30. }
    31. } catch (IOException e) {
    32. e.printStackTrace();
    33. }
    34. }
    35. }.start();
    36. }
  1. public void showLogin(View view) {
  2. final View loginDialog = getLayoutInflater().inflate(R.layout.login, null);
  3. new AlertDialog.Builder(MainActivity.this)
  4. .setTitle("登录系统")
  5. .setView(loginDialog)
  6. .setPositiveButton("登录", new DialogInterface.OnClickListener() {
  7. @Override
  8. public void onClick(DialogInterface dialog, int which) {
  9. final String name = ((EditText) loginDialog.findViewById(R.id.username)).getText().toString();
  10. final String pswd = ((EditText) loginDialog.findViewById(R.id.password)).getText().toString();
  11. new Thread(){
  12. @Override
  13. public void run() {
  14. HttpPost post = new HttpPost("http://www.helloworlds.top/Test/login.jsp");
  15. List<NameValuePair> params = new ArrayList<NameValuePair>();
  16. params.add(new BasicNameValuePair("name",name));
  17. params.add(new BasicNameValuePair("pass",pswd));
  18. try {
  19. post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
  20. HttpResponse response = httpClient.execute(post);
  21. if (response.getStatusLine().getStatusCode() == 200) {
  22. String msg = EntityUtils.toString(response.getEntity());
  23. Looper.prepare();
  24. Message message = new Message();
  25. message.obj = msg;
  26. message.what = 0x123;
  27. handler.sendMessage(message);
  28. Looper.loop();
  29. }
  30. } catch (UnsupportedEncodingException e) {
  31. e.printStackTrace();
  32. } catch (ClientProtocolException e) {
  33. e.printStackTrace();
  34. } catch (IOException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }.start();
  39. }
  40. })
  41. .setNegativeButton("取消",null)
  42. .show();
  43. }
  44. }

发表评论

表情:
评论列表 (有 0 条评论,117人围观)

还没有评论,来说两句吧...

相关阅读