这是我的报废算法:
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;
}
我计划在网页浏览中显示图片。
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);
}
}
按下名为login的按钮。它将执行此代码。
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"));
}
}
答案 0 :(得分:0)
您无法在非UI线程中更改UI。在非UI工作线程中调用doInBackground()
方法。
在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);
}
}
我的更改已完成,您可以复制粘贴。
答案 1 :(得分:0)
您无法在doInBackground中发出与UI相关的任务。所有对UI组件“结果”的访问都需要在onPostExecute()中完成。