0

here is part of my code:

try {
    // make json string, try also hamburger
    //String json = "{\"pid\":\"name\",\"price\":\"created_at\",\"updated_at\":3}";

    // send as http get request
    URL url = new URL("http://smsas.eu/dm/get_all_products.php");
    URLConnection conn = url.openConnection();

    // Get the response
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {

        //System.out.println(line);

        JSONObject nodeRoot  = new JSONObject(line); 
        JSONArray nodeStats = nodeRoot.getJSONArray("products");
        for(int i=0; i < nodeStats.length();i++){
        String sSDR = nodeStats.getJSONObject(i).getString("name");
        System.out.println(sSDR);
        tt.setText(sSDR);
         }



    }
    rd.close();
} catch (Exception e) {
    tt.setText("ERR");

    e.printStackTrace();

}

When I run app on virtual device it's working, but when I run on my phone - not. I've already added internet permission.

Dan Harms
  • 4,725
  • 2
  • 18
  • 28
darjus
  • 417
  • 3
  • 7
  • 21
  • Are you doing the Internet request in a separate thread? What error do you get? – VM4 Apr 15 '14 at 19:19
  • No, everything is in mainActivity, it's almost all code. – darjus Apr 15 '14 at 19:21
  • Well that's your answer, you need to do lenghy operations on another thread. Internet stuff is one of them. Have a look here: http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – VM4 Apr 15 '14 at 19:22
  • Thanks, found solution in your link. – darjus Apr 15 '14 at 20:40

0 Answers0