I am trying to parse json but unable to get. Can anybody help me to find out how to to get this:
{
"firstName": "****",
"headline": "Software Engineer",
"id": "Y8gyiQS0gM",
"lastName": "****",
"location": {
"country": {"code": "in"},
"name": "Bengaluru Area, India"
},
"pictureUrl": "",
"siteStandardProfileRequest": {"url": ""}
}
I used this code to get but didn't get any result in log.
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet("url");
HttpResponse response = httpclient.execute(httpget);
String jsonResp=EntityUtils.toString(response.getEntity());
Log.d("HTTP","Rsponse : "+ jsonResp);
JSONObject jsonObject = new JSONObject(jsonResp);
String firstname = jsonObject.getString("firstName");
String id = jsonObject.getString("id");
String headline = jsonObject.getString("headline");
String lastName = jsonObject.getString("lastName");
//String pictureUrl = jsonObject2.getString("pictureUrl");
Log.d("HTTP", "firstname : " + firstname.toString() + "id" + id.toString());
} ...
I have wrote this code to view the user profile. But didn't get anything as result.
EDIT:
public class ViewProfileActivity extends ListActivity {
Button userProfile;
TextView tv;
private static final String TAG_ID = "id";
private static final String TAG_FNAME = "firstname";
private static final String TAG_LNAME = "lastname";
private static final String TAG_HLINE = "headline";
private static final String TAG_PURL = "pictureUrl";
private static final String TAG_URL = "url";
private ProgressDialog pDialog;
ArrayList<HashMap<String, String>> contactList = null;
String url="...."
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_user);
userProfile = (Button) findViewById(R.id.view);
tv=(TextView)findViewById(R.id.textView1);
userProfile.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
new GetContacts().execute();
}
class GetContacts extends AsyncTask<Void, String, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(ViewProfileActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(
"url");
HttpResponse response = httpclient.execute(httpget);
String jsonResp = EntityUtils.toString(response.getEntity());
Log.d("HTTP","Rsponse : "+ jsonResp);
JSONObject jsonObject = new JSONObject(jsonResp);
String firstname = jsonObject.getString("firstName");
String id = jsonObject.getString("id");
String headline = jsonObject.getString("headline");
String lastname = jsonObject.getString("lastName");
String pictureUrl = jsonObject.getString("pictureUrl");
JSONObject jsonObject2 = jsonObject
.getJSONObject("siteStandardProfileRequest");
String url = jsonObject2.getString("url");
Log.d("HTTP", "firstname : " + firstname + "\n" + "id :"
+ id + "\n" + "headline : " + headline + "\n"
+ "lastName :" + lastname + "\n" + "pictureUrl :"
+ pictureUrl + "\n" + "Url :" + url);
HashMap<String, String> contact = new HashMap<String, String>();
contact.put(TAG_ID, id);
contact.put(TAG_FNAME, firstname);
contact.put(TAG_LNAME, lastname);
contact.put(TAG_HLINE, headline);
contact.put(TAG_PURL, pictureUrl);
contact.put(TAG_URL, url);
contactList.add(contact);
tv.setText((CharSequence) contactList);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
LinkedInSampleActivity.this, contactList,
R.layout.list_item, new String[] {TAG_URL,TAG_FNAME}, new int[]{R.id.imageView1,R.id.textView1 });
setListAdapter(adapter);
}
}
});
}
}