致命异常#asynk任务

时间:2014-06-01 11:25:49

标签: android asynchronous task

我是一个新的Andorid noob :) ...和我关于SO的第一个问题:)

所以我想做什么:

我使用按钮登录Facebook,我收到GraphUser,我调用一个服务,用他的Facebook数据在数据库中注册用户。 该寄存器在Asynk任务中完成。 我从服务和onPostExecute接收json我想用从json收到的数据库userId再次调用其他服务, 也可以在asynk任务中获取其他数据,然后填充视图。 这是我在doInBackground上获得asynk任务异常的地方。我做错了什么?

以下是对他的facebook数据进行注册的呼吁:

public class GetUserFacebookDataTask extends AsyncTask<Void, Void, String> {

    Activity activity;
    String fbNameParam;
    String fbEmailParam;
    String passwordParam = "";
    String android_id = "";
    String fbGengerParam;

    public GetUserFacebookDataTask(String fbNameParam, String fbEmailParam,
            final Activity current_activity, String android_id,
            String fbGengerParam) {
        this.activity = current_activity;
        this.fbNameParam = fbNameParam;
        this.fbEmailParam = fbEmailParam;
        this.android_id = android_id;
        this.fbGengerParam = fbGengerParam;

    }

    @Override
    protected String doInBackground(Void... params) {

        Log.d("GetUserFacebookDataTask doInBackground",
                "GetUserFacebookDataTask");

        Map<String, Object> paramsSend = new LinkedHashMap<String, Object>();
        paramsSend.put("name", fbNameParam);
        paramsSend.put("email", fbEmailParam);
        paramsSend.put("password", "");
        paramsSend.put("deviceToken", android_id);
        paramsSend.put("deviceType", 2);
        paramsSend.put("withFacebook", 1);

        if (fbGengerParam.equals("male")) {
            fbGengerParam = "m";
        } else {
            fbGengerParam = "f";
        }

        paramsSend.put("gender", fbGengerParam);

        String stausHandler = null;

        JsonObject response = null;
        try {
            response = WebServiceApi.PostToServiceLoginFacebook("login",
                    paramsSend);
        } catch (ClientProtocolException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        } catch (JSONException e) {

            e.printStackTrace();
        }

        if (response != null) {
            JsonObject jsonData = ((JsonObject) response.get("userData"));

            JsonElement userPhotoUrl = jsonData.get("userPhotoUrl");
            JsonElement userPhoto = jsonData.get("userPhoto");
            JsonElement userNewEmail = jsonData.get("userNewEmail");
            JsonElement userAge = jsonData.get("userAge");

            String userPhotoUrlf = "";
            String userPhotof = "";
            String userNewEmailf = "";
            String userAgef = "";

            if (userPhotoUrl.toString().equals("null")) {
                userPhotoUrlf = "-";
            }

            if (userPhoto.toString().equals("null")) {
                userPhotof = "-";
            }

            if (userNewEmail.toString().equals("null")) {
                userNewEmailf = "-";
            }

            if (userAge.toString().equals("null")) {
                userAgef = "-";
            }

            UserData userData = new UserData(userPhotoUrlf, jsonData.get(
                    "userHeightInterval").toString(), jsonData.get(
                    "userCity").toString(), userPhotof, jsonData.get(
                    "userPassword").toString(), jsonData.get("userStyle")
                    .toString(), jsonData.get("userEmail").toString(),
                    jsonData.get("userDateRegistered").toString(), jsonData
                            .get("userConfirmed").toString(), jsonData.get(
                            "userWithFacebook").toString(), jsonData.get(
                            "userID").toString(), jsonData
                            .get("userGender").toString(), jsonData.get(
                            "userWantsPushNotifications").toString(),
                    jsonData.get("userName").toString(), userNewEmailf,
                    userAgef, jsonData.get("userShape").toString());

            UserData data = AppManager.getInstance().setUserData(userData);

            if (userData != null) {

                JsonElement status = response.get("status");

                stausHandler = status.toString();

            }
        }

        return stausHandler;
    }

    protected void onPostExecute(String status) {

        Log.d("GetUserFacebookDataTask onPostExecute",
                "GetUserFacebookDataTask onPostExecute");

        StatusHandlerLoginFb(status.toString(), activity);
    }

}

//收到json的状态处理程序

public void StatusHandlerLoginFb(String status, Activity currentActivity) {

    if (status.equals("0")) {
        Log.d("StatusHandlerLoginFb", "StatusHandlerLoginFb");
        Intent intent = new Intent(currentActivity,
                NavigationActivity.class);
        currentActivity.startActivity(intent);
        //new GetPhotoDataTaskFb(currentActivity).execute();
        new GetPhotoDataTask(currentActivity).execute();
    }
}

//其他任务

公共类GetPhotoDataTask扩展             的AsyncTask&GT; {

    Activity activity;

    public GetPhotoDataTask(Activity activity) {
        this.activity = activity;
    }

    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        Log.d("GetPhotoDataTask onPreExecute", "GetPhotoDataTask onPreExecute");
        super.onPreExecute();

        progressDialog = ProgressDialog.show(activity, "Preluare Date",
                "Va rugam asteptati!");
    }

    @Override
    protected List<PhotoData> doInBackground(Void... params) {


        Log.d("GetPhotoDataTask doInBackground", "GetPhotoDataTask doInBackground");
        MyStyleApi myStyleApi = new MyStyleApi();

        List<PhotoData> photoData = null;

        try {
            photoData = myStyleApi.getPhotoDataWithDispatch();
        } catch (JSONException e) {

            e.printStackTrace();
        } catch (ClientProtocolException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        List<String> photoStr = new ArrayList<String>();

        for (int i = 0; i < photoData.size(); i++) {
            photoStr.add(photoData.get(i).getPhotoURL());
        }

        String[] photoUrls = new String[photoStr.size()];
        photoUrls = photoStr.toArray(photoUrls);

        AppManager.getInstance().setphotoUrls(photoUrls);

        List<PhotoData> photoDataS = AppManager.getInstance().setPhotoData(
                photoData);

        return photoData;
    }

    protected void onProgressUpdate(Integer... percent) {
        progressDialog = ProgressDialog.show(activity, "Preluare Date",
                "Va rugam asteptati!");
    }

    protected void onPostExecute(List<PhotoData> photoData) {

        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }
}

// getPhotoDataWithDispatch和另一种接收数据的方法

// get PhotoData with userId as param
public List<PhotoData> getPhotoDataWithDispatch() throws JSONException,
        ClientProtocolException, IOException {

    Log.d("getPhotoDataWithDispatch ", "getPhotoDataWithDispatch ");

    UserData data = AppManager.getInstance().getUserData();
    final String userID = data.getUserID();

    Map<String, Object> params = new LinkedHashMap<String, Object>();
    params.put("userID", userID);

    JsonArray response = WebServiceApi.PostToServiceWithStringResponse(
            "images/get_images_data", params);

    if (response != null) {

        List<PhotoData> photoDataList = new ArrayList<PhotoData>();
        photoDataList = parseJsonArrayForFotoData(response);
        return photoDataList;

    }

    return null;
}

// log cat

06-01 14:44:27.400: D/getUserDataFacebookLogin2(24790): getUserDataFacebookLogin2
06-01 14:44:27.410: D/GetUserFacebookDataTask doInBackground(24790): GetUserFacebookDataTask
06-01 14:44:27.410: D/PostToServiceLoginFacebook(24790): PostToServiceLoginFacebook
06-01 14:44:27.940: D/GetUserFacebookDataTask onPostExecute(24790): GetUserFacebookDataTask onPostExecute
06-01 14:44:27.940: D/StatusHandlerLoginFb(24790): StatusHandlerLoginFb
06-01 14:44:27.980: D/GetPhotoDataTask onPreExecute(24790): GetPhotoDataTask onPreExecute
06-01 14:44:28.040: D/GetPhotoDataTask doInBackground(24790): GetPhotoDataTask doInBackground
06-01 14:44:28.040: D/getPhotoDataWithDispatch(24790): getPhotoDataWithDispatch 
06-01 14:44:28.070: D/memalloc(24790): /dev/pmem: Mapped buffer base:0x4ddae000 size:8110080 offset:7618560 fd:66
06-01 14:44:28.190: W/dalvikvm(24790): threadid=13: thread exiting with uncaught exception (group=0x40c541f8)
06-01 14:44:28.200: E/AndroidRuntime(24790): FATAL EXCEPTION: AsyncTask #3
06-01 14:44:28.200: E/AndroidRuntime(24790): java.lang.RuntimeException: An error occured while executing doInBackground()
06-01 14:44:28.200: E/AndroidRuntime(24790):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at java.lang.Thread.run(Thread.java:856)
06-01 14:44:28.200: E/AndroidRuntime(24790): Caused by: java.lang.IllegalStateException: This is not a JSON Array.
06-01 14:44:28.200: E/AndroidRuntime(24790):    at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at com.example.palashfashion.webservice.WebServiceApi.PostToServiceWithStringResponse(WebServiceApi.java:166)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at com.example.palashfashion.webservice.MyStyleApi.getPhotoDataWithDispatch(MyStyleApi.java:97)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at com.example.palashfashion.webservice.MyStyleApi$GetPhotoDataTask.doInBackground(MyStyleApi.java:877)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at com.example.palashfashion.webservice.MyStyleApi$GetPhotoDataTask.doInBackground(MyStyleApi.java:1)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
06-01 14:44:28.200: E/AndroidRuntime(24790):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-01 14:44:28.200: E/AndroidRuntime(24790):    ... 5 more

1 个答案:

答案 0 :(得分:1)

它崩溃的原因是因为WebServiceApi.PostToServiceWithStringResponse没有返回Json数组