问题描述
- Android 向远程网站发送数据问题,变量和常量的区别。求大神解答。。。
-
Button button = (Button) findViewById(R.id.button); button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { // boolean flag = sendUserInfo(); Map<String, String> params = new HashMap<String, String>(); String tmp1 = cardInfo;//cardInfo为在程序中的变量 String tmp2 = locationInfo;//locationInfo为在程序中的变量 // 发送上边两个cardInfo、locationInfo就不行,发送下边的常量"123"和"234"就可以 // String tmp1 = "123"; // String tmp2 = "234"; params.put("sj", tmp1); params.put("dw", tmp2); boolean flag = false; try { flag = sendPOSTRequest(path, params, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } Toast.makeText(MainActivity.this, flag+"t"+locationInfo, Toast.LENGTH_LONG).show(); } }); public static Boolean sendPOSTRequest(String path,Map<String, String> params, String encoding) throws Exception { StringBuilder url = new StringBuilder(path); if(params != null && params.size() > 0){ url.append('&'); for (Map.Entry<String, String> entry : params.entrySet()) { url.append(entry.getKey()).append("="); url.append(URLEncoder.encode(entry.getValue(), encoding));// 编码 url.append('&'); } url.deleteCharAt(url.length() - 1); } Log.e("url",url.toString()); HttpURLConnection connection = (HttpURLConnection) new URL(url.toString()).openConnection();// 基于HTTP协议连接对象 connection.setConnectTimeout(5000); connection.setRequestMethod("POST"); if (connection.getResponseCode() == 200) { return true; }else{ return false; } }
时间: 2024-09-24 11:16:55