Android get,post 方式请求json数据

本博客内容

  1. 实现get方式请求天气数据,请求到数据后的具体操作未写(如转为JSONObject)
  2. 实现post方式请求手机号的信息,同样,请求到数据后的具体操作未写(如转为JSONObject)

get方式请求json数据

MainActivity.java // 写

public String acceptData;  //定义接受json数据信息的变量

//处理返回结果的函数,系统提供的类方法  //handler处理返回数据, 此方法,我写在onCreate()函数外。
        Handler handler = new Handler(){  //此函数是属于MainActivity.java所在线程的函数方法,所以可以直接条调用MainActivity的 所有方法。
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==0x01){   //
                    //System.out.println("handleMessage():"+acceptData);
                    //可调用  Toast.makeText(MainActivity.this,"返回内容是:"+acceptData,Toast.LENGTH_SHORT)).show();    来显示到UI 
                        //每次数据读取完毕,将acceptData置空。 具体原因,当下还未理解
                        acceptData = "";  //
                    }else{
                        Toast.makeText(MainActivity.this, "请重新输入地址:", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        };

//发送天气请求,在其他地方进行调用
    public void SendGetRequest(final String url,final String content) {
        new Thread(){
            @Override
            public void run() {
                //例如String pathString =  "http://wthrcdn.etouch.cn/weather_mini?city="+"北京";  //请求天气信息
                String pathString = url + content;
                HttpURLConnection connection;
                try {
                    URL url = new URL(pathString);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.connect();
                    //接受数据
                    if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                        InputStream inputStream = connection.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
                        String line;
                        while((line=bufferedReader.readLine())!=null){ //不为空进行操作
                            acceptData+=line;
                        }
                        System.out.println("接受到的数据:"+acceptData);
                        handler.sendEmptyMessage(0x01);  //请求完毕,返回自己自定义的信息 id 
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

post方式请求json数据

MainActiviti.java

//注:handler函数一样,所以只写对请求头进行部分修改。

//请求数据
    public void sendRequestPost(final String url,final String phoneNumber){
        new Thread(){
            @Override
            public void run() {
            //如可以使用  String pathString = "https://way.jd.com/jisuapi/query4";
                String pathString = url;
                try {
                    URL url = new URL(pathString);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setUseCaches(false);
                    connection.setInstanceFollowRedirects(true); //有误重定向
                    connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
                    OutputStream outputStream = connection.getOutputStream();
                    PrintStream printStream = new PrintStream(outputStream);
                    //例如这样写入参数  // phoneNumber为外部传入的手机号 后面为密匙
                    String  submitData = "shouji="+phoneNumber+"&appkey=343c652c4127935f0b1f2ded76c6c68a";

                    printStream.print(submitData); //向服务器提交数据
                    //接受数据
                    if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
                        InputStream inputStream = connection.getInputStream();
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
                        String line;
                        while((line=bufferedReader.readLine())!=null){ //不为空进行操作
                            acceptData+=line;
                        }
                        handler.sendEmptyMessage(0x01); //向主线程返送
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

猜你喜欢

转载自blog.csdn.net/qq_38340601/article/details/81837660