【问题标题】:Parse url with curly brackets in Android App在 Android 应用程序中用大括号解析 url
【发布时间】:2018-04-07 13:16:26
【问题描述】:

我如何解析带有大括号的网址,例如 http://example.com/api/login/{username}/{password} 在 Android 应用程序中。

普通的凌空发帖请求返回 html。但我需要 JSON。

在 Android 应用中集成登录 API

【问题讨论】:

  • 这真的不清楚你的要求。您是想从 url 中获取用户名和密码,还是要发送 GET 请求? JSON在哪里参与?请澄清一下。
  • 我想通过将用户名和密码作为参数传递给带有花括号的上述 url 来从 url 获得成功响应,但我得到的是网站的 html 响应而不是 Json 响应。
  • 你有什么问题?
  • 我的问题是..这是登录 webservice-example.com/api/login {username}/{password}。我有一个带有用户名和密码的登录屏幕我想将此参数发布到上述网址.

标签: android json android-volley


【解决方案1】:

如果我猜对了,你要做的就是发送一个 GET 登录请求。

以下代码可以帮助您(不建议使用 GET 登录,请改用 POST。我提供 GET ex 因为这是您的要求):

final TextView mTextView = (TextView) findViewById(R.id.text);
// ...

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://example.com/api/login/your_username/your_password";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});

【讨论】:

  • 感谢您的评论。您能否分享帖子请求,我还有一个疑问..检查我的网址有用户名和密码的大括号。
  • 大括号没有做任何改变。 post example。如果此处的某个答案对您的问题有所帮助,请考虑在左侧标上复选标记,让人们知道该问题已得到解答。
猜你喜欢
  • 1970-01-01
  • 2013-06-20
  • 2018-01-17
  • 2011-04-12
  • 2011-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多