繁体   English   中英

单击按钮后如何在JSON中获取ID并想在post方法中再次发送该ID

[英]How i have to get the id in JSON after click the button and want to send that id again in post method

实际上我正在解析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,并使用它来显示所需的详细信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM