相关疑难解决方法(0)

如何使用Android在Request上发送JSON对象?

我想发送以下JSON文本

{"Email":"aaa@tbbb.com","Password":"123456"}
Run Code Online (Sandbox Code Playgroud)

到Web服务并阅读响应.我知道如何阅读JSON.问题是上述JSON对象必须以变量名发送jason.

我怎么能从android做到这一点?有哪些步骤,例如创建请求对象,设置内容标题等.

post android json httprequest

115
推荐指数
5
解决办法
23万
查看次数

带有JSON的Android POST请求

经过研究,我仍然无法向服务器发送JSON POST请求.

我已经尝试了一些较旧的答案:

Java - 通过POST方法轻松发送HTTP参数

[Android] -POST Json with HttpUrlConnection

通过HttpUrlConnection发布在服务器上注册用户数据的请求

在android中通过http post方法发送json对象

如何使用Android在Request上发送JSON对象?

我目前的代码是:

FloatingActionButton btn_sendMsg = (FloatingActionButton) findViewById(R.id.btn_sendMsg);
    btn_sendMsg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            /*Snackbar.make(view, "Sendevorgang...", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();*/

            createMsg();
        }
    });

private void createMsg() {
    Message message = new Message(txtbox_msg.getText().toString(), "testUser");

        AsyncT asyncT = new AsyncT();
        asyncT.execute(message);

}
Run Code Online (Sandbox Code Playgroud)

AsyncT.java:

@Override
protected Message doInBackground(Message... params) {

    try {
        URL url = new URL("http://[ip]:[port]"); //in the real code, there is an ip and a port …
Run Code Online (Sandbox Code Playgroud)

java android json httpurlconnection

9
推荐指数
1
解决办法
4万
查看次数

标签 统计

android ×2

json ×2

httprequest ×1

httpurlconnection ×1

java ×1

post ×1