네트워크를 통해 파일을 읽고 반환 된 텍스트를 Fragment의 TextView 위젯에 할당하려고합니다. 이 파일은 ManActivity의 메소드에 의해 호출 된 후에 Async 스레드에서 읽혀집니다.)네트워크의 파일에서 읽은 텍스트를 Android의 TextView 조각
MainActivity.java (relevant part):
public void myClickHandler(View view){
ConnectivityManager connmngr=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo=connmngr.getActiveNetworkInfo();
MainActivity ma=new MainActivity();
if(networkinfo!=null && networkinfo.isConnected()){
new Fragment_2().new DownloadWebpageTask(ma).execute(stringurl);
}else{
textview.setText("No network connection available");
}
}
}
//Called by the OnPostExecute() method
public void displayText(String result){
DateFormat df=DateFormat.getDateInstance(DateFormat.LONG);
java.util.Date d=new java.util.Date();
String display=result + " " + df.format(d);
Log.d("DOWNLOAD",display);
textview=(TextView)findViewById(R.id.web_text);
textview.setText(display);
}
Fragment_2.java
public class Fragment_2 extends Fragment {
private static final String DEBUG_TAG="HttpExample";
MainActivity ma;
String result;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_2, container, false);
TextView textview=(TextView)rootView.findViewById(R.id.web_text);
textview.setText(result);
return rootView;
}
//Call AsyncTask to create a new, background network thread which is separate from the UI thread
public class DownloadWebpageTask extends AsyncTask<String,String,String>{
MainActivity ma;
DownloadWebpageTask(MainActivity ma){
this.ma=ma;
}
@Override
protected String doInBackground(String... urls) {
try{
result=downloadUrl(urls[0]);
}catch(Exception e){
e.printStackTrace();
result="Exception while reading the text";
}
return result;
}
@Override
protected void onPostExecute(String result){
ma.displayText(result);
}
//downloadUrl fetches and processes the web page content. After processing, it returns a result string.
private String downloadUrl(String myurl) throws IOException{
InputStream is=null;
String str=null;
try{
URL url=new URL(myurl);
Log.d("URL:",url.toString());
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setReadTimeout(20000);
conn.setConnectTimeout(30000);
conn.setRequestMethod("GET");
conn.setDoInput(true);
Log.d("TRY DOWNLOAD","Inside try block of downloadUrl");
//Start the query
conn.connect();
int response=conn.getResponseCode();
Log.d(DEBUG_TAG,"The response is : " + response);
BufferedReader in=new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder sb=new StringBuilder();
while((str=in.readLine())!=null){
sb.append(str);
}
return sb.toString();
}finally{
//input stream should be closed after the app has finished using it
if(is!=null){
is.close();
}
}
}
}
}
onPostExecute을합니다 (MainActivity에 해당 DisplayText() 메서드를 호출 코드는 아래와 같습니다. 그러나, 텍스트 조각에 textview에 나타나야합니다. 그래서 Fragment_2.java의 onCreateView() 메소드에서 코드를 변경해야합니다.
하지만 정확히 어떻게 해야할지 모르겠습니다. 도와주세요.
를 해결 희망; MainActivity.java의 displayText 중 – user2994590
onCreate()에서 setContentView를 사용 했습니까? – VM4
예 ..하지만 그것은 setContentView (R.layout.activity_main)입니다. – user2994590