2017-09-15 24 views
-4

我正在學習如何在android中進行api調用。這是我的片段。我已經在myjson.com上傳了這個json文件。從我的片段類中,我嘗試着打api。我相信響應應該將json對象內的數據返回到我的日誌中。API調用android.os.NetworkOnMainThreadException

public class TableFragment extends android.support.v4.app.Fragment { 

public TableFragment() { 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.fragment_table, container, false); 

    Button api_btn = (Button)rootView.findViewById(R.id.enter_button); 
    api_btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      try { 
       URL url = new URL("https://api.myjson.com/bins/n80yl"); 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
       try { 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
        StringBuilder stringBuilder = new StringBuilder(); 
        String line; 
        while ((line = bufferedReader.readLine()) != null) { 
         stringBuilder.append(line).append("\n"); 
        } 
        bufferedReader.close(); 
        Log.d("tagg", "onClick: "+stringBuilder); 
       }catch (Exception e) 
       { 
        Log.d("tagg", "onClick:exception1 "+e); 
       } 
       finally{ 
        urlConnection.disconnect(); 
       } 
      } 
      catch(Exception e) { 
       Log.d("tagg", "doInBackground:exception2 "+e); 

      } 
     } 
    }); 


    return rootView; 
} 

} 

我給出了清單中的Internet權限。

這是正確的方式,以便我可以學習android中的api機制。 在此先感謝

+1

這足以執行內部S.O.一些搜索這個問題是Android的基礎知識。看看這裏https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception –

回答

0

當應用程序嘗試在其主線程上執行聯網操作時引發的異常。 ref

所以,你需要移動到移動到一個後臺進程(線程):

最好的辦法是使用AsyncTask

的AsyncTask能夠正確且容易使用的UI線程。該類允許您執行後臺操作並在UI線程上發佈結果,而無需操作線程和/或處理程序。

因此,如果您將此代碼移動到AsyncTask它應該工作。

URL url = new URL("https://api.myjson.com/bins/n80yl"); 
       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
       try { 
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); 
        StringBuilder stringBuilder = new StringBuilder(); 
        String line; 
        while ((line = bufferedReader.readLine()) != null) { 
         stringBuilder.append(line).append("\n"); 
        } 
        bufferedReader.close(); 
        Log.d("tagg", "onClick: "+stringBuilder); 
       }catch (Exception e) 
       { 
        Log.d("tagg", "onClick:exception1 "+e); 
       } 
       finally{ 
        urlConnection.disconnect(); 
       } 
      } 
      catch(Exception e) { 
       Log.d("tagg", "doInBackground:exception2 "+e); 

      } 

不要忘記Internet權限添加到您的Manifest.xml文件

<uses-permission android:name="android.permission.INTERNET" /> 
+0

我這樣做我點擊我已經給它公共無效onClick(查看v){ getContext( ).startService(new Intent(getContext(),RetrieveFeedTask .class)); } –

+0

如何將意圖傳遞給RetrieveFeedTask類 –

+0

然後更新其中的問題和代碼。 –

相關問題