当我尝试从网站上获取JSON数据时,我可以在浏览器中看到数据,但是当我尝试通过编程实现相同时,我得到空响应http://dotsyndicate.com/unionsports/procedure.php?gc=fb1113&getMatches=1这是我想要获取的网址在我的App中获取空响应,只需将url字符串中的值从1113更改为1416即可正确显示JSON数据。 以下是我的代码
protected Void doInBackground(Void... params) {
String matchesUrl = "http://dotsyndicate.com/unionsports/procedure.php?gc=fb1113&getMatches=1"
data = new ArrayList<ListGroup>();
HttpHandler sh = new HttpHandler();
String jsonStr = sh.makeServiceCall(matchesUrl);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null||jsonStr !="") {
data = jsonExtractor.extractJson(jsonStr);
}else{
Log.e(TAG, "Response from url: null");
}
return null;
}
HttpHandler类:
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
来自应用内的网址:http://dotsyndicate.com/unionsports/procedure.php?gc=fb1113&getMatches=1的响应
E/FootBallMainFragment: Json Data is downloading
06-06 16:06:03.420 23817-24720/com.dotsyndicate.al_islah I/System.out: *** url=http://dotsyndicate.com/unionsports/procedure.php?gc=fb1113&getMatches=1
06-06 16:06:03.420 23817-24720/com.dotsyndicate.al_islah I/System.out: [CDS]rx timeout:0
06-06 16:06:03.421 23817-24720/com.dotsyndicate.al_islah I/System.out: [OkHttp] sendRequest>>
06-06 16:06:03.421 23817-24720/com.dotsyndicate.al_islah I/System.out: [OkHttp] sendRequest<<
06-06 16:06:03.851 23817-24720/com.dotsyndicate.al_islah E/FootBallMainFragment: Response from url:
06-06 16:06:03.870 23817-23817/com.dotsyndicate.al_islah D/ColorDrawable: Color = -1118482, canvas = android.view.GLES20RecordingCanvas@4422f0f, mTintMode = SRC_IN, mTint = null, ColorDrawable = android.graphics.drawable.ColorDrawable@36589616
06-06 16:06:03.871 23817-23817/com.dotsyndicate.al_islah D/ColorDrawable: Color = -13619152, canvas = android.view.GLES20RecordingCanvas@c76cb97, mTintMode = SRC_IN, mTint = null, ColorDrawable = android.graphics.drawable.ColorDrawable@17401d0b
来自应用内的网址:http://dotsyndicate.com/unionsports/procedure.php?gc=fb1416&getMatches=1的响应
E/FootBallMainFragment: Json Data is downloading
06-06 16:04:55.988 23817-24044/com.dotsyndicate.al_islah I/System.out: *** url=http://dotsyndicate.com/unionsports/procedure.php?gc=fb17&getMatches=1
06-06 16:04:55.994 23817-24044/com.dotsyndicate.al_islah I/System.out: [CDS]rx timeout:0
06-06 16:04:55.996 23817-24044/com.dotsyndicate.al_islah I/System.out: [OkHttp] sendRequest>>
06-06 16:04:55.999 23817-24044/com.dotsyndicate.al_islah I/System.out: [OkHttp] sendRequest<<
06-06 16:04:56.386 23817-24044/com.dotsyndicate.al_islah E/FootBallMainFragment: Response from url: {"2017-06-08":{"11|2:1|4:1|0":"REAL MADRID|ZERU|23:30|0|0","12|6:2|8:2|0":"AL AHLY|JUVENTUS|23:55|0|0"},"2017-06-07":{"9|1:1|3:1|0":"ATHLETICO|JEMTEM|23:30|0|0","10|5:2|7:2|0":"PREDATORS|JB|23:55|0|0"},"2017-06-06":{"8|6:2|7:2|0":"AL AHLY|JB|23:55|0|0","7|2:1|3:1|0":"REAL MADRID|JEMTEM|23:30|0|0"},"2017-06-01":{"6|8:2|5:2|0":"JUVENTUS|PREDATORS|23:55|0|0","5|4:1|1:1|0":"ZERU|ATHLETICO|23:30|0|0"},"2017-05-31":{"4|7:2|8:2|0":"JB|JUVENTUS|23:55|0|0","3|3:1|4:1|0":"JEMTEM|ZERU|23:30|0|0"},"2017-05-30":{"2|5:2|6:2|0":"PREDATORS|AL AHLY|23:55|0|0","1|1:1|2:1|0":"ATHLETICO|REAL MADRID|23:30|0|0"}}
它有点令人困惑,因为两个url都正确响应,但只有一个在App内部工作。无法弄清楚为什么会如此
答案 0 :(得分:0)
我认为问题在于在下面的陈述中阅读数据。
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
你正在追加&#39; \ n&#39;在每一行......我都不明白这个用法,你应该从这里删除.append('\n')
后尝试。
答案 1 :(得分:0)
终于找到了问题。
问题来自服务器端。在用户使用 HttpURLConnection 请求时,它会发送null useragent,这就是服务器没有正确响应的原因。
只需在初始化HttpURLConnection后添加自定义useragent即可解决问题。
conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71");