如何使用post方法在ROR的def create函数中从客户端获取数据

时间:2013-05-29 05:16:15

标签: ruby-on-rails json ruby-on-rails-3 http post

我正在努力解决这些问题,如何从客户端获取数据,如何在def create function中使用post方法,从客户端获取数据。 客户端编码是android,服务器端代码是ROR。

服务器端编码在ror

def create 
 respond_to do |format|

   @post = Post.new(params[:post])
    if @post.save
      flash[:notice] = "Prayer Successfully created."
      @posts = Post.paginate(page: params[:page],:per_page => 5)

      format.json{ render :json => @post, :status => :created }
    else 
      flash[:notice] = "Error"
      @posts = Post.paginate(page: params[:page],:per_page => 5)

      format.json{ render :json => @post, :status => :created }
    end
 end 
end

  def create_a_post 
   content = JSON.parse request.body.read 
     @post = Post.new[:content] 
      @post.save 
end

这里我创建了一个新的动作作为抓取的create_a_post,但我觉得我们必须从def create函数本身获取,是否需要代码来获取数据。

如何使用def crate功能,从客户端获取数据

我将在此提及我的客户端代码

public class HomeLayoutActivity extends Activity implements OnClickListener{

        private EditText value;
        private Button btn;
        private ProgressBar pb;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.home_layout);
            value=(EditText)findViewById(R.id.editText1);
            btn=(Button)findViewById(R.id.button1);
            pb=(ProgressBar)findViewById(R.id.progressBar1);
            pb.setVisibility(View.GONE);
            btn.setOnClickListener(this);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.home_layout, menu);
            return true;
        }

        public void onClick(View v) {
            // TODO Auto-generated method stub
                if(value.getText().toString().length()<1){

                    // out of range
                    Toast.makeText(this, "please enter something", Toast.LENGTH_LONG).show();
                }else{
                    pb.setVisibility(View.VISIBLE);
                    new MyAsyncTask().execute(value.getText().toString());      
                }


        } 

        private class MyAsyncTask extends AsyncTask<String, Integer, Double>{

            @Override
            protected Double doInBackground(String... params) {
                // TODO Auto-generated method stub
                postData(params[0]);
                return null;
            }

            protected void onPostExecute(Double result){
                pb.setVisibility(View.GONE);
                Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
            }
            protected void onProgressUpdate(Integer... progress){
                pb.setProgress(progress[0]);
            }

            public void postData(String valueIWantToSend) {
                // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://floating-wildwood-1154.herokuapp.com/posts/create_a_post");

                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("content", valueIWantToSend));
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    httppost.setHeader("Content-Type", "application/json");
                    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

                }
            }
        }
    }

0 个答案:

没有答案