1

As said in title, I have problem running my simple app which makes http request on my phone. On emulator it works perfectly, but on phone it can't download the string required.

public class MainActivity extends AppCompatActivity {
Button btn;

public String uzmiLokacije()
{
    String url_all_products = "http://www.parkingpmf.co.nf/db_get_all.php";
    HttpURLConnection urlConnection = null;
    String res = "";

    try {
        URL url = new URL(url_all_products);
        urlConnection = (HttpURLConnection) url.openConnection();

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line;
        //String res = "";

        while((line=reader.readLine()) != null)
        {
            res = res + line;
        }
    }
    catch(Exception e)
    {
        System.out.println(e.getMessage());
    }
    finally{
        urlConnection.disconnect();
        return res;
    }
}

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

    btn = (Button) findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener(
    ){
        @Override
        public void onClick(View v) {
            String ans = uzmiLokacije();
            Toast.makeText(getApplicationContext(), ans, Toast.LENGTH_LONG).show();
        }
    });
}

}

Any hints why is this happening? I added this in manifest:

<uses-permission android:name="android.permission.INTERNET" />

P.s. I'm working with Android Studio

Yazan
  • 6,074
  • 1
  • 19
  • 33
Luka Bulatovic
  • 357
  • 5
  • 19
  • Have you tried accessing that page in a Web browser on this device? Also, you need to move this network I/O to a background thread, as you may be crashing with a `NetworkOnMainThreadException`. – CommonsWare Feb 28 '16 at 18:07
  • I have tried opening in browser and yes it works. Can you explain how to do it, because I didn't do anything with background threads till now? I will try catching that exception now – Luka Bulatovic Feb 28 '16 at 18:08
  • Please provide some more information for us to help you, like your phone, it's android version, etc... – Luca Schimweg Feb 28 '16 at 18:08
  • Android version on the phone is 4.2.2 Here is my gradle part regarding versions: android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.exampleapp.luka.novi" minSdkVersion 10 targetSdkVersion 10 versionCode 1 versionName "1.0" } – Luka Bulatovic Feb 28 '16 at 18:09
  • Edit: I caught NetworkOnMainThreadException exception. Now what? – Luka Bulatovic Feb 28 '16 at 18:15
  • http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – CommonsWare Feb 28 '16 at 18:19

1 Answers1

0

Android forbids loading data from the internet in the main thread, because it will make the app laggy with a slow connection. You can write your own worker thread or you can use the one provided by android itself.

final TextView mTextView = (TextView) findViewById(R.id.text);
...

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

(Example taken from here)

As you can see, this example sets the text of a TextView to a text loaded from the internet. You have to change the code in the Methods onResponse and onErrorResponse to implement your own logic.

Luca Schimweg
  • 747
  • 5
  • 18