异步任务获取错误

时间:2014-10-16 16:43:40

标签: android android-fragments asynchronous

我在这里遇到问题。我正在尝试检索配置文件,以便我可以查看它,然后在需要时更新它。但是,它一直在返回获取配置文件时出错。但是,没有堆栈跟踪,只有我的标签要调试。任何人都可以帮助我吗?

这是片段:

public static class ProfileSectionFragment extends Fragment {

        public ProfileSectionFragment() {

        }

        public static final String ARG_SECTION_NUMBER = "section_number";

        TextView nameDesc, name, contactTV, emailTV, addressTV, postalTV;
        EditText contactET, emailET, addressET1, addressET2, addressET3,
        postalET;
        Button update;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub

            recLifeCycle_with_savedInstanceState(savedInstanceState);
            View rootView = inflater.inflate(R.layout.fragment_profile,
                    container, false);

            nameDesc = (TextView) rootView.findViewById(R.id.nameDescTV);
            name = (TextView) rootView.findViewById(R.id.nameTV);
            contactTV = (TextView) rootView.findViewById(R.id.contactTV);
            emailTV = (TextView) rootView.findViewById(R.id.emailTV);
            addressTV = (TextView) rootView.findViewById(R.id.addressTV);
            postalTV = (TextView) rootView.findViewById(R.id.postalTV);

            contactET = (EditText) rootView.findViewById(R.id.contactET);
            emailET = (EditText) rootView.findViewById(R.id.emailET);
            addressET1 = (EditText) rootView.findViewById(R.id.addressET1);
            addressET2 = (EditText) rootView.findViewById(R.id.addressET2);
            addressET3 = (EditText) rootView.findViewById(R.id.addressET3);
            postalET = (EditText) rootView.findViewById(R.id.postalET);

            update = (Button) rootView.findViewById(R.id.updateButton);

            Button unlink = (Button) rootView.findViewById(R.id.unlinkButton);

            Log.i("User_id profile", user_id);

            new GetProfileAsyncTask((MainActivity) getActivity()).execute(user_id);

            update.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    if (update.getText().equals("Update My Particulars")) {

                        update.setText("Done");

                        contactET.setClickable(true);
                        contactET.setCursorVisible(true);

                        emailET.setClickable(true);
                        emailET.setCursorVisible(true);

                        addressET1.setClickable(true);
                        addressET1.setCursorVisible(true);

                        addressET2.setClickable(true);
                        addressET2.setCursorVisible(true);

                        addressET3.setClickable(true);
                        addressET3.setCursorVisible(true);

                        postalET.setClickable(true);
                        postalET.setCursorVisible(true);

                    }

                    if (update.getText().equals("Done")) {
                        // make async task to update particulars

                        update.setText("Update My Particulars");

                        contactET.setClickable(false);
                        contactET.setCursorVisible(false);

                        emailET.setClickable(false);
                        emailET.setCursorVisible(false);

                        addressET1.setClickable(false);
                        addressET1.setCursorVisible(false);

                        addressET2.setClickable(false);
                        addressET2.setCursorVisible(false);

                        addressET3.setClickable(false);
                        addressET3.setCursorVisible(false);

                        postalET.setClickable(false);
                        postalET.setCursorVisible(false);

                    }
                }

            });

            unlink.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    nyp.edu.sg.alumnigo.SharedPreferences.clearUserName(getActivity()); 
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                }
            });

            return rootView;
        }

        public void addDetails(final Profile profile){

            Log.i("Profile/Profile", profile.toString());

            name.setText(profile.getName());
            contactET.setText(profile.getContact());
            emailET.setText(profile.getEmail());
            addressET1.setText(profile.getAddLine1());
            addressET2.setText(profile.getAddLine2());
            addressET3.setText(profile.getAddLine3());
            postalET.setText(profile.getPostal());
        }

然后,这是异步任务:

public class GetProfileAsyncTask extends AsyncTask<String, Integer, Boolean>{

    ProgressDialog progressDialog;
    MainActivity activityMain;
    Profile profile;

    public GetProfileAsyncTask(MainActivity parent)
    {
        activityMain = parent;
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected Boolean doInBackground(String... params) {
        // TODO Auto-generated method stub
        Boolean error = false;
        try {
            error = getData(params[0]);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return error;
    }

    protected void onPostExecute(Boolean error){    
        if(error == true)
        {
            Log.i("GetProfile", "Error at get profile");
            activityMain.errorOccured();
        }
        else
        {
            activityMain.getProfileSuccess(profile);
        }

    } 

    protected void onProgressUpdate(Integer... progress){
    }


    public Boolean getData(String userId) throws JSONException {

        Boolean error = false;
        HttpClient httpclient = new DefaultHttpClient();
        // specify the URL you want to post to

        try {

            HttpGet httpget = new HttpGet(Constants.HOST_NAME+"/"+Constants.SERVICE_NAME+"/api/Account/"+userId);
            BufferedReader reader;
            StringBuffer sb;
            String line = "";
            String NL="";
            String json;
            HttpResponse response = httpclient.execute(httpget);

            if(response.getStatusLine().getStatusCode()==200)
            {
                reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

                sb = new StringBuffer("");
                line = "";
                NL = System.getProperty("line.separator");

                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + NL);
                }

                reader.close();
                json = sb.toString();

                Log.i("profile json",json);
                try
                {
                    JSONObject jsonObj = new JSONObject(json);   

                    JSONObject attr = jsonObj.getJSONObject(json);
                    profile =  new Profile(); 
                    if(attr.has("Name"))
                    {
                        profile.setName(attr.getString("Name"));
                        profile.setAddLine1(attr.getString("AddressLineOne"));
                        profile.setAddLine2(attr.getString("AddressLineTwo"));
                        profile.setAddLine3(attr.getString("AddressLineThree"));
                        profile.setPostal(attr.getString("PostalCode"));
                        profile.setContact(attr.getString("HomeTel"));
                        profile.setEmail(attr.getString("Email"));

                        Log.i("Async/Profile", profile.toString());

                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                    error = true;
                }
            }
            else
            {
                error = true;
            }
        } 

        catch (ClientProtocolException e) 
        {
            // process execption
            e.printStackTrace();
            error = true;
        } 

        catch (IOException e) 
        {
            // process execption
            e.printStackTrace();
            error = true;
        }

        return error;

    }
}

这是异步任务调用的方法(它在主活动中):

public void getProfileSuccess(Profile profile) {
    final ProfileSectionFragment fragment = (ProfileSectionFragment) getSupportFragmentManager().findFragmentByTag(getProfileFragmentTag());
    Log.i("Main/Profile", profile.toString());
    fragment.addDetails(profile);
}

然后,如果需要,这是xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragmentProfile"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/nameDescTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name: " />

        <TextView
            android:id="@+id/nameTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Placeholder" />

        <TextView
            android:id="@+id/contactTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Contact: " />

        <EditText
            android:id="@+id/contactET"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:cursorVisible="false"
            android:inputType="phone"
            android:hint="Contact Number"
            android:text="Placeholder" />

        <TextView
            android:id="@+id/emailTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Email: " />

        <EditText
            android:id="@+id/emailET"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:cursorVisible="false"
            android:inputType="textEmailAddress"
            android:hint="E-mail"
            android:text="Placeholder" />

        <TextView
            android:id="@+id/addressTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Address: " />

        <EditText
            android:id="@+id/addressET1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:cursorVisible="false"
            android:inputType="text"
            android:text="Placeholder" />

        <EditText
            android:id="@+id/addressET2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:cursorVisible="false"
            android:inputType="text"
            android:text="Placeholder" />

        <EditText
            android:id="@+id/addressET3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:cursorVisible="false"
            android:inputType="text"
            android:text="Placeholder" />

        <TextView
            android:id="@+id/postalTV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Postal Code: " />

        <EditText
            android:id="@+id/postalET"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:cursorVisible="false"
            android:inputType="number"
            android:hint="Postal Code"
            android:text="Placeholder" />

        <Button
            android:id="@+id/updateButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/custom_border"
            android:text="Update My Particulars"
            android:textColor="#2470FA"
            android:textStyle="bold" />

        <Button 
            android:id="@+id/unlinkButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:background="@drawable/custom_border"
            android:text="Unlink Account"
            android:textColor="#2470FA"
            android:textStyle="bold"/>

    </LinearLayout>

</FrameLayout>

最后,这是logcat:

10-17 01:35:17.404: I/MYTAG(13333): ProfileSectionFragment.onCreateView / savedInstanceState == null
10-17 01:35:17.414: D/TextView(13333): Constructor - Got Res id for appearance for textColorPrimaryInverse
10-17 01:35:17.414: W/ResourceType(13333): Skipping entry 0x106009f in package table 0 because it is not complex!
10-17 01:35:17.414: D/TextView(13333): Constructor - Got appearance for textColorPrimaryInverse
10-17 01:35:17.414: D/TextView(13333): Constructor -- Got mEditTextBackgroundColor
10-17 01:35:17.424: D/TextView(13333): Constructor - Got Res id for appearance for textColorPrimaryInverse
10-17 01:35:17.424: W/ResourceType(13333): Skipping entry 0x106009f in package table 0 because it is not complex!
10-17 01:35:17.424: D/TextView(13333): Constructor - Got appearance for textColorPrimaryInverse
10-17 01:35:17.424: D/TextView(13333): Constructor -- Got mEditTextBackgroundColor
10-17 01:35:17.434: D/TextView(13333): Constructor - Got Res id for appearance for textColorPrimaryInverse
10-17 01:35:17.434: W/ResourceType(13333): Skipping entry 0x106009f in package table 0 because it is not complex!
10-17 01:35:17.434: D/TextView(13333): Constructor - Got appearance for textColorPrimaryInverse
10-17 01:35:17.434: D/TextView(13333): Constructor -- Got mEditTextBackgroundColor
10-17 01:35:17.434: D/TextView(13333): Constructor - Got Res id for appearance for textColorPrimaryInverse
10-17 01:35:17.434: W/ResourceType(13333): Skipping entry 0x106009f in package table 0 because it is not complex!
10-17 01:35:17.434: D/TextView(13333): Constructor - Got appearance for textColorPrimaryInverse
10-17 01:35:17.434: D/TextView(13333): Constructor -- Got mEditTextBackgroundColor
10-17 01:35:17.434: D/TextView(13333): Constructor - Got Res id for appearance for textColorPrimaryInverse
10-17 01:35:17.434: W/ResourceType(13333): Skipping entry 0x106009f in package table 0 because it is not complex!
10-17 01:35:17.434: D/TextView(13333): Constructor - Got appearance for textColorPrimaryInverse
10-17 01:35:17.434: D/TextView(13333): Constructor -- Got mEditTextBackgroundColor
10-17 01:35:17.444: D/TextView(13333): Constructor - Got Res id for appearance for textColorPrimaryInverse
10-17 01:35:17.444: W/ResourceType(13333): Skipping entry 0x106009f in package table 0 because it is not complex!
10-17 01:35:17.444: D/TextView(13333): Constructor - Got appearance for textColorPrimaryInverse
10-17 01:35:17.444: D/TextView(13333): Constructor -- Got mEditTextBackgroundColor
10-17 01:35:17.464: I/MYTAG(13333): ProfileSectionFragment.onStart
10-17 01:35:17.464: I/MYTAG(13333): ProfileSectionFragment.onResume
10-17 01:35:17.464: I/MYTAG(13333): TicketSectionFragment.onCreateView / savedInstanceState == null
10-17 01:35:17.484: I/MYTAG(13333): TicketSectionFragment.onStart
10-17 01:35:17.484: I/MYTAG(13333): TicketSectionFragment.onResume
10-17 01:35:17.544: I/GetProfile(13333): Error at get profile
10-17 01:35:18.034: I/MYTAG(13333): EventsSectionFragment.onPause
10-17 01:35:18.034: I/MYTAG(13333): EventsSectionFragment.onStop
10-17 01:35:18.034: I/MYTAG(13333): EventsSectionFragment.onDestroyView
10-17 01:35:18.034: I/MYTAG(13333): EcardSectionFrontFragment.onPause
10-17 01:35:18.034: I/MYTAG(13333): EcardSectionFrontFragment.onStop
10-17 01:35:18.034: I/MYTAG(13333): EcardSectionFrontFragment.onDestroyView
10-17 01:35:18.054: I/dalvikvm(13333): Could not compile trace for Ljava/util/Arrays;fill, offset 7
10-17 01:35:18.054: I/dalvikvm(13333): ++++++++++++++++++++++++++++++++++++++++++++
10-17 01:35:18.054: I/dalvikvm(13333): JIT_INFO: ME Issues while compiling trace  Ljava/util/Arrays;fill, offset 7
10-17 01:35:18.054: I/dalvikvm(13333):  The trace provoked a spill.
10-17 01:35:18.054: I/dalvikvm(13333): Trying less registerization from 1 to 0
10-17 01:35:18.064: I/dalvikvm(13333): Could not compile trace for Ljava/util/Arrays;fill, offset 5
10-17 01:35:18.064: I/dalvikvm(13333): ++++++++++++++++++++++++++++++++++++++++++++
10-17 01:35:18.064: I/dalvikvm(13333): JIT_INFO: ME Issues while compiling trace  Ljava/util/Arrays;fill, offset 5
10-17 01:35:18.064: I/dalvikvm(13333):  The trace provoked a spill.
10-17 01:35:18.064: I/dalvikvm(13333): Trying less registerization from 1 to 0

如果您还需要,请随时说出来。

编辑: 这是较新的logcat信息:

10-17 02:03:22.904: I/profile json(14252): {"UserID":"S7182827A","Gender":"F","Password":"","Name":"LIM WOAN CHIN","AddressLineOne":"12","AddressLineTwo":"COLLEGE ROAD","AddressLineThree":"","PostalCode":"169852","HomeTel":"07-4346084","Email":"","DateOfBirth":"","UserType":"ALUM","GradYear":"1996","SMSCode":"53348","SMSCodeLastUpdated":"10/16/2014 10:08:08 PM"}
10-17 02:03:22.904: W/System.err(14252): org.json.JSONException: No value for {"UserID":"S7182827A","Gender":"F","Password":"","Name":"LIM WOAN CHIN","AddressLineOne":"12","AddressLineTwo":"COLLEGE ROAD","AddressLineThree":"","PostalCode":"169852","HomeTel":"07-4346084","Email":"","DateOfBirth":"","UserType":"ALUM","GradYear":"1996","SMSCode":"53348","SMSCodeLastUpdated":"10/16/2014 10:08:08 PM"}
10-17 02:03:22.904: W/System.err(14252):    at org.json.JSONObject.get(JSONObject.java:355)
10-17 02:03:22.904: W/System.err(14252):    at org.json.JSONObject.getJSONObject(JSONObject.java:574)
10-17 02:03:22.904: W/System.err(14252):    at nyp.edu.sg.alumnigo.asynctask.GetProfileAsyncTask.getData(GetProfileAsyncTask.java:106)
10-17 02:03:22.904: W/System.err(14252):    at nyp.edu.sg.alumnigo.asynctask.GetProfileAsyncTask.doInBackground(GetProfileAsyncTask.java:44)
10-17 02:03:22.904: W/System.err(14252):    at nyp.edu.sg.alumnigo.asynctask.GetProfileAsyncTask.doInBackground(GetProfileAsyncTask.java:1)
10-17 02:03:22.904: W/System.err(14252):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
10-17 02:03:22.904: W/System.err(14252):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-17 02:03:22.904: W/System.err(14252):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-17 02:03:22.904: W/System.err(14252):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-17 02:03:22.904: W/System.err(14252):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-17 02:03:22.904: W/System.err(14252):    at java.lang.Thread.run(Thread.java:841)
10-17 02:03:22.904: I/GetProfile(14252): Error at get profile

1 个答案:

答案 0 :(得分:0)

这行代码出了点问题:

JSONObject attr = jsonObj.getJSONObject(json);

json 是一把钥匙吗?

删除该行。然后你的if语句应该是:

  if(jsonObj.has("Name"))

而不是

  if(attr.has("Name"))

然后需要将attr的所有引用更改为jsonObj