HttpUrlConnection(GET方式)

来源:互联网 发布:ps4合金装备vb 编辑:程序博客网 时间:2024/05/29 07:36

一.
基本知识点:HttpUrlConnection有两种网络请求方式,GET和POST。下面我会把两种方式都写出来。
写之前我要说由于我不会弄服务器,所以我的链接是使用的天气预报的链接,反正本来就是来练手,不要在意太多,知道了使用方法,相信也就能举一反三。
1.GET方式:
1.1首先我们设置权限哦

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    <uses-permission android:name="android.permission.INTERNET" />

1.2我的布局文件

<RelativeLayout 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=".MainActivity">    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/edit"        />    <RadioGroup        android:layout_width="match_parent"        android:layout_height="55dp"        android:layout_below="@+id/edit"        android:orientation="horizontal"        android:id="@+id/rg"        android:weightSum="4">        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:text="①GET"            android:id="@+id/one"            android:layout_weight="1"/>        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:text="①POST"            android:id="@+id/two"            android:layout_weight="1"/>        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:text="②GET"            android:id="@+id/three"            android:layout_weight="1"/>        <Button            android:layout_width="0dp"            android:layout_height="match_parent"            android:text="②POST"            android:id="@+id/four"            android:layout_weight="1"/>    </RadioGroup>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/text"        android:layout_below="@+id/rg"/></RelativeLayout>

其中:
①GET表示HttpUrlConnection的GET方式
①POST表示HttpUrlConnection的POST方式
②的按钮不用管,还没写呢!!!
初始化那些基本步骤我就不说了
1.3使用线程,进行访问

 private class OneThread implements Runnable {        InputStream is;        BufferedReader br;        @Override        public void run() {            getPath();            try {                //通过路径得到URL对象                url = new URL(todayWeather);                //打开服务器                conn = (HttpURLConnection) url.openConnection();                //连接服务器                conn.connect();                //得到输入流                is = conn.getInputStream();                //封装                br = new BufferedReader(new InputStreamReader(is));                String line = null;                //将数据存储在StringBuffere中                StringBuffer sb = new StringBuffer();                while ((line = br.readLine()) != null) {                    sb.append(line);                }                sb.append("\n" + "\nWays:HttpUrlConnection-GET");                content = sb.toString();                //使用消息传递机制,更改主线程UI                Message message = new Message();                message.what = 1;                handler.sendEmptyMessage(message.what);            } catch (MalformedURLException e) {                e.printStackTrace();            } catch (IOException e) {                e.printStackTrace();            } finally {                try {                    if (is != null) {                        is.close();                    }                    if (br != null) {                        br.close();                    }                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }

其中getPath()方法是得到天气预报接口,将它传给todayWeather了。
代码在这:

  public void getPath() {        String todayStart = "http://api.k780.com:88/?app=weather.today&weaid=";        String todayCity = edit.getText().toString();        String todayEnd = "&&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json";        todayWeather = (todayStart + todayCity + todayEnd);    }

不在子线程中使用的话,程序会崩掉哦!!!
感觉自己代码中注释挺详细的,这里就不再解释了。
1.4由于要更改UI,即把数据放到TextView中,我们知道,更改UI是只能主线程做的哦,所以我们要使用消息传递机制,告诉主线程,你要更改UI啦,为此我们写一个类继承Handler,在里面处理。

class MyHandler extends Handler {        @Override        public void handleMessage(Message msg) {            if (msg.what == 1) {                text.setText(content);            } else {                text.setText("出错了");            }        }    }

这样我们就实现了GET方法的网络请求了,不过最近脑袋不太好使,漏掉什么的话大家多包涵吧。
POST在下一篇写吧,完整代码也在下一篇给吧(两种方法一起给出),Bye。

0 0