简体   繁体   English

Android Volley-如何发送原始文本并获取json响应

[英]Android Volley- How to send raw text and get json response

I am new to android and using volley to communicate with web services. 我是android的新手,并使用凌空与Web服务进行通信。 Postman details 邮递员详细信息

I found sending a raw text to the web service will only work on postman.I try using grant_type=password&username=myusername&password=mypassowrd getting myusername and mypassword from two editText from my mainActivity, . 我发现将原始文本发送到Web服务仅适用于邮递员。我尝试使用grant_type = password&username = myusername&password = mypassowrd从mainActivity的两个editText中获取myusername和mypassword。 Is there any better way to achieve this? 有没有更好的方法来实现这一目标?

public class MainActivity extends AppCompatActivity {

Button btnLogin,btnFetch,btnExit;
EditText editTxtUsr,editTxtPass;
TextView txtView;
String uRL ="****";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnLogin=findViewById(R.id.buttonLogin);
    btnFetch=findViewById(R.id.buttonFetch);
    btnExit=findViewById(R.id.buttonExit);
    editTxtUsr=findViewById(R.id.editTextUsrname);
    editTxtPass=findViewById(R.id.editTextPassword);
    txtView=findViewById(R.id.textViewData);


    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String wsURL = uRL + "/authtoken";
            //final String appData = "grant_type=password&username=" + editTxtUsr + "&password=" + editTxtPass;

            StringRequest request = new StringRequest(Request.Method.POST, wsURL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (response.equals("true")) {
                        Toast.makeText(MainActivity.this, "Login Successful", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this, "Incorrect ", Toast.LENGTH_SHORT).show();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(MainActivity.this, "Some error occured" + volleyError, Toast.LENGTH_SHORT).show();
                }
            }) {
                @Override
                public Map<String,String> getHeaders() throws AuthFailureError {
                    Map<String, String> headers = new HashMap<>();
                    headers.put("Content-Type", "application/text");
                    headers.put("charset", "TYPE_UTF8_CHARSET");
                    return headers;

                }

                @Override
                protected Map<String,String> getParams() throws AuthFailureError {
                    Map<String, String> parameters = new HashMap<>();
                    parameters.put("grand_type", "SET_VALUE");
                    parameters.put("Username", editTxtUsr.getText().toString());
                    parameters.put("Password", editTxtPass.getText().toString());
                    return parameters;

                }
            };

            RequestQueue rQueue= Volley.newRequestQueue(MainActivity.this);
            rQueue.add(request);
        }
    });

    btnExit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });
}
}

I have tried various ways to achieve this, but with no luck. 我已经尝试了各种方法来实现这一目标,但是没有运气。 That's a sample of my code that I use to try to connect to the web service. 这是我的代码示例,用于尝试连接到Web服务。

I get [253] BasicNetwork.performRequest: Unexpected response code 400 for... 我得到[253] BasicNetwork.performRequest:意外的响应代码400,用于...

I searched thoroughly through StackOverflow but I can't seem to find the correct answer to my question. 我通过StackOverflow进行了彻底搜索,但似乎找不到正确的答案。 Any help would be appreciated. 任何帮助,将不胜感激。

Its been days I have been working on this but after I posted my question, I found the solution after some tries. 我一直在努力解决这一问题,但是在发布问题后,经过一些尝试,我找到了解决方案。 The answer is simpler than I have thought. 答案比我想象的简单。 What you need to do is make an objectrequest and override the body with the text you want to post. 您需要做的是发出一个objectrequest并用要发布的文本覆盖正文。 Nothing had to change in headers or parameters. 标头或参数无需更改。 I still can't find an answer using Stringrequest. 我仍然无法使用Stringrequest找到答案。

                @Override
                public byte[] getBody() {
                    try {
                        return requestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                        return null;
                    }
                }

Where requestBody is the raw text you want to send as a string. 其中requestBody是要作为字符串发送的原始文本。

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

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