0

I have an asyncTask to get the profile of a user. I want to run this task and get the data from the JSON object. Data as in user's emailId, password, location, mobile no etc. I want to show this in edit text when the fragment will get start.

getProfileAsyncTask

    public class GetProfileAsyncTask extends AsyncTask<String, Void, JSONObject> {
    String api;
    JSONObject jsonParams;
    private Context context;

    public GetProfileAsyncTask(Context context) {
        this.context = context;
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        try {
            api = context.getResources().getString(R.string.server_url) + "api/user/getprofile.php";

            jsonParams = new JSONObject();
            String username = params[0];  // params[0] is username
            jsonParams.put("user", username);

            ServerRequest request = new ServerRequest(api, jsonParams);
            return request.sendRequest();
        } catch(JSONException je) {
            return Excpetion2JSON.getJSON(je);
        }
    }  //end of doInBackground

    @Override
    protected void onPostExecute(JSONObject response) {
        super.onPostExecute(response);
        Log.e("ServerResponse", response.toString());
        try {
            int result = response.getInt("result");
            String message = response.getString("message");
            if (result == 0 ) {
                Toast.makeText(context, message, Toast.LENGTH_LONG).show();
                //code after getting profile details goes here
            } else {
                Toast.makeText(context, message, Toast.LENGTH_LONG).show();
                //code after failed getting profile details goes here
            }
        } catch(JSONException je) {
            je.printStackTrace();
            Toast.makeText(context, je.getMessage(), Toast.LENGTH_LONG).show();
        }
    } //end of onPostExecute
}

I tried to access the data from JSON object but it is throwing JSON exception and null value for userUsername.

Fragment :

public class BasicInformationFragment extends android.support.v4.app.Fragment {

    EditText email,pass,mobile,code;
    public static final String MyPREFERENCES = "MyPrefs" ;
    SharedPreferences sharedpreferences;
    JSONObject jsonParams;
    String userId,password,mobileNumber;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_basic_information,
                container, false);

        try {


            GetProfileAsyncTask task = new GetProfileAsyncTask(getActivity());

            JSONObject obj = new JSONObject();

            userId = obj.getString("userUsername");

            task.execute(userId);

            password = obj.getString("userPassword");
            mobileNumber = obj.getString("userMobileNo");


            email = (EditText) view.findViewById(R.id.edt_email);
            pass = (EditText) view.findViewById(R.id.edt_pass);
            mobile = (EditText) view.findViewById(R.id.edt_mobile);
            code = (EditText) view.findViewById(R.id.edt_code);


            email.setText(userId);
            pass.setText(password);
            mobile.setText(mobileNumber);


        }


        catch (JSONException e)
        {}
        return view;
    }

}

user parameters:

    HashMap<String, String> params = new HashMap<String, String>();
    params.put("userUsername", "user1");
    params.put("userPassword", "user1");
    params.put("gender", "M");
    params.put("birthDate", "1986/7/12");
    params.put("religion", "Hindu");
    params.put("nationality", "Indian");
    params.put("motherTongue", "Marathi");
    params.put("birthPlace", "Pune");
    params.put("userCountry", "India");
    params.put("userState", "Maharashtra");
    params.put("userCity", "Nashik");
    params.put("userPincode", "422101");
    params.put("userEmailid", "user1@gmail.com");
    params.put("userMobileNo", "9696323252");
    params.put("userPhoto", userPhoto);

How to do this?

0 Answers0