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
###############################
QQ 3087438119