继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Http相关(一)

白衣染霜花
关注TA
已关注
手记 126
粉丝 17
获赞 57

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

开始第一个学习任务是用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

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP