2017-10-22 1 views
0

Im 초심자는 Json 파싱 및 인터넷에서 스트리밍을 배우고 있습니다. 이 앱에서는 오류가 발생하지 않지만 아무 것도 표시하지 않습니다. 문제가 무엇인지 알지 못하고 로그에서 문제를 볼 수 없습니다. 여기에 코드 :Cant는 앱에서 Json 정보를 얻습니다.

InputStream is; 
String line; 
TextView textView; 




@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    textView = (TextView) findViewById(R.id.text); 
    try { 
     URL url = new URL("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&minlatitude=4&maxlatitude=5"); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     is = new BufferedInputStream(connection.getInputStream()); 
     if(connection.getInputStream()==null) 
     { 
      textView.setText("input stream empty"); 
     } 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

     StringBuilder builder = new StringBuilder(); 

     while((line=reader.readLine())!=null){ 
      builder.append(line); 
     } 
     if(builder.toString().equals("")) 
     { 
      textView.setText("no work builder empty"); 
     } 

     line=builder.toString(); 
     JSONObject object = new JSONObject(line); 
     JSONArray fea = object.getJSONArray("features"); 
     JSONObject QUAKE = fea.getJSONObject(0); 
     JSONObject pro = QUAKE.getJSONObject("properties"); 
     int mag = pro.getInt("mag"); 
     textView.setText(mag+""); 



    } catch (Exception e) { 
     e.printStackTrace(); 
    } 


} 

감사합니다!

답변

1

아래 보시기 바랍니다. 따라서 네트워크 호출은 주 스레드에서 네트워크 호출을 수행 할 때 발생하지 않으며 네트워크 호출 대신 NetworkOnMainThreadException을 직접 throw합니다. 에서 AsyncTask를 실행하십시오.

1

메인 스레드에서 네트워크 작업을하는 것은 Android에서 범죄입니다. network_operation_on_main_thread 예외로 처벌됩니다. AsyncTask에서 도움을 받아야합니다.

예외가 응용 프로그램의 주 스레드에서 네트워킹 작업을 수행하려고 할 때 발생합니다 코드

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
textView = (TextView) findViewById(R.id.text); 
new LongOperation().execute(""); 
} 


private class LongOperation extends AsyncTask<String, Void, String> { 
    String data = "input stream empty"; 
    @Override 
    protected String doInBackground(String... params) { 
     try { 
    URL url = new URL("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&minlatitude=4&maxlatitude=5"); 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
    is = new BufferedInputStream(connection.getInputStream()); 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

    StringBuilder builder = new StringBuilder(); 

    while((line=reader.readLine())!=null){ 
     builder.append(line); 
    } 
    if(builder.toString().equals("")) 
    { 
     data = "no work builder empty"; 
    } 

    line=builder.toString(); 
    JSONObject object = new JSONObject(line); 
    JSONArray fea = object.getJSONArray("features"); 
    JSONObject QUAKE = fea.getJSONObject(0); 
    JSONObject pro = QUAKE.getJSONObject("properties"); 
    int mag = pro.getInt("mag"); 
    data = mag+""; 



} catch (Exception e) { 
    e.printStackTrace(); 
} 
     return null; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
    textView.setText(data); 
    } 

    @Override 
    protected void onPreExecute() { 

    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
    } 
} 
+1

** doInBackground (..) ** 메소드에서 UI 조작을 수행 할 수 없으므로이 코드는 충돌합니다. ** onPreExecute() ** 또는 ** onPostExecute() ** 메소드 –

+0

@DeepLathia에서 수행 할 수 있습니다. 감사합니다. 업데이트 된 ANS를 확인하십시오. – Sush

관련 문제