2014-10-16 142 views
0

我在这里遇到问题。我试图检索一个配置文件,以便我可以查看它,然后根据需要进行更新。但是,它一直在返回,得到配置文件时出错。但是,没有堆栈跟踪,只有我的标签进行调试。谁能帮我?异步任务获取错误

这里的片段:

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 
+0

貌似还有就是当你打电话的getData一些错误。当你在getDate方法中捕获异常时,可能会添加更多的日志标记? – alpinescrambler 2014-10-16 16:54:51

+0

看起来我有更多的日志需要显示。我会添加它。 – 2014-10-16 17:04:26

+0

你期望什么'JSONObject jsonObj = new JSONObject(json); JSONObject attr = jsonObj.getJSONObject(json);'做什么? – njzk2 2014-10-16 17:08:59

回答

0

某处有问题,这行代码:

JSONObject attr = jsonObj.getJSONObject(json); 

JSON的关键?

删除该行。那么你的if语句应该是:

if(jsonObj.has("Name")) 

,而不是

if(attr.has("Name")) 

,然后attr的所有引用必须改为jsonObj

+0

谢谢!你是一个救星! – 2014-10-16 17:22:16

+0

只是一个快速的qn。除非按下“更新我的详细信息”按钮,否则如何使textView不可编辑? – 2014-10-16 17:24:27