0

这是我的报废算法:

private StringBuilder inputStreamToString(InputStream is) {
         String line = "";
         StringBuilder total = new StringBuilder();
         // Wrap a BufferedReader around the InputStream

         BufferedReader rd = new BufferedReader(new InputStreamReader(is));
         // Read response until the end

        try{
          while ((line = rd.readLine()) != null) 
              {
                    source = source + line;
              }

          st = new StringTokenizer(line, "><");
          marker = st.nextToken();

          while(st.hasMoreTokens())
          {

                nameLoop:
                if(marker.equals("Name:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Name = marker;
                                    marker = st.nextToken();
                                }
                                break nameLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                addressLoop:
                if(marker.equals("Address:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Address = marker;
                                    marker = st.nextToken();
                                }
                                break addressLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                sexLoop:
                if(marker.equals("Sex:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Sex = marker;
                                    marker = st.nextToken();
                                }
                                break sexLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                birthLoop:
                if(marker.equals("Birthdate:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Birthdate = marker;
                                    marker = st.nextToken();
                                }
                                break birthLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                bplaceLoop:
                if(marker.equals("Birth Place:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Birthplace = marker;
                                    marker = st.nextToken();
                                }
                                break bplaceLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                citizenLoop:
                if(marker.equals("Citizen:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Citizen = marker;
                                    marker = st.nextToken();
                                }
                                break citizenLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                parentLoop:
                if(marker.equals("Parent:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Parent = marker;
                                    marker = st.nextToken();
                                }
                                break parentLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                telLoop:
                if(marker.equals("Tel No.:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    TelNo = marker;
                                    marker = st.nextToken();
                                }
                                break telLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                idLoop:
                if(marker.equals("ID #: "))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    IDNo = marker;
                                    marker = st.nextToken();
                                }
                                break idLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                courseLoop:
                if(marker.equals("Course:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Course = marker;
                                    marker = st.nextToken();
                                }
                                break courseLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                majorLoop:
                if(marker.equals("Major:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Major = marker;
                                    marker = st.nextToken();
                                }
                                break majorLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                yearLoop:
                if(marker.equals("Year:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Year = marker;
                                    marker = st.nextToken();
                                }
                                break yearLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                sectionLoop:
                if(marker.equals("Section:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    Section = marker;
                                    marker = st.nextToken();
                                }
                                break sectionLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                schoolLoop:
                if(marker.equals("High School:"))
                {
                    while(st.hasMoreTokens())
                        {   
                            if(marker.equals("strong"))
                            {
                                marker = st.nextToken();
                                while(!(marker.equals("/strong")))
                                {
                                    School = marker;
                                    marker = st.nextToken();
                                }
                                break schoolLoop;
                            }

                            marker = st.nextToken();
                        }
                }

                else
                    marker = st.nextToken();
          }
        }

            catch (IOException e) {
                e.printStackTrace();
            }


         return total;

     }

我打算在 webview 中显示图像。

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://students.usls.edu.ph/login.cfm");

该程序将首先通过登录连接到一个网站。它将用户名和密码edittext的内容传递给该网站并登录。然后它将检索图片的 url 并将其显示在下一个面板中。它还将从网站上抓取所需的信息,例如姓名、地址等。

private class URLConnect extends AsyncTask<String, Void, Void> {
         private final ProgressDialog dialog = new ProgressDialog(Login.this);

         // can use UI thread here
         protected void onPreExecute() {
            this.dialog.setMessage("Logging in...");
            this.dialog.show();


         }

         // automatically done on worker thread (separate from UI thread)
         protected Void doInBackground(final String... args) {



            try {

                String username = txtUname.getText().toString();
                String password = txtPass.getText().toString();

                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("username",username));
                nameValuePairs.add(new BasicNameValuePair("password",password));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                HttpResponse response = httpclient.execute(httppost);

                str = inputStreamToString(response.getEntity().getContent()).toString();


            }

            catch (ClientProtocolException e) {
                e.printStackTrace();
               } catch (IOException e) {
                e.printStackTrace();
               }


            return null;
         }

         protected void onPostExecute(Void result) {

                    result.setText(str);

        results = (String) result.getText();
                Intent intent = new Intent(Login.this,Main_screen.class);
                intent.putExtra("result", results);
                pic = "http://teachers.usls.edu.ph/student_pics/" + txtUname.getText() + ".jpg";
                intent.putExtra("pic",pic);

                startActivity(intent);
         }
     }

通过按下一个称为登录的按钮。它将执行此代码。

public void login (View view)
{
    new URLConnect().execute();
}

第二个面板中的代码是这样的:

public class Main_screen extends Activity {


public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    TextView result = (TextView)findViewById(R.id.txtresult);
    String stdName;

    result.setText(""+getIntent().getExtras().getString("result"));
}
}
4

2 回答 2

0

您不能在非 UI 线程中更改 UI。该doInBackground()方法在非 UI 工作线程中调用。

和在 UI 线程onPreExecute()onPostExecute()调用,因此您可以使用它们来更改 UI。

我建议你这样做:

private class URLConnect extends AsyncTask<String, Void, String> {
     private final ProgressDialog dialog = new ProgressDialog(Login.this);

     // can use UI thread here
     protected void onPreExecute() {
        this.dialog.setMessage("Logging in...");
        this.dialog.show();


     }

     // automatically done on worker thread (separate from UI thread)
     protected String doInBackground(final String... args) {



        try {

            String username = txtUname.getText().toString();
            String password = txtPass.getText().toString();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username",username));
            nameValuePairs.add(new BasicNameValuePair("password",password));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);

            String str = inputStreamToString(response.getEntity().getContent()).toString();

            results = (String) result.getText();

            return str; //Here you return the string you want to set

        }

        catch (ClientProtocolException e) {
            e.printStackTrace();
           } catch (IOException e) {
            e.printStackTrace();
           }


        return null;
     }

     protected void onPostExecute(String resultString) {

            result.setText(resultString); //Here you change the text on the TextView;
            Intent intent = new Intent(Login.this,Main_screen.class);
            intent.putExtra("result", results);
            pic = "http://teachers.usls.edu.ph/student_pics/" + txtUname.getText() + ".jpg";
            intent.putExtra("pic",pic);

            startActivity(intent);
     }
 }

我的更改已完成,您可以复制粘贴它。

于 2012-08-14T14:31:28.650 回答
0

您不能在 doInBackground 中发出与 UI 相关的任务。所有对 UI 组件“结果”的访问都需要在 onPostExecute() 中完成。

于 2012-08-14T14:32:36.837 回答