I'm making a simplified app with only one EditText where a user enters an URL address and a Button "download". When the user clicks on the button, the app should download a source of given website and print it on the screen. How can I do it? Or where I should start looking for a solution?
            Asked
            
        
        
            Active
            
        
            Viewed 433 times
        
    -4
            
            
        - 
                    `Or where I should start looking for a solution?` search on the internet – Denny May 24 '17 at 11:52
- 
                    1Make a GET request to the URL and show the result as text. – Henry May 24 '17 at 11:52
- 
                    1Have you consider using WebView ? Or you want to display the source code of the URL? – Timo May 24 '17 at 11:53
- 
                    may be this will help You https://stackoverflow.com/questions/2423498/how-to-get-the-html-source-of-a-page-from-a-html-link-in-android – Solaiman Hossain May 24 '17 at 11:54
- 
                    @Timo I want to display the source code – Nikola May 24 '17 at 11:55
- 
                    @msh.nayan unfortunately all the given solutions are are deprecated now – Nikola May 24 '17 at 11:56
1 Answers
0
            
            
        First give permission in your Manifest file for INTERNET as
<uses-permission android:name="android.permission.INTERNET" />
Here is your layout
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_url"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="bottom">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:layout_width="233dp"
                android:layout_height="58dp"
                android:text="New Button"
                android:id="@+id/downloadImage"
                android:layout_gravity="center"
                android:background="@color/blue"
                android:clickable="true" />
        </LinearLayout>
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="145dp"
            android:layout_height="58dp">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imageView786" />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="45dp">
    </LinearLayout>
</LinearLayout>
Here is your Activity
public class Download extends Activity {
Button bt;
ImageView iv;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.download);
    bt = (Button)findViewById(R.id.downloadImage);
    et = (EditText )findViewById(R.id.et_url);
    iv = (ImageView)findViewById(R.id.imageView786);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DownloadImageFromPath(et.getText().toString());
        }
    });
}
public void DownloadImageFromPath(String path){
    InputStream in =null;
    Bitmap bmp=null;
    //ImageView iv = (ImageView)findViewById(R.id.imageView786);
    int responseCode = -1;
    try{
        URL url = new URL(path);//"http://192.xx.xx.xx/mypath/img1.jpg
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setDoInput(true);
        con.connect();
        responseCode = con.getResponseCode();
        if(responseCode == HttpURLConnection.HTTP_OK)
        {
            //download
            in = con.getInputStream();
            bmp = BitmapFactory.decodeStream(in);
            in.close();
            iv.setImageBitmap(bmp);
        }
    }
    catch(Exception ex){
        Log.e("Exception", ex.toString());
    }
}
Now if u will give url as in edit text https://i.stack.imgur.com/MB4VU.jpg It will show the image
 
    
    
        Mohammad Arman
        
- 508
- 4
- 13
- 
                    I need to display the source code of the entered URL instead of an image but I'll try to adapt the code to my task. Thanks! – Nikola May 24 '17 at 14:17
- 
                    Have u solved your problem @Nikola ? May be it is duplicate of https://stackoverflow.com/questions/2423498/how-to-get-the-html-source-of-a-page-from-a-html-link-in-android – Mohammad Arman May 25 '17 at 05:36
- 
                    @Nikola it seems u have changed your question , first u have asked downloading image using URL so i have post above code from scratch.. – Mohammad Arman May 25 '17 at 05:42
- 
                    I haven't, pleas read my question once again. I know that myquestion is a duplicate of https://stackoverflow.com/questions/2423498/how-to-get-the-html-source-of-a-page-from-a-html-link-in-android but all the solutions in the given link are deprecated now. – Nikola May 25 '17 at 09:37
- 
                    
 
    