第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何通過(guò)Android請(qǐng)求發(fā)送JSON對(duì)象?

如何通過(guò)Android請(qǐng)求發(fā)送JSON對(duì)象?

三國(guó)紛爭(zhēng) 2019-06-19 10:46:07
如何通過(guò)Android請(qǐng)求發(fā)送JSON對(duì)象?我想發(fā)送以下JSON文本{"Email":"aaa@tbbb.com","Password":"123456"}并讀取響應(yīng)。我知道怎么讀JSON。問(wèn)題是上面的JSON對(duì)象必須以變量名發(fā)送jason.我怎樣才能在Android上做到這一點(diǎn)呢?創(chuàng)建請(qǐng)求對(duì)象、設(shè)置內(nèi)容標(biāo)題等步驟有哪些。
查看完整描述

3 回答

?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

Android沒(méi)有發(fā)送和接收HTTP的特殊代碼,您可以使用標(biāo)準(zhǔn)Java代碼。我建議使用Android附帶的ApacheHTTP客戶機(jī)。下面是我用來(lái)發(fā)送HTTPPOST的代碼片段。

我不明白在名為“Jason”的變量中發(fā)送對(duì)象與任何事情有什么關(guān)系。如果您不確定服務(wù)器到底想要什么,請(qǐng)考慮編寫一個(gè)測(cè)試程序,將各種字符串發(fā)送到服務(wù)器,直到您知道它需要采用何種格式為止。

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);


查看完整回答
反對(duì) 回復(fù) 2019-06-19
?
慕標(biāo)5832272

TA貢獻(xiàn)1966條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果使用ApacheHTTP客戶端,從Android發(fā)送JSON對(duì)象很容易。下面是一個(gè)關(guān)于如何實(shí)現(xiàn)的代碼示例。您應(yīng)該為網(wǎng)絡(luò)活動(dòng)創(chuàng)建一個(gè)新線程,以避免鎖定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發(fā)送和檢索JSON。


查看完整回答
反對(duì) 回復(fù) 2019-06-19
?
Cats萌萌

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超9個(gè)贊

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) {
    }}


查看完整回答
反對(duì) 回復(fù) 2019-06-19
  • 3 回答
  • 0 關(guān)注
  • 858 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)