如何实现返回json数据的的api,语言是html5与PHP都行。技术问题,不懂勿回答,最好有实例文件。
展开全部
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpRequestUtil {
/**
* 从url请求中获得返回的字符串
*
* @param requestUrl
* @return JSON字符串
*/
public static String HttpRequest(String requestUrl) {
StringBuffer sb = new StringBuffer();
InputStream ips = getInputStream(requestUrl);
InputStreamReader isreader = null;
try {
isreader = new InputStreamReader(ips, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(isreader);
String temp = null;
try {
while ((temp = bufferedReader.readLine()) != null) {
sb.append(temp);
}
bufferedReader.close();
isreader.close();
ips.close();
ips = null;
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 从请求的URL中获取返回的流数据
* @param requestUrl
* @return InputStream
*/
private static InputStream getInputStream(String requestUrl) {
URL url = null;
HttpURLConnection conn = null;
InputStream in = null;
try {
url = new URL(requestUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
in = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
}
URL中传的汉字要用UTF-8编码,程序中用你自己的令牌,就可以了
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class HttpRequestUtil {
/**
* 从url请求中获得返回的字符串
*
* @param requestUrl
* @return JSON字符串
*/
public static String HttpRequest(String requestUrl) {
StringBuffer sb = new StringBuffer();
InputStream ips = getInputStream(requestUrl);
InputStreamReader isreader = null;
try {
isreader = new InputStreamReader(ips, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(isreader);
String temp = null;
try {
while ((temp = bufferedReader.readLine()) != null) {
sb.append(temp);
}
bufferedReader.close();
isreader.close();
ips.close();
ips = null;
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 从请求的URL中获取返回的流数据
* @param requestUrl
* @return InputStream
*/
private static InputStream getInputStream(String requestUrl) {
URL url = null;
HttpURLConnection conn = null;
InputStream in = null;
try {
url = new URL(requestUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
in = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
return in;
}
}
URL中传的汉字要用UTF-8编码,程序中用你自己的令牌,就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询