post方法中的Params错误,它只获得null值

时间:2013-06-12 07:21:40

标签: android ruby-on-rails ruby ruby-on-rails-3 post

从android(客户端)我发送数据到服务器端(ROR)post方法显示空值,问题可能在params。

serversidecode(ROR)

def create

 @post = Post.create(params[:post])
   #@post = Post.create(:content=>params[:post][":content"],:title=>params[:post][":title"])
   respond_to do |format|
    if @post.save
      @posts = Post.paginate(page: params[:page],:per_page => 10)
      format.json { render json: @post, status: :created }

    else
      @posts = Post.paginate(page: params[:page],:per_page => 10)
      format.json { render json: @post.errors, status: :unprocessable_entity }

    end
    @guest = Guest.new
    @users = User.paginate(page: params[:page],:per_page => 10)
 end
  end

客户机侧(androidcode)

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://dry-brushlands-3645.herokuapp.com/posts.json");

                try {
                    // Add your data
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("content", 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


                }
            }
        }
    }

Ror它只显示空值,而不是从android端获取正确的数据

1 个答案:

答案 0 :(得分:3)

尝试更换:

nameValuePairs.add(new BasicNameValuePair("content", valueIWantToSend));

使用:

nameValuePairs.add(new BasicNameValuePair("post[content]", valueIWantToSend));

您需要确保使用android提交的数据格式与HTML表单提交的格式相同。