Java 如何在单击按钮并希望在post方法中再次发送该id后,在JSON中获取id
实际上,我正在解析json id、内容、标题、计数。但我不想显示id,但单击按钮后,它必须获取id值,我必须将该id发送到服务器端 这是我的json解析值:Java 如何在单击按钮并希望在post方法中再次发送该id后,在JSON中获取id,java,android,json,post,get,Java,Android,Json,Post,Get,实际上,我正在解析json id、内容、标题、计数。但我不想显示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
{"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.
Activity.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);
}
});
公共类MainActivity扩展活动实现FetchDataListener、OnClickListener
{
私有静态最终整数活动_CREATE=0;
私人对话;
ListView lv;
私人清单项目;
私人按钮btnGetSelected;
专用按钮计数;
公共整数计数;
私有字符串stringVal;
私有文本视图值;
//私人项目设计顾问;
//私有SimpleCursorAdapter数据适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u list\u项);
//mDbHelper=新项目数据采集程序(本);
//mDbHelper.open();
//fillData();
//registerForContextMenu(getListView());
praycount=(按钮)findViewById(R.id.pray);
praycount.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
计数++;
stringVal=Integer.toString(计数);
value.setText(stringVal);
如果(value.getText().toString().length()为此,您可以使用javascript读取所选特定属性的id,并使用该id显示所需的任何详细信息。在我必须使用javascript的地方,您能告诉我代码是什么吗如果您解析了json,只需将其保存到arraylist,然后获取所选位置例如:arraylist.get(position).get(“id”);在应用程序的客户端使用java脚本,并将所选值传递给服务器。