java - 单击按钮后如何获取 JSON 中的 id,并希望在 post 方法中再次发送该 id

标签 java android json post get

实际上我正在解析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.

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);


            }


        });

实际上我想要从一个 json 中获取一个 id,我已经提到过,单击按钮选择 id 后,我想在另一个 json url 中作为 post 方法再次发送。

最佳答案

为此,您可以使用 javascript 读取所选特定属性的 ID,并使用它可以显示您想要的详细信息。

关于java - 单击按钮后如何获取 JSON 中的 id,并希望在 post 方法中再次发送该 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17167108/

相关文章:

java - JSON获取没有对象的数组

java - 父类(super class)是每个子类的模板吗

java - 文本文件中的微型 GP 输出

java - Spring Boot路径参数url编码问题

java - android location apis : FusedLocationApi. getLastLocation 给出 null

ios - 无法将类型 '__NSDictionaryM' (0x10b708d38) 的值转换为 'NSMutableArray' (0x10b708978)。并且无法在 Collection View 中查看我的数据

java - 删除 recyclerView 和 SQLite 数据库上的两行列表项

android - 如何静默添加通知?

android - 如何在 MPAndroidChart 中访问 XLabel

java - 从 URL 读取 90 MB 的巨大文件