0

i'm sorry if there is a question like this i just wanna know, i'm trying to read file .txt on server and i convert to int and i have succeed when i try on froyo simulator

this is my code

final Context context = this;
TextView textServer, textApp;
final String textSource = "http://xxx/dev/android/androidversi.txt";

DECLARATION FOR LOCATION OF .txt file

URL textUrl;
String StringBuffer;
String stringText = "";

try {
    textUrl = new URL(textSource);
    BufferedReader bufferReader = new BufferedReader(new InputStreamReader(textUrl.openStream()));

    while ((StringBuffer = bufferReader.readLine()) != null) 
    {
        stringText += StringBuffer;
    }
    bufferReader.close();
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

CODE ABOVE READS .txt FILE FROM SERVER AND THIS CODE WHICE ARE ERRORS IN JELLY BEAN, BECAUSE I'VE TRIED ON FROYO IS RUNNING NORMALLY

PackageManager manager = getPackageManager();
PackageInfo info;
try {
    info = manager.getPackageInfo(getPackageName(), 0);
    int version = info.versionCode;

    if(Integer.parseInt(stringText)>version) // I'VE CONVERTED STRING FROM .txt TO INT SO THAT I CAN COMPARE IT WITH VERSION ON APP
    {
        // IF VERSION ON SERVER > VERSION APP THERE IS LINK TO DOWNLOAD HERE
    }
} catch (NameNotFoundException e){
    e.printStackTrace();
}

compare the version

Please help me :'(

Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
oyiym
  • 11
  • 7

1 Answers1

0

Its nothing but the Network on main thread exception; Just include

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

to your code (in onCreate() method) and you'll be fine :)

Manoj Kumar
  • 1,510
  • 3
  • 20
  • 40
  • Strict mode is a method of avoiding network actions in main thread; introduced from 3.0 Honeycomb; the code just tells if the target version is above froyo, it'll turn off strict mode:) – Manoj Kumar Sep 13 '12 at 08:07
  • i change 9 (num that you write on code) to 8, is it problem? btw is it code safety and no effects? – oyiym Sep 13 '12 at 08:22
  • thanks, dude :). btw do you understand about blackberry programming? – oyiym Sep 13 '12 at 08:52
  • unfortunately i've played with only Android and ios for now; will learn it soon:) sorry for now – Manoj Kumar Sep 13 '12 at 09:28
  • The correct solution is to **get the networking code off the main application thread**. This "solution" is merely burying one's head in the sand. – CommonsWare Sep 21 '12 at 12:03