安卓HttpURLConnection 进行http请求(传递数据 获取数据 主线程禁止网络请求)以get方式为例

Posted m0_49558200

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓HttpURLConnection 进行http请求(传递数据 获取数据 主线程禁止网络请求)以get方式为例相关的知识,希望对你有一定的参考价值。

注意:

1.安卓主线程禁止联网操作,如果在主线程中使用HttpURLConnection需通过new Thread()在新的线程中使用。

2.使用HttpURLConnection时必须设计异常处理。

3.get方式无法传递中文参数,如果需要传递中文参数则需要在前台和后台分别进行编解码                操作:get 方式传递中文_fade-CSDN博客

1.创建URL对象,并传递参数

String path="http://xxx.xx.xxx.xx/xxxxx?x=52&y=4554"
URL url = new URL(path);

在get方式中,如果需要传递参数,格式如上所示。每个参数之间用&分割。问号后表示向服务器传递两个参数x和y,x=52,y=4554。如果参数的数据不是固定的,可以用“xxxx=”+变量名的方式设置path字符串。如

String path="http://xxx.xx.xxx.xx/xxxxx?"+"x="+x+"&"+"y="+y;

2.获取HttpURLConnection的对象实例

HttpURLConnection conn=(HttpURLConnection)url.openConnection();

3.设置请求方式与其他参数

conn.setRequestMethod("GET");
conn.setConnectTimeout(6000);//超时时间
conn.setReadTimeout(6000);//响应时间

4.获取输入流,并进行格式转换

StringBuffer buffer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line = "";
while ((line = reader.readLine()) != null) 
    buffer.append(line);
//输入流转字符流
reader.close();//关闭输入流
Log.i("x",buffer.toString());//输出测试

5.关闭http连接

conn.disconnect();

6.设置android权限

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

完整代码如下

public void onClick(View view) 
                new Thread()
                    @Override

                    public void run() 
                        try 
                            int x=800;
                            int y=2500;
                            String path="http://xxx.xx.xxx.xx/xxxxx?"+"x="+x+"&"+"y="+y;
                            URL url = new URL(path);
                            HttpURLConnection conn=(HttpURLConnection)url.openConnection();

                            conn.setRequestMethod("GET");
                            conn.setConnectTimeout(6000);//超时时间
                            conn.setReadTimeout(6000);//响应时间

                           
                            StringBuffer buffer = new StringBuffer();
                            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
                            String line = "";
                            while ((line = reader.readLine()) != null) 
                                buffer.append(line);
                            
                            reader.close();
                            Log.i("x",buffer.toString());


                        
                        catch(MalformedURLException e)
                            e.printStackTrace();
                        
                        catch (IOException e) 
                            e.printStackTrace();
                        

                    

                .start();

            

以上是关于安卓HttpURLConnection 进行http请求(传递数据 获取数据 主线程禁止网络请求)以get方式为例的主要内容,如果未能解决你的问题,请参考以下文章

安卓必备,HttpUrlConnection

HttpURLConnection和HttpClient的区别

通过HttpURLConnection模拟post表单提交

安卓应用的HTTP请求方式:Apache HTTP Client和HttpURLConnection

HttpURLConnection responseCode 405 方法不允许错误

HTTP基础与Android之(安卓与服务器通信)——使用HttpClient和HttpURLConnection