gpt4 book ai didi

android - Android进度栏未显示

转载 作者:搜寻专家 更新时间:2023-11-01 07:39:34 25 4
gpt4 key购买 nike

private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
removeDialog(0);
switch (msg.what)
{
case SUCCESS:
Log.i("LoginActivity", "Got inputstream");
final InputStream is = (InputStream)msg.obj;
if (is != null)
{
//TODO: examine the input stream, etc...
}
break;
case FAILURE:
Log.i("LoginActivity", "Login failed");
//TODO: Failure handling
break;
default:
break;
}
}
};

@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case 0:
{
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}

public void loginRequest(String name,String password)
{
showDialog(0);
loginThread thread = new loginThread(serverURL, name, password,getMyIMENumber(), handler);
thread.start();
}

……
public class loginThread extends Thread {

String name;
String password;
InputStream is;
String Url;
String ime;

private final Handler handler;

public loginThread(String Url,String name,String password,String ime,final Handler handler) {
this.name = name;
this.Url = Url;
this.password = password;
this.ime = ime;
this.handler = handler;
}


public void run() {
try{
HttpClient httpClient = new DefaultHttpClient();
String loginURL = Url+"member/login";
HttpPost httppost = new HttpPost(loginURL);
HttpParams httpParameters = new BasicHttpParams();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
if(name.contentEquals(""))
{
pairs.add(new BasicNameValuePair("ime", ime));
}
else
{
pairs.add(new BasicNameValuePair("userName", name));
pairs.add(new BasicNameValuePair("password", password));
}


int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
httppost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
final InputStream is = entity.getContent();

Message message = new Message();
message.obj = is;
message.what = server.SUCCESS;
handler.sendMessage(message);
}catch(Exception e){
Log.e("log_tag", "Error in http connection " + e.toString(), e);
handler.sendEmptyMessage(server.FAILURE);
}

}

}
进度对话框没有显示我在这里做错了什么!!请帮帮我..
…………
//dexfile.java编译(版本1.5:49.0,超级位)
公共最终类dalvik.system.dexpile{
//方法描述符8(ljava/io/file;)v
//stack:3,locals:2
公共dexfile(java.io.file文件)抛出java.io.ioexception;
阿洛德[这个]
1调用特殊java.lang.object()[1]
4新java.lang.RuntimeException[2]
7重复
8最不发达国家[3]
10调用特殊java.lang.RuntimeException(java.lang.String)[4]
13阿思罗
行号:
[PC:0,第4行]
局部变量表:
[PC:0,PC:14]本地:此索引:0类型:dalvik.system.dexpile
[PC:0,PC:14]本地:文件索引:1类型:java.io.file
//方法描述符18(ljava/lang/string;)v
//stack:3,locals:2
公共dexfile(java.lang.string文件名)抛出java.io.ioexception;
阿洛德[这个]
1调用特殊java.lang.object()[1]
4新java.lang.RuntimeException[2]
7重复
8最不发达国家[3]
10调用特殊java.lang.RuntimeException(java.lang.String)[4]
13阿思罗
行号:
[PC:0,第5行]
局部变量表:
[PC:0,PC:14]本地:此索引:0类型:dalvik.system.dexpile
[PC:0,PC:14]本地:文件名索引:1类型:java.lang.string
//method descriptor 22(ljava/lang/string;ljava/lang/string;i)ldalvik/system/dexfile;
//stack:3,locals:3
public static dalvik.system.dexfile loaddex(java.lang.string sourcepathname,java.lang.string outputpathname,int flags)抛出java.io.ioexception;
0新java.lang.RuntimeException[2]
3个重复
4最不发达国家[3]
6调用特殊java.lang.RuntimeException(java.lang.String)[4]
9阿瑟罗
行号:
[个人电脑:0,线路:6]
局部变量表:
[PC:0,PC:10]本地:源路径名索引:0类型:java.lang.string
[PC:0,PC:10]本地:outputPathName索引:1类型:java.lang.string
[PC:0,PC:10]本地:标志索引:2类型:int
//方法描述符28()ljava/lang/string;
//stack:3,locals:1
公共java.lang.string getname();
0新java.lang.RuntimeException[2]
3个重复
4最不发达国家[3]
6调用特殊java.lang.RuntimeException(java.lang.String)[4]
9阿瑟罗
行号:
[个人电脑:0,线路:7]
局部变量表:
[PC:0,PC:10]本地:此索引:0类型:dalvik.system.dexpile
//方法描述符30()v
//stack:3,locals:1
public void close()抛出java.io.ioexception;
0新java.lang.RuntimeException[2]
3个重复
4最不发达国家[3]
6调用特殊java.lang.RuntimeException(java.lang.String)[4]
9阿瑟罗
行号:
[PC:0,第8行]
局部变量表:
[PC:0,PC:10]本地:此索引:0类型:dalvik.system.dexpile
//方法描述符32(ljava/lang/string;ljava/lang/classloader;)ljava/lang/class;
//stack:3,locals:3
公共java.lang.Class加载类(java.lang.String名称,java.lang.Classloader加载程序);
0新java.lang.RuntimeException[2]
3个重复
4最不发达国家[3]
6调用特殊java.lang.RuntimeException(java.lang.String)[4]
9阿瑟罗
行号:
[PC:0,第9行]
局部变量表:
[PC:0,PC:10]本地:此索引:0类型:dalvik.system.dexpile
[PC:0,PC:10]本地:名称索引:1类型:java.lang.string
[PC:0,PC:10]本地:加载程序索引:2类型:java.lang.classloader
//方法描述符37()ljava/util/enumeration;
//签名:()ljava/util/enumeration;
//stack:3,locals:1
公共java.util.Enumeration Entries();
0新java.lang.RuntimeException[2]
3个重复
4最不发达国家[3]
6调用特殊java.lang.RuntimeException(java.lang.String)[4]
9阿瑟罗
行号:
[PC:0,第10行]
局部变量表:
[PC:0,PC:10]本地:此索引:0类型:dalvik.system.dexpile
//方法描述符30()v
//stack:3,locals:1
protected void finalize()抛出java.io.ioexception;
0新java.lang.RuntimeException[2]
3个重复
4最不发达国家[3]
6调用特殊java.lang.RuntimeException(java.lang.String)[4]
9阿瑟罗
行号:
[PC:0,第11行]
局部变量表:
[PC:0,PC:10]本地:此索引:0类型:dalvik.system.dexpile
//方法描述符42(ljava/lang/string;)z
public static native boolean isdexptneed(java.lang.string arg0)抛出java.io.fileNotFoundException、java.io.ioException;
}

最佳答案

您可以立即从您的线程中检索InputStream,它很可能仍然为空,或者具有其初始值,但尚未获得http响应。这样您的进度对话框最多显示几毫秒。
在调用loginRequest之后结束thread.start(),如果您使用run获得了ui线程的结果或任何异常,则从线程的Handler方法内部进行通信。在处理程序的handleMessage中检查loginRequest的结果(结果输入流最好直接传递给处理程序,而不是从线程中检索它),然后删除进度对话框。
更新
以下是使用线程实现所需功能的示例:
在下面的代码中,您的主活动被引用为LoginActivity(因为我不知道您的类的名称),所以您应该将其更改为java活动类的任何名称,其中有loginRequest方法。
从主活动中删除showProgressDialog()方法,改为:

public static final int FAILURE = 0;
public static final int SUCCESS = 1;
private ProgressDialog dialog;

private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
removeDialog(0);
switch (msg.what)
{
case SUCCESS:
Log.i("LoginActivity", "Got inputstream");
final InputStream is = (InputStream)msg.obj;
if (is != null)
{
//TODO: examine the input stream, etc...
}
break;
case FAILURE:
Log.i("LoginActivity", "Login failed");
//TODO: Failure handling
break;
default:
break;
}
}
};

@Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case 0:
{
dialog = new ProgressDialog(this);
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}

public void loginRequest(String name, String password)
{
showDialog(0);
LoginThread thread = new LoginThread(serverURL, name, password,
getMyIMENumber(), handler);
thread.start();
}

注意, loginRequest方法已经改变!
你的课也是:
public class LoginThread extends Thread
{

private final String name;
private final String password;
private final String url;
private final String ime;
private final Handler handler;

public LoginThread(String Url, String name, String password,
String ime, final Handler handler)
{
this.name = name;
this.url = Url;
this.password = password;
this.ime = ime;
this.handler = handler;
}

public void run()
{
try
{
HttpClient httpClient = new DefaultHttpClient();
String loginURL = url + "member/login";
HttpPost httppost = new HttpPost(loginURL);
HttpParams httpParameters = new BasicHttpParams();
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
if (name.contentEquals(""))
{
pairs.add(new BasicNameValuePair("ime", ime));
}
else
{
pairs.add(new BasicNameValuePair("userName", name));
pairs.add(new BasicNameValuePair("password", password));
}

int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
httppost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse response = httpClient.execute(httppost);
HttpEntity entity = response.getEntity();
final InputStream is = entity.getContent();

Message message = new Message();
message.obj = is;
message.what = LoginActivity.SUCCESS;
handler.sendMessage(message);
}
catch (Exception e)
{
Log.e("log_tag", "Error in http connection " + e.toString(), e);
handler.sendEmptyMessage(LoginActivity.FAILURE);
}
}
}

现在您的 LoginThread实例有一个 LoginThread类型的新成员。它是通过构造函数参数初始化的(与其他参数一样),因此您可以将其声明为 Handler
此处理程序将通知您的ui线程(主活动),如果它已从http响应获取输入流,或者由于某种原因失败。
完成 final实现,让它做您需要的事情。
我希望代码在这种方式下能澄清一点。

关于android - Android进度栏未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5814170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com