Android:使用“进度”GUI在后台线程中从web加载数据?
可能重复: 我想将信息从web服务器加载到我的应用程序中。目前,我在主线程中执行此操作,我读到这是一个非常糟糕的做法,如果请求时间超过5秒,应用程序就会崩溃 因此,我想学习如何将此操作移动到后台线程。这是否涉及某种服务 下面是我发出服务器请求的代码示例:Android:使用“进度”GUI在后台线程中从web加载数据?,android,multithreading,json,url,Android,Multithreading,Json,Url,可能重复: 我想将信息从web服务器加载到我的应用程序中。目前,我在主线程中执行此操作,我读到这是一个非常糟糕的做法,如果请求时间超过5秒,应用程序就会崩溃 因此,我想学习如何将此操作移动到后台线程。这是否涉及某种服务 下面是我发出服务器请求的代码示例: // send data URL url = new URL("http://www.myscript.php"); URLConnection conn = url.openConnectio
// send data
URL url = new URL("http://www.myscript.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line + "\n");
}
wr.close();
rd.close();
String result = sb.toString();
Intent i = new Intent(searchEventActivity.this, searchResultsActivity.class);
i.putExtra("result", result);
startActivity(i);
所以我在等待建立一个JSON字符串响应,然后将该字符串传递给一个新的活动。这是一个及时的操作,而不是挂起用户界面,我想向用户展示一个很好的进度条,即使是一个带有旋转灯的圆圈,当这个URL业务在后台线程中发生时也是很好的
感谢您的帮助或教程链接 该流程的基本思想是创建一个线程来处理web请求,然后使用处理程序和可运行程序来管理UI交互 我在我的应用程序中管理它的方式是使用一个自定义类,该类包含所有智能和业务规则来管理我的通信。它还包括构造函数中的变量,以允许调用UI线程 下面是一个例子:
public class ThreadedRequest
{
private String url;
private Handler mHandler;
private Runnable pRunnable;
private String data;
private int stausCode;
public ThreadedRequest(String newUrl, String newData)
{
url = newUrl;
data = newData;
mHandler = new Handler();
}
public void start(Runnable newRun)
{
pRunnable = newRun;
processRequest.start();
}
private Thread processRequest = new Thread()
{
public void run()
{
//Do you request here...
if (pRunnable == null || mHandler == null) return;
mHandler.post(pRunnable);
}
}
}
这将从UI线程调用,如下所示:
final ThreadedRequest tReq = new ThreadedRequest(url, maybeData);
//This method would start the animation/notification that a request is happening
StartLoading();
tReq.start(new Runnable()
{
public void run()
{
//This would stop whatever the other method started and let the user know
StopLoading();
}
});
该过程的基本思想是创建一个线程来处理web请求,然后使用处理程序和可运行程序来管理UI交互 我在我的应用程序中管理它的方式是使用一个自定义类,该类包含所有智能和业务规则来管理我的通信。它还包括构造函数中的变量,以允许调用UI线程 下面是一个例子:
public class ThreadedRequest
{
private String url;
private Handler mHandler;
private Runnable pRunnable;
private String data;
private int stausCode;
public ThreadedRequest(String newUrl, String newData)
{
url = newUrl;
data = newData;
mHandler = new Handler();
}
public void start(Runnable newRun)
{
pRunnable = newRun;
processRequest.start();
}
private Thread processRequest = new Thread()
{
public void run()
{
//Do you request here...
if (pRunnable == null || mHandler == null) return;
mHandler.post(pRunnable);
}
}
}
这将从UI线程调用,如下所示:
final ThreadedRequest tReq = new ThreadedRequest(url, maybeData);
//This method would start the animation/notification that a request is happening
StartLoading();
tReq.start(new Runnable()
{
public void run()
{
//This would stop whatever the other method started and let the user know
StopLoading();
}
});
... 已经有几百个了,很多类似/相同的问题。。。。已经有几百个了,很多类似/相同的问题。