接口返回数据是一条数据和一个数组的区别
2个回答
展开全部
java中的接口是一种特殊的类,使用关键字interface创建。接口功能完全实现后,可以打成jar包,提供给其他公司使用。
要返回json格式数据,可以把接口中抽象方法的返回值类型规定为JSONObject或JSONString类型。这样当其他公司调用时,得到的数据就是json数据了。
另外,以jar形式提供的接口,可以通过反编译得到你的源码,如果你不希望开源,就要加密了。
要返回json格式数据,可以把接口中抽象方法的返回值类型规定为JSONObject或JSONString类型。这样当其他公司调用时,得到的数据就是json数据了。
另外,以jar形式提供的接口,可以通过反编译得到你的源码,如果你不希望开源,就要加密了。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
api url stock/me
with token:WudYqKDKzijeMcrmYcP.qFiGgIavFs0u
response:{"desc":"","name":"1460630091572","balance":100000000,"sex":"","nickName":"1460630091572","logo":"http://115.28.189.219/player_icon/19.png","watched":0,"phone":""}
public static void getMe() {
try{
String getMe_URL = "http://115.28.189.219:9898/stock/me?access_token="+access_token;
//创建连接
URL url = new URL(getMe_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
//connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
connection.connect();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
////////解析返回数据
String retString = sb.toString();
JSONObject retObject = JSONObject.fromObject(retString);
System.out.println(retObject.getString("balance"));
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
例2:
public static void myInvestment() {
try{
String myInvestment_URL = "http://115.28.189.219:9898/stock/products?access_token="+access_token;
//创建连接
URL url = new URL(myInvestment_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
//connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
connection.connect();
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
////////解析返回数据
String retString = sb.toString();
// JSONObject retObject = JSONObject.(retString);
JSONArray authorJsonArray = JSONArray.fromObject(retString);
for(int i = 0; i < authorJsonArray.size();i++)
{
JSONObject retObject0 = authorJsonArray.getJSONObject(i);
System.out.println(retObject0.getString("desc"));
}
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
解析返回数据部分是区别,返回数组时,应按照例2写
with token:WudYqKDKzijeMcrmYcP.qFiGgIavFs0u
response:{"desc":"","name":"1460630091572","balance":100000000,"sex":"","nickName":"1460630091572","logo":"http://115.28.189.219/player_icon/19.png","watched":0,"phone":""}
public static void getMe() {
try{
String getMe_URL = "http://115.28.189.219:9898/stock/me?access_token="+access_token;
//创建连接
URL url = new URL(getMe_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
//connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
connection.connect();
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
////////解析返回数据
String retString = sb.toString();
JSONObject retObject = JSONObject.fromObject(retString);
System.out.println(retObject.getString("balance"));
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
例2:
public static void myInvestment() {
try{
String myInvestment_URL = "http://115.28.189.219:9898/stock/products?access_token="+access_token;
//创建连接
URL url = new URL(myInvestment_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
//connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");
connection.connect();
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//读取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
}
System.out.println(sb);
////////解析返回数据
String retString = sb.toString();
// JSONObject retObject = JSONObject.(retString);
JSONArray authorJsonArray = JSONArray.fromObject(retString);
for(int i = 0; i < authorJsonArray.size();i++)
{
JSONObject retObject0 = authorJsonArray.getJSONObject(i);
System.out.println(retObject0.getString("desc"));
}
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
reader.close();
// 断开连接
connection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
解析返回数据部分是区别,返回数组时,应按照例2写
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询