这是 json 对象
JSONObject json = jsonParser.makeHttpRequest(PROFILE_URL, "POST",
params);
你需要返回json
in doInbackground
。返回的结果是 param toonPostExecute
Then
test(json);
然后在测试中你可以解析json
Edit:
你还需要改变
AsyncTask<String, String, String>
to
AsyncTask<String, String, JSONObject>
Then
protected JSONObject doInBackground(String... args) {
JSONObject json=null;
// Building Parameters
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(AdProfile.this);
String post_username = sp.getString("username", "anon");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", post_username));
// getting JSON string from URL
json = jsonParser.makeHttpRequest(PROFILE_URL, "POST",
params);
// Check your log cat for JSON reponse
Log.d("Profile JSON: ", json.toString());
try {
// profile json object
user = json.getJSONArray(TAG_PROFILE);
} catch (JSONException e) {
e.printStackTrace();
}
return json;
}
然后在onPostExecute中
@Override
protected void onPostExecute(JSONObject result) {
super.onPOstExecute(result);
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
test(result);
}
In test
public void test(JSONObject response){
{
// parse response here and set text to textview
}
Or
你解析吗doInbackgrond
在 onPostExecute 中更新 textview
Edit:
Code:
public class AddProfile extends Activity {
// All xml labels
TextView txtFname;
TextView txtMname;
TextView txtLname;
// Progress Dialog
private ProgressDialog pDialog;
// Profile json object
JSONArray user;
JSONObject hay;
// Profile JSON url
private static final String PROFILE_URL = "http://www.stagconnect.com/StagConnect/admin/TestProfile.php";
// ALL JSON node names
private static final String TAG_PROFILE = "user";
// private static final String TAG_ID = "id";
private static final String TAG_USERNAME = "username";
private static final String TAG_FIRSTNAME = "first_name";
private static final String TAG_MIDDLENAME = "middle_initial";
private static final String TAG_LASTNAME = "last_name";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.adminprofile);
txtFname = (TextView) findViewById(R.id.fname);
txtMname = (TextView) findViewById(R.id.lname);
txtLname = (TextView) findViewById(R.id.mname);
// Loading Profile in Background Thread
new LoadProfile().execute();
}
class LoadProfile extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AddProfile.this);
pDialog.setMessage("Loading profile ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting Profile JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
String json = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", "admin"));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PROFILE_URL);
httppost.setEntity(new UrlEncodedFormEntity(params));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
json = EntityUtils.toString(resEntity);
Log.i("Profile JSON: ", json.toString());
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
@Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
// dismiss the dialog after getting all products
pDialog.dismiss();
try
{
hay = new JSONObject(json);
JSONArray user = hay.getJSONArray("user");
JSONObject jb= user.getJSONObject(0);
String firstname = jb.getString("firstname");
String middlename = jb.getString("middlename");
String lastname = jb.getString("lastname");
// displaying all data in textview
txtFname.setText("Firstname: " + firstname);
txtMname.setText("Middle Name: " + middlename);
txtLname.setText("Last Name " + lastname);
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
The json
{ // json object node
"success": 1,
"message": "Post Available!",
"user": [ // json array user
{ // json object node
"designation": "Student Affairs Office in-charge",
"middlename": "",
"firstname": "Jose Patrick",
"lastname": "Ocampo"
}
]
}
浏览器快照