Java PHP文件中的JSON返回垃圾
我试图从我的网站example.net/index.php获取一个JSON文件,但是我总是在Android应用程序中获取某种HTML,而不是JSON对象 返回的字符串:Java PHP文件中的JSON返回垃圾,java,php,android,json,Java,Php,Android,Json,我试图从我的网站example.net/index.php获取一个JSON文件,但是我总是在Android应用程序中获取某种HTML,而不是JSON对象 返回的字符串: <html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d)
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("674bdf26f9a5fe3df1461aafc3120641");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://example.net/index.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
如何获取JSON文件而不是此HTML代码?您应该传递内容类型
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setRequestProperty("Content-type", "application/json; charset=utf-8");
c.setUseCaches(false);
看看html源代码试图告诉您的内容: 此站点需要Javascript才能工作 您应该首先在该页面上使用javascript解释器来获取json 所以网址错了。它不提供json
如果您使用浏览器请求该页面,浏览器将在您不知情的情况下调用javascript解释器。谢谢,我尝试了,但它仍然返回相同的结果,而不是JSON。没有,但我尝试了库并得到了相同的结果。请尝试使用postman应用程序,以确保响应是否具有JSON格式,或者使用结果更新问题。另外,尝试在android中使用改型库进行API调用。
public String getJSON(String url, int timeout) {
HttpURLConnection c = null;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
} catch (MalformedURLException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setRequestProperty("Content-type", "application/json; charset=utf-8");
c.setUseCaches(false);