Java类
package voteHandler.Org.RSPSVote;
/*
* Author: Sieu Phan
* Website: RSPS Vote
* Data: 5/7/2014
* Version: 1.1
*/
import java.io.IOException;
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
public class voteHandler {
private static String SERVER = "";
private static String API_KEY = "";
public voteHandler(String SERVER_TO_SET, String API_KEY_TO_SET)
{
SERVER = SERVER_TO_SET;
API_KEY = API_KEY_TO_SET;
}
/*
Sends a GET request to RSPS Vote API.
RETURNS: a boolean
*/
public static boolean authenticate(String AUTH)
{
String url = generateURL(AUTH);
try {
String getJSON = IOUtils.toString(new URL(url));
JSONObject message = (JSONObject) JSONValue.parseWithException(getJSON);
String messageData = (String) message.get("MESSAGE");
System.out.println(messageData);
int responseData = Integer.parseInt((String) message.get("RESPONSE"));
System.out.println(responseData);
if(responseData == 5)
return true;
} catch (IOException | ParseException e) {
e.printStackTrace();
}
return false;
}
public static boolean getReward(String AUTH)
{
String url = generateURL(AUTH);
return false;
}
/*
Generates the url to the API
RETURNS: a String
*/
private static String generateURL(String AUTH)
{
String URL = "http://rspsvote.com/widget/api.php?"
+ "action=check"
+ "&server=" + SERVER
+ "&key=" + API_KEY
+ "&auth=" + AUTH;
return URL;
}
}
测试类
import voteHandler.Org.RSPSVote.voteHandler;
public class testing {
public static void main(String args[])
{
voteHandler vote = new voteHandler("l6DZw", "ELWXQ-MI4BC-3K1RT-VUKPT-DGL2U");
vote.authenticate("Ptpz1n");
}
}
从网站返回此JSON格式有什么问题?
这也是我从日食中得到的错误:
Unexpected character (<) at position 63.
at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at org.json.simple.JSONValue.parseWithException(JSONValue.java:78)
at voteHandler.Org.RSPSVote.voteHandler.authenticate(voteHandler.java:34)
at testing.main(testing.java:8)
答案 0 :(得分:2)
从网站返回此JSON格式有什么问题?
它不会返回JSON。响应的内容类型为text/html
,正文包含
{"RESPONSE":"7","MESSAGE":"AUTHENTICATION WAS UNSUCCESSFUL."}
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->
您需要摆脱XML或在JSON之后停止解析。