为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过Android请求发送JSON对象?

如何通过Android请求发送JSON对象?

三国纷争 2019-06-19 10:46:07
如何通过Android请求发送JSON对象?我想发送以下JSON文本{"Email":"aaa@tbbb.com","Password":"123456"}并读取响应。我知道怎么读JSON。问题是上面的JSON对象必须以变量名发送jason.我怎样才能在Android上做到这一点呢?创建请求对象、设置内容标题等步骤有哪些。
查看完整描述

3 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

Android没有发送和接收HTTP的特殊代码,您可以使用标准Java代码。我建议使用Android附带的ApacheHTTP客户机。下面是我用来发送HTTPPOST的代码片段。

我不明白在名为“Jason”的变量中发送对象与任何事情有什么关系。如果您不确定服务器到底想要什么,请考虑编写一个测试程序,将各种字符串发送到服务器,直到您知道它需要采用何种格式为止。

int TIMEOUT_MILLISEC = 10000;  // = 10 secondsString postMessage="{}"; //HERE_YOUR_POST_STRING.HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);HttpPost request = new HttpPost(serverUrl);request.setEntity(new ByteArrayEntity(
    postMessage.toString().getBytes("UTF8")));HttpResponse response = client.execute(request);


查看完整回答
反对 回复 2019-06-19
?
慕标5832272

TA贡献1966条经验 获得超4个赞

如果使用ApacheHTTP客户端,从Android发送JSON对象很容易。下面是一个关于如何实现的代码示例。您应该为网络活动创建一个新线程,以避免锁定UI线程。

    protected void sendJson(final String email, final String pwd) {
        Thread t = new Thread() {

            public void run() {
                Looper.prepare(); //For Preparing Message Pool for the child Thread
                HttpClient client = new DefaultHttpClient();
                HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                HttpResponse response;
                JSONObject json = new JSONObject();

                try {
                    HttpPost post = new HttpPost(URL);
                    json.put("email", email);
                    json.put("password", pwd);
                    StringEntity se = new StringEntity( json.toString());  
                    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                    post.setEntity(se);
                    response = client.execute(post);

                    /*Checking response */
                    if(response!=null){
                        InputStream in = response.getEntity().getContent(); //Get the data in the entity
                    }

                } catch(Exception e) {
                    e.printStackTrace();
                    createDialog("Error", "Cannot Estabilish Connection");
                }

                Looper.loop(); //Loop in the message queue
            }
        };

        t.start();      
    }

你也可以用谷歌gson发送和检索JSON。


查看完整回答
反对 回复 2019-06-19
?
Cats萌萌

TA贡献1805条经验 获得超9个赞

public void postData(String url,JSONObject obj) {
    // Create a new HttpClient and Post Header

    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 10000);
    HttpClient httpclient = new DefaultHttpClient(myParams );
    String json=obj.toString();

    try {

        HttpPost httppost = new HttpPost(url.toString());
        httppost.setHeader("Content-type", "application/json");

        StringEntity se = new StringEntity(obj.toString()); 
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se); 

        HttpResponse response = httpclient.execute(httppost);
        String temp = EntityUtils.toString(response.getEntity());
        Log.i("tag", temp);


    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    }}


查看完整回答
反对 回复 2019-06-19
  • 3 回答
  • 0 关注
  • 818 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信