Android使用DefaultHttpClient访问要登录的网络
来源:互联网 发布:mac图片处理 编辑:程序博客网 时间:2024/06/05 04:55
用DefaultHttpClient访问要登录的网络
有时候,有一些网站不光是浏览,还需要登录才能访问某些版块,这里就涉及了session\cookie等问题. Android中提供了一个DefaultHttpClient类来解决登录和访问的接口问题
DefaultHttpClient访问这类性质的网络数据步骤如下:
第一步 调用DefaultHttpClient类
第二步 新建一个子线程来写登录代码(发送登录信息),新建一个子线程来写打开页面代码(接收要访问的数据)
第三步 在AndroidManifest.xml文件中添加访问网络权限
用HttpURLConnection发送数据给网络并获得响应数据(登录)的流程:
第一步:新建一个子线程来写通信代码
第二步:用HttpPost类处理访问地址
第三步:设置请求参数并发送数据
可调用HttpPost类里的setEntity()方法设置,如:
ArrayList list=new ArrayList();
list.add(new BasicNameValuePair(参数名, 参数值);
url.setEntity(new UrlEncodedFormEntity(list,HTTP.UTF_8));
强调内容第四步:调用DefaultHttpClient类里的execute()方法建立网络连接并获得返回数据,如:
HttpResponse shuju1=DHC.execute(url);
第五步: 调用返回数据里的getEntity()方法获得核心数据,比如:
HttpEntity shuju2=shuju1.getEntity();
第六步: 用输入流读取网络数据
调用核心数据里的getContent()方法获得网络数据流
第七步: 用Handler处理获得的数据
用DefaultHttpClient接收网络数据(实现访问)的流程:
第一步: 新建一个子线程来写通信代码
第二步: 用HttpGet类或HttpPost类处理访问地址
第三步: 调用DefaultHttpClient类里的execute()方法建立网络连接并获得返回数据,如:
HttpResponse shuju1=DHC.execute(url);
第四步: 调用返回数据里的getEntity()方法获得核心数据,比如:
HttpEntity shuju2=shuju1.getEntity();
第五步: 用输入流读取网络数据
调用核心数据里的getContent()方法获得网络数据流
第六步: 用Handler处理获得的数据
代码(访问的网址为自己搭建的服务器网站,请自行使用自己的网址测试):
主页面XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.adminis.wedapp.DefiaultHttpClientLoginActivity" android:orientation="vertical" android:background="#FFF565"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:text="主页面" android:textSize="25dp" android:layout_marginLeft="30dp" android:layout_marginTop="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="mainPage_OnClick"/> <Button android:textSize="25dp" android:text="登 录" android:layout_marginLeft="120dp" android:layout_marginTop="50dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="login_OnClick"/> </LinearLayout> <TextView android:id="@+id/show_Tv" android:text="Please Login......" android:layout_width="337dp" android:layout_height="381dp" android:layout_gravity="center" android:background="#ffffff"/></LinearLayout>
ItemXMl:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_marginTop="30dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名:" android:textSize="35dp"/> <EditText android:id="@+id/loginUse_Ed" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_marginTop="60dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码:" android:textSize="35dp"/> <EditText android:id="@+id/loginPass_Ed" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout></LinearLayout>
Java:
package com.example.adminis.wedapp;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.widget.EditText;import android.widget.RelativeLayout;import android.widget.TextView;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HTTP;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;public class DefiaultHttpClientLoginActivity extends Activity { private EditText name; private EditText pass; private DefaultHttpClient DHC; //必须定义在类中,这样才能共同拥有一个接口来访问网络 private StringBuilder s; private TextView showText; //用Handler处理获得的数据 Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { showText.setText(s); super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_defiault_http_client_login); showText = (TextView) findViewById(R.id.show_Tv); //注意,必须定义在类中,这样才能共同拥有一个接口来访问网络,如果只是单独发送而不和其他东西连用,可以定义在方法里面 //在javaweb里有一个session机制,在按了登录之后,会给session发送一个用户信息(这里是帐号和密码) //然后在登录主页面的时候,就会直接使用发送过来的session里面保存的用户信息登录去匹配条件,符合的话,就可以登录访问网络 //这样就可以实现在App直接登录和访问网页 DHC = new DefaultHttpClient(); } //访问到主页面按钮 public void mainPage_OnClick(View view){ new Thread(new Runnable() { //创建一个线程来接收 @Override public void run() { BufferedReader in = null; HttpGet url = new HttpGet("http://192.168.0.102:8080/follow2/zhu.jsp"); try { HttpResponse data = DHC.execute(url); HttpEntity data2 = data.getEntity(); in = new BufferedReader(new InputStreamReader(data2.getContent())); s = new StringBuilder(); String line = ""; while ((line = in.readLine())!=null){ s.append("\n"+line); } handler.sendEmptyMessage(0x123); } catch (IOException e) { e.printStackTrace(); }finally { if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }); } //登录按钮 public void login_OnClick(View view){ //实例化一个弹出框 AlertDialog.Builder b = new AlertDialog.Builder(this); //得到自定义的布局,并设置为弹出框的布局 final RelativeLayout RL = (RelativeLayout) getLayoutInflater().inflate(R.layout.login_item,null); b.setView(RL); //给弹出框添加一个确定按钮,并实现发送数据并获得网络反馈 b.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { //注意这里获取的控件不在是当前的Activity,而是弹出框的Activity,所以要加上RL name = (EditText) RL.findViewById(R.id.loginUse_Ed); pass = (EditText) RL.findViewById(R.id.loginPass_Ed); //启动发送线程Send(自定义) new Thread(new Send()).start(); } }); //事件为空就是直接关闭 b.setPositiveButton("取消",null); //创建并显示 b.create().show(); } //Send使用线程发送数据(使用线程来发送) private class Send implements Runnable{ @Override public void run() { BufferedReader in = null; //使用HttpPost类处理访问地址 HttpPost url = new HttpPost("http://192.168.0.102:8080/follow1/denglu.jsp"); //得到用户输入的用户名和密码,并封装好,不同于HttpURLConnection使用的是流来保存信息,这里HttpPost使用的是Arraylist并且是NameValuePair类型 ArrayList<NameValuePair> value = new ArrayList<NameValuePair>(); value.add(new BasicNameValuePair("name",name.getText().toString())); value.add(new BasicNameValuePair("pass",pass.getText().toString())); try { //设置好要发送的数据 url.setEntity(new UrlEncodedFormEntity(value, HTTP.UTF_8)); //建立连接,这里才是真正的发送 HttpResponse data = DHC.execute(url); //获得核心数据 HttpEntity data2 = data.getEntity(); //获得输入流 in = new BufferedReader(new InputStreamReader(data2.getContent())); //创建一个暂时保存数据的字符容器,把数据一行行读出来并写到容器中 s = new StringBuilder(); String line=""; while ((line = in.readLine())!=null){ s.append("\n"+line); } handler.sendEmptyMessage(0x123); //发送handler请求动作,执行hadler里面的方法 } catch (IOException e) { e.printStackTrace(); }finally { //释放资源 if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }}
- Android使用DefaultHttpClient访问要登录的网络
- Android使用DefaultHttpClient访问Wap网络
- Anroid使用DefaultHttpClient访问Wap网络
- Anroid使用DefaultHttpClient访问Wap网络
- 使用DefaultHttpClient发起网络请求遇到的问题
- DefaultHttpClient使用
- DefaultHttpClient使用
- DefaultHttpClient,HttpClient,DefaultHttpClient使用详解
- VBS使用不同的登录 重新访问网络主机
- DefaultHttpClient过时想要继续使用的问题
- Android中,HttpURLConnection与Apache的DefaultHttpClient哪个更被推荐使用?
- 利用DefaultHttpClient实现代理访问,和登录CAS并且写cookie
- android的网络访问
- android的网络访问
- android DefaultHttpClient 超时
- android DefaultHttpClient 超时
- android DefaultHttpClient设置setCookieStore
- Android DefaultHttpClient 上传文件
- 数据库事务
- iOS ---- 混合模式(引导页)
- 网络是否可用的广播
- 杭电2013蟠桃记
- 代码整洁之道 命名 函数
- Android使用DefaultHttpClient访问要登录的网络
- Linux下mysql安装教程
- 字符串和格式化输入/输出
- jboss修改监听端口8080
- android5.0上去掉按钮阴影
- 反向传播BP算法
- java中Collections.sort排序详解
- STL list链表的用法详细解析
- jQuery回调、递延对象,then方法