簡體   English   中英

從服務器接收數據時出現Android錯誤

[英]Android error while receiving data from server

我正在使用Android中的代碼:以編程方式安裝.apk

我試圖從服務器接收數據,以更新我的應用程序。

單擊按鈕后,應用程序崩潰。

如果您知道要解決這些錯誤很熱,請告訴我。

我收到此錯誤:

android.os.NetworkOnMainThreadException

at com.example.appupdate.SelfInstall01Activity.GetVersionFromServer(SelfInstall01Activity.java:285)

at com.example.appupdate.SelfInstall01Activity$1.onClick(SelfInstall01Activity.java:89)

我那里有錯誤:

btnCheckUpdates.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            GetVersionFromServer(BuildVersionPath);
           //here

            if(checkInstalledApp(AppName.toString()) == true)
            {
                Toast.makeText(getApplicationContext(), "Application Found " + AppName.toString(), Toast.LENGTH_SHORT).show();


            }else{
                Toast.makeText(getApplicationContext(), "Application Not Found. "+ AppName.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    });

和:

HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();
    //here

這是其余的代碼:

public void GetVersionFromServer(String BuildVersionPath)
{
URL u;
    try {
        u = new URL(BuildVersionPath.toString());

        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();

        InputStream in = c.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024]; 


        int len1 = 0;
        while ( (len1 = in.read(buffer)) != -1 )
        {
            baos.write(buffer,0, len1); 
        }

        String temp = "";
        String s = baos.toString();

        for (int i = 0; i < s.length(); i++)
        {
            i = s.indexOf("=") + 1;
            while (s.charAt(i) == ' ')
            {
                i++; 
            }
            while (s.charAt(i) != ';'&& (s.charAt(i) >= '0' && s.charAt(i) <= '9' || s.charAt(i) == '.'))
            {
                temp = temp.toString().concat(Character.toString(s.charAt(i))) ;
                i++;
            }
            //
            s = s.substring(i); 
            temp = temp + " "; 
        }
        String[] fields = temp.split(" ");

        VersionCode = Integer.parseInt(fields[0].toString());
        VersionName = fields[1].toString();

        baos.close();
    }

當您在主線程中執行網絡操作時,會發生NetworkOnMainThreadException。您需要在doInBackground方法中執行網絡操作

在這里,您的按鈕點擊功能應該是這樣的。

btnCheckUpdates.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        { 
           new YourTask().execute();
        }
    });

這是您的網絡操作。

private class YourTask extends AsyncTask<String, String,String> {
     protected String doInBackground(String... urls) {

         // put here your network operation method
           GetVersionFromServer(BuildVersionPath)

        return null;
     }

 }

當您在UI /主線程中發出網絡請求時,將引發NetworkOnMainThreadException。 因此,您需要在主線程上調用GetVersionFromServer()。 使用線程或AsyncTask。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM