模拟器不会执行AsyncTask.execute()。get()

时间:2012-12-23 18:14:49

标签: java android json android-emulator android-asynctask

我有一个从服务器下载JSON字符串的程序。直到最近,当我试着致电

时,情况仍然很好
jsonReader.execute(getUrl).get();

其中jsonReader是下载JSON字符串的AsyncTask,getUrl是要执行的URL。这种方法永远不会被执行,奇怪的是,它可以在我的手机上运行

这是我的代码

调用json字符串阅读器的方法

private PointOfInterest getPointWithID(int id) {

    String getUrl;
    JSONReader jsonReader = new JSONReader();
    try {
        Log.d(TAG,"Trying to get ID: " + id);
        getUrl = url + String.valueOf(id);
        Log.d(TAG,"Trying to get json from: " + id); <-- Last Log line to get printed when run on emulator
        jsonReader.execute(getUrl).get();
    } catch (Exception e) {
        e.printStackTrace();
    }

    String jsonString = jsonReader.returnJSONString();
    Log.d(TAG,"Downloaded: " + jsonString);
    //System.out.println("JSON: " + jsonString);

    JSONResponse jsonResponse = JSONResponse.convertJSONToResponse(jsonString);
    //System.out.println("JSON RESPONSE " + jsonResponse);


    return jsonResponse.getPointofInterest();
}

jsonReader

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

/**
 * Read JSON Strings
 * @author Tom
 *
 */
public class JSONReader extends AsyncTask <String, Void, String> {

    String result;
    DefaultHttpClient  httpclient = new DefaultHttpClient(new BasicHttpParams());
    StringBuilder sb = new StringBuilder();
    String TAG = "JSONReader";

    // constructor
    public JSONReader() {
        result = "";
    }

    @Override
    protected String doInBackground(String... url) {

        Log.d(TAG, "Executing " + url); <--Does not get printed when run on emulator 

        HttpPost httppost = new HttpPost(url[0]);
        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");

        try {
            HttpResponse response = httpclient.execute(httppost);

            HttpEntity entity = response.getEntity();

            InputStream inputStream = entity.getContent();

            // json is UTF-8 by default i believe
            BufferedReader reader = null;

            reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);

            String line = null;
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }
        }catch(Exception e){

            e.printStackTrace();
        }
        return result = sb.toString();
    }

    public String returnJSONString(){

        return result;
    }

}

我已经尝试重新启动adb,制作新的AVD但似乎没有任何效果,我唯一的选择就是在我的手机上进行测试,但这是不可取的,因为我正在为api 17开发。感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

此类必须遵循一些线程规则才能正常工作:

必须在UI线程上加载AsyncTask类。这是从JELLY_BEAN开始自动完成的。

必须在UI线程上创建任务实例。

必须在UI线程上调用

execute(Params ...)。 不要手动调用onPreExecute(),onPostExecute(Result),doInBackground(Params ...),onProgressUpdate(Progress ...)。

该任务只能执行一次(如果尝试第二次执行则会抛出异常。)