그래서, 나는 클라이언트를위한 어플리케이션을 만들고 네트워크 어플리케이션에 참여했습니다. 현재 MainThreadException을받지 못하도록 manifest에서 targetSDK 태그를 제거하고 있습니다. 사람들과 설명서를 보았습니다. 네트워크 작업이 느려지는 것처럼 메인 쓰레드가 아닌데 ...메인 스레드에서 안드로이드 네트워크 사용
질문 : 메인 쓰레드에서 네트워크 작업을 수행하지 않고 어디서 어떻게합니까? 당신은 클래스 AsyncTask의 사용을해야
public static class MagFragment extends Fragment {
public static final String ARG_MAGAZINE_NUMBER = "magazine_number";
public MagFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int i = getArguments().getInt(ARG_MAGAZINE_NUMBER);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
if(i != 0){
rootView = inflater.inflate(R.layout.fragment_magazine, container, false);
TextView magText = (TextView) rootView.findViewById(R.id.textViewMag);
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://csddata.site11.com");
HttpResponse response = httpclient.execute(httppost);
HttpEntity httpEntity = response.getEntity();
InputStream inputstream = httpEntity.getContent();
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream,"iso-8859-1"),8);
magText.setText(reader.readLine());
}catch (Exception e){
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String magazine = getResources().getStringArray(R.array.magazine_array)[i];
Log.d("MagFragment", "MagNumber" + i);
getActivity().setTitle(magazine);
return rootView;
}
}
다음 내용을 읽었습니까? http://developer.android.com/training/basics/network-ops/connecting.html –
다음 큰 일을하고 있다면 다음을 고려하십시오. http://developers.google. com/live/shows/474338138/;-)- –
나는 스레드 클래스를 만들고, 객체를 만들고, 필요한 변수를 전달하고 시작을 호출하는 작업을 수행했습니다. 스레드에 대한 자세한 내용 http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html – LanternMike