我有一个asyncTask来获取用户的个人资料。我想运行此任务并从JSON对象获取数据。用户的emailId,密码,位置,手机号码等数据。我想在片段开始时以编辑文本显示此内容。
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
}
我尝试从JSON对象访问数据,但是它为userUsername抛出了JSON异常和null值。
片段:
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;
}
}
用户参数:
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);
怎么做?