android http post

西北逍遥發表於2024-04-14

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

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

相關文章