java 怎么调用php的api接口 5
2个回答
展开全部
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接口
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接口
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询