2017-05-30 66 views
1

我確實喜歡這樣。我想打電話給休息API。 'urlString'是url的例子。 但我什麼都看不到。沒有錯誤,沒有結果。 我該怎麼辦? (LogManager在Android監視器logcat中打印字符串)。Android HttpUrlConnection沒有錯誤,也沒有結果

btnCar.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) {    

     final String urlString = "https://apis.skplanetx.com/tmap/routes?version=1&appKey='MyPersonalAPIkey'&startX=14368651.605895586&startY=4188210.3283031476&endX=14135591.321771959&endY=4518111.822510956&reqCoordType=EPSG3857&resCoordType=EPSG3857&tollgateFareOption=8"; // url example 

     Thread thread = new Thread() { 
      @Override 
      public void run() { 
       try { 
         LogManager.printLog(urlString); 
         URL url = new URL(urlString); 

         HttpURLConnection http = (HttpURLConnection) url.openConnection(); 

         http.setDefaultUseCaches(false); 
         http.setDoInput(true); 
         http.setDoOutput(true); 
         http.setRequestMethod("POST"); 
         http.setRequestProperty("content-type", "application/x-www-form-urlencoded"); 

         OutputStreamWriter outStream = new OutputStreamWriter(http.getOutputStream(), "UTF-8"); 
         PrintWriter writer = new PrintWriter(outStream); 
         writer.flush(); 

         InputStreamReader tmp = new InputStreamReader(http.getInputStream(), "UTF-8"); 
         BufferedReader reader = new BufferedReader(tmp); 
         StringBuilder builder = new StringBuilder(); 
         String strResult; 
         while ((strResult = reader.readLine()) != null) { 
          builder.append(strResult + "\n"); 

         } 
         LogManager.printLog("result: " + builder.toString()); 


        } catch (MalformedURLException e) { 
         e.printStackTrace(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      }; 
    } 
} 

回答

0

我跑你的代碼,唯一缺少的是:

thread.start(); 

我用:

Log.d("result: ", builder.toString()); 

要顯示在logcat中收到的JSON數據。

+0

謝謝!我解決了這個問題。 – User910308

相關問題