实际上我正在解析 json id、content、title、count。但我不想显示 id,但是在单击按钮后它必须获取 id 值,我必须将该 id 发送到服务器端。
这是我的 json 解析值:
{"post":[{"id":170,"title":"Exams","content":"pass","count":3},{"id":169,"title":"Exams","content":"pass","count":3}, From here i want to get the id after click the pray button and want to send that id in post method also.
活动.java
public class MainActivity extends Activity implements FetchDataListener,OnClickListener
{
private static final int ACTIVITY_CREATE=0;
private ProgressDialog dialog;
ListView lv;
private List<Application> items;
private Button btnGetSelected;
private Button praycount;
public int count;
private String stringVal;
private TextView value;
//private ProjectsDbAdapter mDbHelper;
//private SimpleCursorAdapter dataAdapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);
//mDbHelper = new ProjectsDbAdapter(this);
//mDbHelper.open();
//fillData();
//registerForContextMenu(getListView());
praycount = (Button) findViewById(R.id.pray);
praycount.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
stringVal = Integer.toString(count);
value.setText(stringVal);
if(value.getText().toString().length()<1){
// out of range
//Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
}else{
praydata(stringVal);
}
}});
lv =(ListView)findViewById(R.id.list);
btnGetSelected = (Button) findViewById(R.id.btnget);
btnGetSelected.setOnClickListener(this);
initView();
}
public void praydata(String valueIWantToSend) {
// Create a new HttpClient and Post Header.
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.ginfy.com/api/v1/posts/id.json");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("post[id]", valueIWantToSend));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//httppost.addHeader("Authorization","Basic "+authorization);
//httppost.addHeader("Content-Type","application/x-www-form-urlencoded");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setHeader("Accept", "application/json");
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
private void initView()
{
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://www.ginfy.com/api/v1/posts.json";
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
createProject();
return super.onMenuItemSelected(featureId, item);
}
private void createProject() {
Intent i = new Intent(this, AddPrayerActivity.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
initView();
}
@Override
public void onFetchComplete(List<Application> data)
{
this.items = data;
// dismiss the progress dialog
if ( dialog != null )
dialog.dismiss();
// create new adapter
ApplicationAdapter adapter = new ApplicationAdapter(this, data);
// set the adapter to list
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
CheckBox chk = (CheckBox) view.findViewById(R.id.checkbox);
Application bean = items.get(position);
if (bean.isSelected()) {
bean.setSelected(false);
chk.setChecked(false);
} else {
bean.setSelected(true);
chk.setChecked(true);
}
}
});
}
// Toast is here...
private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onFetchFailure(String msg)
{
// dismiss the progress dialog
if ( dialog != null )
dialog.dismiss();
// show failure message
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
@Override
public void onClick(View v) {
StringBuffer sb = new StringBuffer();
// Retrive Data from list
for (Application bean : items) {
if (bean.isSelected()) {
sb.append("Title:");
sb.append(Html.fromHtml(bean.getTitle()));
sb.append(",Content:");
sb.append(Html.fromHtml(bean.getContent()));
sb.append("\n");
}
}
showAlertView(sb.toString().trim());
}
@SuppressWarnings("deprecation")
private void showAlertView(String str) {
AlertDialog alert = new AlertDialog.Builder(this).create();
final String strContactList = str.substring(0, str.length());
if (TextUtils.isEmpty(str)) {
alert.setTitle("Not Selected");
alert.setMessage("No One is Seleceted!!!");
} else {
// Remove , end of the name
alert.setTitle("Selected");
alert.setMessage(strContactList);
}
alert.setButton("sms", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//sendSMS();
/*Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", strContactList);
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
finish();*/
Intent intent1=new Intent(MainActivity.this,SendSMSActivity.class);
//Log.d("test","strContactList: "+strContactList);
intent1.putExtra("firstKeyName", strContactList);
startActivity(intent1);
}
});
实际上,我希望我想从一个 json 中获取一个 id,我已经提到过,在单击按钮选择 id 后,我想在另一个 json url 中作为 post 方法再次发送。