2017-06-06 127 views
0

當我嘗試從網站獲取JSON數據時,我可以在瀏覽器中看到數據,但是當我嘗試通過編程來做同樣的事情時,我得到空的響應http://dotsyndicate.com/unionsports/procedure.php?gc=fb1113&getMatches=1這是我嘗試的url取我在我的應用程序中獲得空響應,只需將url字符串中的值從1113更改爲1416,即可正確看到JSON數據。 下面是我的代碼獲取空響應,而獲取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類:應用程序

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"}} 
http://dotsyndicate.com/unionsports/procedure.php?gc=fb1416&getMatches=1http://dotsyndicate.com/unionsports/procedure.php?gc=fb1113&getMatches=1該應用從URL

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 = [email protected], mTintMode = SRC_IN, mTint = null, ColorDrawable = [email protected] 
06-06 16:06:03.871 23817-23817/com.dotsyndicate.al_islah D/ColorDrawable: Color = -13619152, canvas = [email protected], mTintMode = SRC_IN, mTint = null, ColorDrawable = [email protected] 

響應內:從URL

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(); 
    } 
} 

響應

它有點混亂,因爲這兩個url都正確響應,但只有一個在應用程序內工作。不知道爲什麼這樣

+0

讓我告訴你一些事情,那個JSON是可怕的,你應該問一個更好的格式化JSON響應,讓你的生活更輕鬆。 –

+0

這只是一個二維數組編碼的JSON格式永遠不會少,唯一的問題是我無法弄清楚爲什麼我的應用程序不會識別第一個url的響應@JonathanAste –

回答

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"); 
0

我想問題是在讀取數據在下面的語句。

while ((line = reader.readLine()) != null) { 
       sb.append(line).append('\n'); 
      } 

你在每一行追加「\ n」 ......我不明白利用這一點,你應該從這裏取出.append('\n')後再試。

+0

沒有變化,仍然從url空響應 –