溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Android訪問(wèn)WEBAPI,傳遞json

發(fā)布時(shí)間:2020-05-30 21:15:05 來(lái)源:網(wǎng)絡(luò) 閱讀:6149 作者:武小豬 欄目:移動(dòng)開(kāi)發(fā)

public static String sendHttpRequestByPost(final String address,String encode,MatterReflectDTO dto){

        String result = "";

        DefaultHttpClient defaultHttpClient = new DefaultHttpClient();

        HttpPost httpPost = new HttpPost(address);


        String jsonString = dto.toString();


        try {

            StringEntity stringEntity = new StringEntity(new GsonBuilder().create().toJson(dto));

            System.out.println(stringEntity.toString());

            httpPost.setEntity(stringEntity);

            httpPost.setHeader("Accept", "application/json");

            httpPost.setHeader("Content-type", "application/json");

            httpPost.setHeader("Accept-Encoding", "gzip");


            HttpResponse httpResponse = defaultHttpClient.execute(httpPost);

            System.out.println("請(qǐng)求結(jié)果:"+httpResponse.getStatusLine().getStatusCode());

            int code = httpResponse.getStatusLine().getStatusCode();

            if(code ==200){

                InputStream is = httpResponse.getEntity().getContent();

                result = "true";


            }


        } catch (UnsupportedEncodingException e) {

            e.printStackTrace();  

        } catch (ClientProtocolException e) {

            e.printStackTrace(); 

        } catch (IOException e) {

            e.printStackTrace();  

        }

        return result;

    }

服務(wù)器端使用的是.net暫時(shí)就不貼出來(lái)了,需要注意的是并不是通過(guò)參數(shù)傳遞的。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI