2016-09-23 70 views
0

我有一個可生成JSON數據的彈簧引導Web服務,該服務在本地機器http://localhost:8080/demo/services中運行。由於連接超時而無法獲取JSON數據

我試圖用我的IP地址來獲取JSON數據,但它在logcat中給了以下錯誤:

09-23 15:18:27.897 2923-3540/com.example.palangou.foodie1 
    W/System.err: java.net.ConnectException: failed to connect to /192.168.1.9 
    (port 8080): connect failed: ETIMEDOUT (Connection timed out) 

相關的Java代碼:

new JSONpart().execute("http://192.168.1.9:8080/demo/services"); 

public class JSONpart extends AsyncTask<String, Void, List<POJO>> { 


    @Override 
    protected List<POJO> doInBackground(String... urls) { 
     HttpURLConnection connection = null; 
     BufferedReader reader = null; 
     StringBuffer buffer = new StringBuffer(); 
     String place = "", name = ""; 
     List<POJO> arraylist = new ArrayList<>(); 
     try { 
      URL url = new URL(urls[0]); 
      try { 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.connect(); 
       InputStream input = connection.getInputStream(); 
       reader = new BufferedReader(new InputStreamReader(input)); 

       String line = ""; 
       while ((line = reader.readLine()) != null) { 
        buffer.append(line); 
       } 

       Log.d("Recursive places",buffer.toString()); 
       String finalJSON = buffer.toString(); 
       try { 
        //JSONObject object = new JSONObject(finalJSON); 
        JSONArray array = new JSONArray(finalJSON); 

        for(int i=0;i< array.length();i++) 
        {//Log.d("Recursive places",  finalobject.getString("name")); 
         POJO pojo = new POJO(); 
         JSONObject finalobject = array.getJSONObject(i); 
         pojo.setName(finalobject.getString("service")); 
         pojo.setPlace(finalobject.getString("servicedesc")); 
         // 
         arraylist.add(pojo); 

        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       // Log.d("name", arraylist.get(1).getName().toString()); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
      ; 
     } finally { 
      connection.disconnect(); 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     // return buffer.toString(); 
     return arraylist; 
    } 

    @Override 
    protected void onPostExecute(List<POJO> s) { 
     super.onPostExecute(s); 
     // Log.d("OnPOST", s.get(1).getName().toString()); 
     FoodiesAdapater adapter = new FoodiesAdapater(getApplicationContext(),R.layout.row,s); 
     listView.setAdapter(adapter); 



    } 
} 
+0

是你的API正在工作?? –

+1

您是否連接到與您的電腦相連的同一個wifi? – user6556461

+0

檢查你的服務器是否打開! – W4R10CK

回答

1

請檢查是否您的API或網址正在工作。如果您將Json文件上傳到此http://myjson.com/並從此在線服務器獲取URL,那將會更好。

+0

你也可以檢查你的Json是否工作或不在這個鏈接 vishnumm93

+0

奇怪的是,這工作,因爲LogCat沒有顯示問題是與那。 @GouthamPalani你能在這裏解釋一下嗎? :) – Sufian

+0

@Sufian我只是用myjson.com來獲取數據。我手動上傳了我的JSON文件 –

相關問題