2016-03-22 8 views
-1

urlからテキストファイルを読み取り、textviewに表示する以下のアンドロイドアプリがあります(下記参照)。今度は、bt.executeで使用されているURLアドレスがハードコーディングではなくユーザー入力によって与えられるようにコードを変更する必要があります。つまり、アプリケーションの起動時に、ユーザーにURLアドレスを与えるよう要求し、このアプリは、実際のデバイスで使用するためのものです。ユーザーが指定したアドレスにURL接続する方法

アドバイスをいただきありがとうございます。

package com.example.kaytto.main; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.text.method.ScrollingMovementMethod; 
import android.view.Menu; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
public class MainActivity extends Activity { 
private Activity context; 
private ProgressDialog pd; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    context=this; 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

public void onStart(){ 
    super.onStart(); 

    BackTask bt=new BackTask(); 
    bt.execute("http://123.12.22.32/tmp/text.txt"); 
} 

//background process to download the file from internet 
private class BackTask extends AsyncTask<String,Integer,Void>{ 
    String text=""; 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     //display progress dialog 
     pd = new ProgressDialog(context); 
     pd.setTitle("Reading alarms"); 
     pd.setMessage("Please wait..."); 
     pd.setCancelable(true); 
     pd.setIndeterminate(false); 
     pd.show(); 
    } 

    protected Void doInBackground(String...params){ 
     URL url; 
     int lines = 0; 
     try { 
      //create url object to point to the file location on internet 
      url = new URL(params[0]); 
      //make a request to server 
      HttpURLConnection con=(HttpURLConnection)url.openConnection(); 
      //get InputStream instance 
      InputStream is=con.getInputStream(); 
      //create BufferedReader object 
      BufferedReader br=new BufferedReader(new InputStreamReader(is)); 
      String line; 
      //read content of the file line by line 
      while((line=br.readLine())!=null){ 
       if(++lines > 1900) 
        text+=line + "\n"; 
      } 

      br.close(); 

     }catch (Exception e) { 
      e.printStackTrace(); 
      //close dialog if error occurs 
      if(pd!=null) pd.dismiss(); 
     } 
     return null; 
    } 

    protected void onPostExecute(Void result){ 
     //close dialog 
     if(pd!=null) 
      pd.dismiss(); 
     TextView txtview = (TextView) findViewById(R.id.text_view); 
     txtview.setMovementMethod(ScrollingMovementMethod.getInstance()); 
     //display read text in TextView 
     txtview.setText(text); 
    } 
} 
} 

答えて

1

は完全なソリューションです。

レイアウトファイル

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editext" 
     android:gravity="center_horizontal" 
     android:text="http://123.12.22.32/tmp/text.txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

    <Button 
    android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Load" 
     android:layout_below="@+id/editext" 
     android:layout_alignRight="@+id/editext" 
     android:layout_alignEnd="@+id/editext" /> 
</RelativeLayout> 

活動ファイル

package com.example.kaytto.main; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.text.method.ScrollingMovementMethod; 
import android.view.Menu; 
import android.widget.TextView; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
public class MainActivity extends Activity { 
private Activity context; 
private ProgressDialog pd; 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    context=this; 
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     BackTask bt=new BackTask(); 
    EditText text=(EditText)findViewById(R.id.editext); 
    bt.execute(text.getText().toString()); 
    } 
}); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 



//background process to download the file from internet 
private class BackTask extends AsyncTask<String,Integer,Void>{ 
    String text=""; 
    protected void onPreExecute(){ 
     super.onPreExecute(); 
     //display progress dialog 
     pd = new ProgressDialog(context); 
     pd.setTitle("Reading alarms"); 
     pd.setMessage("Please wait..."); 
     pd.setCancelable(true); 
     pd.setIndeterminate(false); 
     pd.show(); 
    } 

    protected Void doInBackground(String...params){ 
     URL url; 
     int lines = 0; 
     try { 
      //create url object to point to the file location on internet 
      url = new URL(params[0]); 
      //make a request to server 
      HttpURLConnection con=(HttpURLConnection)url.openConnection(); 
      //get InputStream instance 
      InputStream is=con.getInputStream(); 
      //create BufferedReader object 
      BufferedReader br=new BufferedReader(new InputStreamReader(is)); 
      String line; 
      //read content of the file line by line 
      while((line=br.readLine())!=null){ 
       if(++lines > 1900) 
        text+=line + "\n"; 
      } 

      br.close(); 

     }catch (Exception e) { 
      e.printStackTrace(); 
      //close dialog if error occurs 
      if(pd!=null) pd.dismiss(); 
     } 
     return null; 
    } 

    protected void onPostExecute(Void result){ 
     //close dialog 
     if(pd!=null) 
      pd.dismiss(); 
     TextView txtview = (TextView) findViewById(R.id.text_view); 
     txtview.setMovementMethod(ScrollingMovementMethod.getInstance()); 
     //display read text in TextView 
     txtview.setText(text); 
    } 
} 
} 
+0

ありがとうございました!それは完璧な答えでした!私はまだ私のレイアウトで少し作業する必要がありますが、そうでなければあなたのコードは完璧に動作します。 – Vallu

+0

私の喜び。答えを受け入れるか自由にアップアップしてください。 :) – Darish

+0

私は試しましたが、私はまだ評判スコアのためにそれを行うことはできません – Vallu

1

EditTextとButtonを使用してURLを取得します。

bt.execute(etEditTextName.getText().toString()); 
1

あなたは、以下のようにのEditTextとレイアウトが含まれているアクティビティを作成することができます。ボタンをクリックしたときに、このような()メソッドを.executeするためのEditText値を設定します。これはLauncherアクティビティである必要があります。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/editext" 
     android:gravity="center_horizontal" 
     android:text="Test URL" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Fetch" 
     android:layout_below="@+id/editext" 
     android:layout_alignRight="@+id/editext" 
     android:layout_alignEnd="@+id/editext" /> 
</RelativeLayout> 

ユーザーが[送信]ボタンをクリックすると、テキスト値を取得してputExtras経由で渡して、次のアクティビティで使用できます。

詳細については、How to pass edit text data in form of string to next activity?をご確認ください。

1

レイアウトに編集テキストとボタンを作成し、開始時のコードをボタンのOnClickにシフトします。この後

だけでボタンのクリック時にのEditTextの値を取得し、あなたのbt.executeを呼び出します。ここでは

String url= etUrlByUser.getText().toString(); BackTask bt=new BackTask(); bt.execute("http://123.12.22.32/tmp/text.txt");

+0

あなたの答えが間違っています* *。 – 323go

関連する問題