-1

Okay so I'm trying to make an app that is able to retrieve data from a web server and then show it on a textView and constantly update it if the data changes from the web server. And what I got so far after many rounds of trying is this as shown

public class HttpTest extends AppCompatActivity {

TextView text = (TextView) this.findViewById(R.id.textView3);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_http_test);

    HttpURLConnection urlConnection = null;

    try {
        URL url = new URL("http://IPGOESHERE/");

        HttpURLConnection urLConnection = (HttpURLConnection) url.openConnection();
        InputStream in = urLConnection.getInputStream();
        BufferedReader data = new BufferedReader(new InputStreamReader(in));

        StringBuilder total = new StringBuilder();
        String line;
        while ((line = data.readLine()) != null) {
            total.append(line).append('\n');
        }
        String result = total.toString();
        text.setText(result);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

I don't think what i have written allows for constant update, but I at least want to be able to show some kind of data on a textView and make sure it doesn't crash first. So if someone could point out what I'm doing wrong it will be much appreciated.

Amos
  • 1
  • 1
  • 4
  • 2
    Looks like networkingOnMainThread errror – mehulmpt Mar 07 '17 at 13:43
  • So how do I fix this? Forgot to mention im new to this... – Amos Mar 07 '17 at 13:44
  • have you set permission at your manifest ? – Nazim ch Mar 07 '17 at 13:44
  • also, can you post your log – msecilmis Mar 07 '17 at 13:45
  • I'm not really sure that's the exact error. You have to show your logcat. You'll surely see something there. But with the code youve got it looks like you're probably getting networkingOnMainThread only. Try to create a new thread and do your networking operation in that – mehulmpt Mar 07 '17 at 13:45
  • What kind of permission should I set? I only know i need to add this if I want to use any internet connectivity function – Amos Mar 07 '17 at 13:45
  • You should have in your AndroidManifest.xml, and make all networking calls in the separate Thread. Check: https://developer.android.com/reference/android/os/AsyncTask.html – Vadims Savjolovs Mar 07 '17 at 13:46
  • How do I get the log? Really new to this, I cant seem to find it – Amos Mar 07 '17 at 13:47
  • Your app is likely crashing because of [this](http://stackoverflow.com/questions/36446114/why-android-app-crashes-for-initializing-variable-with-findviewbyidr-id), currently. Also, consult the following posts for general help in determining the cause of a crash: http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Mike M. Mar 07 '17 at 14:54

2 Answers2

2

The app crash because you are making HTTP request on main thread. Use an AsyncTask instead

Angelo Parente
  • 796
  • 2
  • 6
  • 15
1

you need to this kind of work on a different thread. try using an AsyncTask. you create a class that extends AsyncTask, than make the url connection in the doInBackground method (a method that gets called on a worker thread). you can then update the textview in the onPostExecute method which is called on the ui thread. good luck :)

nir
  • 302
  • 3
  • 12