java 怎么调用php的api接口

Python018

java 怎么调用php的api接口,第1张

import java.io.BufferedInputStream

import java.io.BufferedOutputStream

import java.io.BufferedReader

import java.io.InputStreamReader

import java.net.HttpURLConnection

import java.net.URL

import java.nio.charset.Charset

import XmlHelper

public class QXOutStream {

public String outPutStr(String urlStr, String input) throws Exception{

StringBuffer strBuf = new StringBuffer()

String Resulst=""

try{

URL url = new URL(urlStr)

HttpURLConnection con = (HttpURLConnection)url.openConnection()

con.setDoInput(true)

con.setDoOutput(true)

con.setRequestMethod("POST")

con.setAllowUserInteraction(false)

con.setUseCaches(false)

con.setRequestProperty("Accept-Charset", "GBK")

BufferedOutputStream bufOutPut = new BufferedOutputStream(con.getOutputStream())

byte[] bdat = input.getBytes("UTF-8")//解决中文乱码问题

bufOutPut.write(bdat, 0, bdat.length)

bufOutPut.flush()

BufferedInputStream inp = new BufferedInputStream(con.getInputStream())

InputStreamReader in = new InputStreamReader(inp,Charset.forName("GBK"))

BufferedReader bufReador = new BufferedReader(in)

String tempStr = ""

while (tempStr != null) {

strBuf.append(tempStr)

tempStr = bufReador.readLine()

}

Resulst = XmlHelper.getPostNodeText(strBuf.toString(), "OPERATOR_RESULT")//.getPostFirstRowText(strBuf.toString(), "OPERATOR_RESULT")

}

catch (Exception e) {

//System.err.println("Exception:"+e.toString())

throw e

//return "N"

}

finally{

return Resulst

}

}

}

你可以参考这个例子调用php 的api接口,这里面的urlStr就是你调用php的api url接口

接口返回的参数格式一般是由客户端的需要来设置,至于你说的这些,一般是封装成一个对象,然后将对象转换成Json字符串返回,客户端接收到Json字符串后,再转换成对象来解析需要的信息就可以了。

这跟java无关,WebService哪种语言开发的都可以,php都是一样调用

调用方法网上很多例子,就不搬运了:http://www.cnblogs.com/xjnotxj/p/6212143.html