0

我正在使用 Ruby on Rails 3 应用程序,我想从我的 Android 应用程序发布数据。我已经设置了正确的标题"Content-Type: application/json" and "Accept: application/json".

我在我的控制器中获取参数params["name"],这些参数params["description"]是从 Android 应用程序发布的。

但我无法在我的控制器中获取 params[:test] 之类的参数。

如果我使用“curl”发布数据,例如:

curl --verbose --header "Accept: application/json" --header "Content-type: application/json" --request POST --data '{"name":"I belong to AAA","description":"AAA"}' myurl/tests

然后我可以获取数据params[:test]并将值正确保存到数据库中,并且我不必在我的创建方法中执行任何其他操作,例如 json 解析等。

当我从 Android 应用程序发布数据时,如何实现同样的目标?

任何想法?

4

1 回答 1

0
Following is my Android code and Response:

public InputStream HTTP_POST(String URL,List<NameValuePair> mNameValuePairs) 
{
    HttpClient mHttpClient = new DefaultHttpClient();
    try 
    {
        URL url = new URL("http", "192.168.0.126", 3000, "/tests.json");
        Log.d("URL", url.toString());
        /*Log.d("URL", URL);
        URI baseURL = new URI(URL);*/
        HttpPost mHttpPost = new HttpPost(url.toURI());

        if(mNameValuePairs!=null)
        {
mHttpPost.setEntity(new UrlEncodedFormEntity(mNameValuePairs));
        }

        HttpResponse mHttpResponse = mHttpClient.execute(mHttpPost);
        Log.d("status code",""+getResponseStatusCode(mHttpResponse));

        if(getResponseStatusCode(mHttpResponse) == 200) 
        {
            HttpEntity mHttpEntity = mHttpResponse.getEntity();
            return mHttpEntity.getContent();
        }
        else 
        {
            Log.d("Error", ""+getResponseStatusCode(mHttpResponse));
            return null;
        }
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
        return null;
    } 
}
 ----------------------
 private class PostTest extends AsyncTask<String, Void, String>
{
    private ProgressDialog mProgressDialog = null;
    String name = null;
    String details = null;
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(LoginForm.this);
        mProgressDialog.setMessage(Constant.MSG_PROGRESSBAR_WAITING);
        mProgressDialog.setCancelable(false);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();

        name = editTextLogin.getText().toString().trim();
        details = editTextPassword.getText().toString().trim();

    }

    @Override
    protected String doInBackground(String... params) 
    {
        try 
        {
            String baseUrl = "http://192.168.0.126:3000/tests/";

    List<NameValuePair> mNameValuePairs = new ArrayList<NameValuePair>();
            mNameValuePairs.add(new BasicNameValuePair("name", name));
    mNameValuePairs.add(new BasicNameValuePair("description", details));
return common.InputStreamToString(common.HTTP_POST(baseUrl, mNameValuePairs));
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    protected void onPostExecute(String response)
    {
        super.onPostExecute(response);
        mProgressDialog.dismiss();
        try 
        {
            if(response != null)
            {
                Log.d("response", response);

            }
            else
            {
Toast.makeText(LoginForm.this, "Login Failed...  Try again", Toast.LENGTH_SHORT).show();
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }
}
--------------------------
     Response which I got from this code:
{"name":null,"created_at":"2013-01-11T05:31:39Z","updated_at":"2013-01-11T05:31:39Z","description":null,"id":106}
于 2013-01-11T06:05:03.760 回答