0

I am trying to fetch the contents of this url. The contents will get changed for each refresh.

I am wrote my code to fetch the content and display it in my app using TextView by a Button Click.

But it takes more time ( Minimum 2 seconds , Maximum 6 seconds ) to get the data and display into Textview. So what change do i need in my code to make it efficiant and reduce the delay.


Here is my code,

( Fetching done in getData() method using URLConnection method )

public class MainActivity extends AppCompatActivity {

    TextView resultView;
    String TAG="MainActivity kbt";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.enableDefaults();
        resultView = (TextView) findViewById(R.id.result);

        try {
            getData();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Button insert=(Button) findViewById(R.id.button);
        insert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
                try {
                    getData();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

 
    public void getData() throws IOException {

        String result = "";
        InputStream isr = null;

        URLConnection urlConnection = null;
        URL url = new URL("http://kbtganesh.16mb.com/index.php"); 
        urlConnection =  url.openConnection();
        isr =urlConnection.getInputStream();
        try {  
            BufferedReader reader = new BufferedReader(new InputStreamReader(isr));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            isr.close();

            result=sb.toString();
        }
        catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        //parse json data
        try {
            resultView.setText(result);
        }
        catch(Exception e) {
            Log.e("log_tag", "Couldn't set text, damnit.");
        }

    }
}

Thanks in Advance (:

Community
  • 1
  • 1
Ganesh
  • 1,820
  • 2
  • 20
  • 40
  • You can do nothing, if Web service itself is slow. You need to tell the developer.. – Chintan Soni Dec 17 '15 at 06:10
  • But When i try with browser, it was perfect. – Ganesh Dec 17 '15 at 06:12
  • I am using kitkat in my phone ( Where i tested ) and target sdk is 23... I am not getting you **It looks like you're doing a network operation on the main thread** ??? What do you mean? – Ganesh Dec 17 '15 at 06:17
  • Where do i need to do these operations @DanielNugent – Ganesh Dec 17 '15 at 06:17
  • I would have thought you would be getting a NetworkOnMainThreadException, take a look here: http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Daniel Nugent Dec 17 '15 at 06:19
  • I checked in chrome debugger.. your service is taking atleast 1.93 seconds to response... – Chintan Soni Dec 17 '15 at 06:20
  • @ChintanSoni While starting an app, it takes around 5 seconds. After some time of using app, its taking 2 seconds to load – Ganesh Dec 17 '15 at 06:27
  • @ChintanSoni : Daniel is saying something about **NetworkOnMainThreadException** which i think that could be the reason for the delay at starting time of the app. Am i right ? – Ganesh Dec 17 '15 at 06:28
  • So don't do network operations on main thread. – Chintan Soni Dec 17 '15 at 06:28
  • @DanielNugent Thanks mate.. I'll put these into AsyncTask... – Ganesh Dec 17 '15 at 06:30
  • Yes.. Ofcourse he is right.. Dont ever do network operations on main thread.. Because of your this line, `StrictMode.enableDefaults();` its not throwing any exception. – Chintan Soni Dec 17 '15 at 06:31

2 Answers2

0

Try test it in on a local server on your computer, instead of a remote one like you have. Sometimes it's just slow internet speed slowing the page down, and nothing to do with the code.

Tom Anderson
  • 440
  • 3
  • 10
  • Dude, i am not having local server thats why i choosed remote server. – Ganesh Dec 17 '15 at 06:19
  • Just test it on a local server. If it works up to speed, then you know that it's your internet, and not the code. Otherwise, it might then be the code – Tom Anderson Dec 17 '15 at 06:25
0

Remove the StrictMode.enableDefaults(); instead use the AsyncTask to fetch the data from the network as doc says

"Keeping disk and network operations off the main thread makes for much smoother, more responsive applications"

check doc here StrictMode Doce

Else everything looks fine you can also check the improvment on the webservice end check the query it can be optimized as there is a very less data your api is returns

Sandy
  • 985
  • 5
  • 13