java调用接口传递参数_JAVA接口传递参数(POST),从接口接收数据(JSON) -----记录...

1,给接口传递json格式的数据

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import net.sf.json.JSONObject;

public class AppAddTest {

public static final String ADD_URL = "http://192.168.1.1:8080/*.controller/*.action";

public static void appadd() {

try {

URL url = new URL(ADD_URL);

HttpURLConnection connection = (HttpURLConnection) url

.openConnection();

connection.setDoOutput(true);

connection.setDoInput(true);

connection.setRequestMethod("POST");

connection.setUseCaches(false);

connection.setInstanceFollowRedirects(true);

connection.setRequestProperty("connection", "Keep-Alive");

//connection.setRequestProperty("Content-Type", "text/plain; charset=utf-8");

connection.connect();

//POST请求

DataOutputStream out = new DataOutputStream(

connection.getOutputStream());

JSONObject obj = new JSONObject();

String message = java.net.URLEncoder.encode("哈哈哈","utf-8");

你可能感兴趣的:(java调用接口传递参数)