Jquery 如何防止在进行异步AJAX调用时冻结Android中的PhoneGap应用程序?
我有PhoneGap Android应用程序,我正在使用Jquery。我正在进行异步AJAX调用,在调用过程中,应用程序只是冻结并在AJAX调用完成后等待(在GSM连接上主要是显而易见的) 如果我执行同步请求,我会理解这一点,但我有:Jquery 如何防止在进行异步AJAX调用时冻结Android中的PhoneGap应用程序?,jquery,android,ajax,asynchronous,cordova,Jquery,Android,Ajax,Asynchronous,Cordova,我有PhoneGap Android应用程序,我正在使用Jquery。我正在进行异步AJAX调用,在调用过程中,应用程序只是冻结并在AJAX调用完成后等待(在GSM连接上主要是显而易见的) 如果我执行同步请求,我会理解这一点,但我有: $.ajax({type: 'POST',url: 'http://www.example.com', data: data,async:true,success:callback}); 有人能帮你吗?你能让你的AJAX取消主UI线程吗?例如: public c
$.ajax({type: 'POST',url: 'http://www.example.com', data: data,async:true,success:callback});
有人能帮你吗?你能让你的AJAX取消主UI线程吗?例如:
public class JsonParsingActivity extends Activity {
private static final String url = "http://search.twitter.com/search.json?q=javacodegeeks";
protected InitTask _initTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
_initTask = new InitTask();
_initTask.execute( getApplicationContext() );
}
});
}
@Override
public void onStop() {
super.onStop();
_initTask.cancel(true);
}
protected class InitTask extends AsyncTask<Context, String, SearchResponse>
{
@Override
protected SearchResponse doInBackground( Context... params )
{
InputStream source = retrieveStream(url);
SearchResponse response = null;
if (source != null) {
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
try {
response = gson.fromJson(reader, SearchResponse.class);
publishProgress( response.query );
reader.close();
} catch (Exception e) {
Log.w(getClass().getSimpleName(), "Error: " + e.getMessage() + " for URL " + url);
}
}
if (!this.isCancelled()) {
return response;
} else {
return null;
}
}
@Override
protected void onProgressUpdate(String... s)
{
super.onProgressUpdate(s);
Toast.makeText(getApplicationContext(), s[0], Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostExecute( SearchResponse response )
{
super.onPostExecute(response);
StringBuilder builder = new StringBuilder();
if (response != null) {
String delim = "* ";
List<Result> results = response.results;
for (Result result : results) {
builder.append(delim).append(result.fromUser);
delim="\n* ";
}
}
if (builder.length() > 0) {
Toast.makeText(getApplicationContext(), builder.toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "The response was empty.", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCancelled() {
super.onCancelled();
Toast.makeText(getApplicationContext(), "The operation was cancelled.", 1).show();
}
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest;
try {
getRequest = new HttpGet(url);
HttpResponse getResponse = client.execute(getRequest);
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
} catch (Exception e) {
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
return null;
}
}
}
}
public类JsonParsingActivity扩展活动{
私有静态最终字符串url=”http://search.twitter.com/search.json?q=javacodegeeks";
受保护的InitTask\u InitTask;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
按钮按钮=(按钮)findViewById(R.id.button1);
setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
_initTask=新的initTask();
_执行(getApplicationContext());
}
});
}
@凌驾
公共void onStop(){
super.onStop();
_initTask.cancel(true);
}
受保护类InitTask扩展了AsyncTask
{
@凌驾
受保护的SearchResponse doInBackground(上下文…参数)
{
InputStream源=检索流(url);
SearchResponse=null;
如果(源!=null){
Gson Gson=新的Gson();
Reader Reader=新的InputStreamReader(源);
试一试{
response=gson.fromJson(reader,SearchResponse.class);
发布进度(response.query);
reader.close();
}捕获(例外e){
Log.w(getClass().getSimpleName(),“错误:”+e.getMessage()+“用于URL”+URL);
}
}
如果(!this.isCancelled()){
返回响应;
}否则{
返回null;
}
}
@凌驾
受保护的void onProgressUpdate(字符串…s)
{
super.onProgressUpdate(s);
Toast.makeText(getApplicationContext(),s[0],Toast.LENGTH_SHORT).show();
}
@凌驾
受保护的void onPostExecute(SearchResponse响应)
{
super.onPostExecute(响应);
StringBuilder=新的StringBuilder();
if(响应!=null){
字符串delim=“*”;
列表结果=response.results;
对于(结果:结果){
append(delim).append(result.fromUser);
delim=“\n*”;
}
}
如果(builder.length()>0){
Toast.makeText(getApplicationContext(),builder.toString(),Toast.LENGTH_SHORT).show();
}否则{
Toast.makeText(getApplicationContext(),“响应为空。”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
受保护的void onCancelled(){
super.onCancelled();
Toast.makeText(getApplicationContext(),“操作已取消。”,1.show();
}
私有InputStream retrieveStream(字符串url){
DefaultHttpClient=新的DefaultHttpClient();
HttpGet-getRequest;
试一试{
getRequest=新的HttpGet(url);
HttpResponse getResponse=client.execute(getRequest);
HttpEntity getResponseEntity=getResponse.getEntity();
返回getResponseEntity.getContent();
}捕获(例外e){
Log.w(getClass().getSimpleName(),“URL错误”+URL,e);
返回null;
}
}
}
}
来自Jquery文档:
异步的
布尔值
默认值:true
默认情况下,所有请求都是异步发送的(即,默认情况下设置为true)。如果需要同步请求,请将此选项设置为false。跨域请求和数据类型:“jsonp”请求不支持同步操作。请注意,同步请求可能会暂时锁定浏览器,从而在请求处于活动状态时禁用任何操作
由于phonegap代码在本地主机上运行,因此它将跨域运行。如果可以,请通过GET而不是POST执行请求。今天遇到了相同的问题。通过使用延迟10毫秒的setTimeout解决了这个问题
不知道它为什么会起作用,这很可怕。但是确实有效。我也遇到了同样的问题,我通过调用phonegap触发的“deviceready”事件上的ajax请求解决了这个问题 我无意中阅读了一篇教程,其中说ajax请求可以在应用程序正确加载后发送。首先绑定到DocumentDeviceReady事件,并在事件处理程序中发送ajax请求。这样做可以解决阻塞UI的问题
function appReady(){
// do the ajax here
}
document.addEventListener("deviceready", appReady, false);
我希望这也能解决您的问题。尝试以下代码:
$(document).ready(function () {
$.ajax({
type: "POST",
url: "https://demo.yoursuppliernetwork.com/tnvrs/userservice/login/checkUser",
data:{"username":login,"password":password},
dataType:"json",
contentType: "application/json; charset=utf-8",
success: function(html)
{
ss=html;
console.log(ss);
document.write("success");
},
error:function(error)
{
console.log(error.status)
document.write("error");
},
complete:function(html){
console.log()
document.write("complete");
}
});
});
感谢您的回答,但我实际上是在jQuery中寻找解决方案,因为我的应用程序是使用PhoneGap用HTML5编写的……很抱歉,这没有什么帮助。我正在将它添加到我的收藏夹中。我也对答案感兴趣+1没有意义-它说crossdomain不支持同步。GET vs POST在这里没有区别。