android http post

android  http post

 

 

private void sendLocationToServer(double latitude, double longitude) {

        String tagID="xcvxczvxzvcxz";
        String name="";
        String biaoduan="";
        String type1="";
        String postData ="tagID="+tagID+"&name="+name+"&biaoduan="+biaoduan+"&type1="+type1+"&longitude="+longitude+"&latitude="+latitude+"&WatchdogCount="+WatchdogCount+"";

        String urlString = "http://117.34.47.236:8080/bjcj/dsfsadfasdsfasdfdsa.action?"+postData;


        Log.d("111:",urlString);


        try {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            //connection.setDoOutput(true);
            //connection.setDoInput(true);

            // 设置请求头信息
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Accept", "application/json");
            connection.setRequestProperty("User-Agent", "Your-App-Name/Version");

            int  respaonseCode=connection.getResponseCode();
            Log.d("222:",respaonseCode+"");

            TextView textView6=findViewById(R.id.textView6);
            textView6.setText(urlString);







            // 关闭连接
            connection.disconnect();

        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

 

 

该方法不可放在主程序里面,否则运行报错,建议用okhttp

###############################

posted @ 2024-04-14 08:35  西北逍遥  阅读(1)  评论(0编辑  收藏  举报