为了账号安全,请及时绑定邮箱和手机立即绑定

Http相关(一)

标签:
Android

网络通信已经学了两天了,因为还是学习阶段,并没有做到多么高端,只是初步了解一些方法的使用。

开始第一个学习任务是用HttpURLConnection请求一下百度的XML文件。

先放一下代码:

private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SHOW_RESPONSE:
                    String response = (String)msg.obj;
                    responseText.setText(response);
            }
        }
    };

这段代码的大致作用是把获取到的信息存储在Message上,Handler是完成这一活动的方法(因为初学,这是我个人对于代码的认知)

 sendRequestWithHttpURLConnection();

这个方法是设置具体的接口(URL)和参数(out.write),而参数的设置需要new一个输出流的实体对象:

DataOutputStream out = new DataOutputStream(connection.getOutputStream());

然后用实体对象out调用write方法实现传参;

值得提的是下面这一段:

 connection = (HttpURLConnection) url.openConnection();//这是开启连接方法
                 connection.setRequestMethod("POST");
                 connection.setConnectTimeout(8000);
                 connection.setReadTimeout(8000);
                 connection.setDoOutput(true);
                 connection.setDoInput(true);
                 connection.setRequestProperty("Content-type",
 "application/x-www-form-urlencoded;charset=utf-8");

对这些东西,我的个人理解可以把他们当成XML文件里面的类似于宽、高、距离父容器边缘的距离之类的属性。其实他们原本就是属性,不过是网络请求的属性(请求时间超时,等待时间超时...)只要用网络请求实体对象connection调用一下相关的方法就可以进行设置。

我深知小白不见源码不下笔的苦楚,把完整的源码粘贴出来:

public class HttpDamo extends AppCompatActivity implements View.OnClickListener {
    public  static final int SHOW_RESPONSE = 0;
    private Button sendRequest;
    private TextView responseText;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case SHOW_RESPONSE:
                    String response = (String)msg.obj;
                    responseText.setText(response);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_damo);
        sendRequest = (Button)findViewById(R.id.send_request);
        responseText = (TextView)findViewById(R.id.request_text);
        sendRequest.setOnClickListener(this);
    }
    public void onClick (View v){
        if (v.getId()==R.id.send_request){
           sendRequestWithHttpURLConnection();
        }
}
    private void sendRequestWithHttpURLConnection() {
     new Thread(new Runnable() {
         @Override
         public void run() {
             HttpURLConnection connection = null;
             try {
                 URL url = new URL
                         ("http:baidu.com");
                 connection = (HttpURLConnection) url.openConnection();
                 connection.setRequestMethod("POST");
                 connection.setConnectTimeout(8000);
                 connection.setReadTimeout(8000);
                 connection.setDoOutput(true);
                 connection.setDoInput(true);
                 connection.setRequestProperty("Content-type",
 "application/x-www-form-urlencoded;charset=utf-8");
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
                 out.write("参数".getBytes());
                 out.flush();
                 out.close();
                 InputStream in = connection.getInputStream();
                 BufferedReader resder = new BufferedReader(new InputStreamReader(in));
                 StringBuilder response = new StringBuilder();
                 String line;
                 while ((line=resder.readLine()) !=null){
                 response.append(line);
                   }
                 Message message = new Message();
                 message.what = SHOW_RESPONSE;
                 message.obj = response.toString();
                 handler.sendMessage(message);
             } catch (MalformedURLException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }finally {
                 if (connection != null){
                     connection.disconnect();
                 }
             }


         }
     }).start();
    }

}

原文链接:http://www.apkbus.com/blog-879004-62971.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消