簡體   English   中英

調用其余的Web服務?

[英]calling rest web service?

我是android開發的新手。 我想打電話給其他Web服務,並顯示一個簡單的txt文件或xls文件。 那可能嗎? 如果是,怎么辦? 先感謝您...

您可以發送服務器的POST並讀取結果。

這是我用JSON對象實現的,您應該做類似的事情:

        JSONObject obj = new JSONObject();
        obj.put("jsonrpc", "2.0");
        obj.put("method", "getSomething");

        JSONObject auth = new JSONObject();
        auth.put("email", "user");
        auth.put("password", "pass");

        params.put("auth", auth);
        obj.put("params", params);

        int TIMEOUT_MILLISEC = 10000; // = 10 seconds
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient client = new DefaultHttpClient(httpParams);

        HttpPost request = new HttpPost("server address");
        try {
            StringEntity entity = new StringEntity(obj.toString());
            entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            request.setEntity(entity);

            ResponseHandler<String> handler = new BasicResponseHandler();
            String returnValue = client.execute(request, handler);
                    //returnValue has the return from the server.
        } catch (Throwable e) {
            e.printStackTrace();
        }

最好在異步調用中執行此操作,以便不阻塞主UI線程。 您可以考慮使用此庫。

它為Android中可用的Apache httpclient提供了異步包裝。

http://loopj.com/android-async-http/

我已經寫了一個異步http庫。 您可以查看https://github.com/darko1002001/android-rest-client

我還添加了一個簡單的示例,用於執行帶參數的請求。

如果您要以json格式發送參數,只需使用以下方法即可,那么您只需要解析json響應即可。

public String getApiResponse(String url,JSONObject param) {
    //Log.e("url",url);
    //Log.e("Params",param.toString());
    String responseString = null;
    HttpResponse httpResponse;
    HttpClient client=new DefaultHttpClient();
    HttpConnectionParams.setConnectionTimeout(client.getParams(),20000);
    HttpPost httpPost=new HttpPost(url);
    try {
        StringEntity entity=new StringEntity(param.toString());
        httpPost.setEntity(entity);
        httpResponse=client.execute(httpPost);
        responseString=convertStreamToString(httpResponse.getEntity().getContent());

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //Log.e("Response",responseString);
    return responseString;
}`


        String line;
    StringBuilder builder=new StringBuilder();
    InputStreamReader reader=new InputStreamReader(inputStream);
    BufferedReader bufferedReader=new BufferedReader(reader);
    try {
        while ((line=bufferedReader.readLine())!=null) {
            builder.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return builder.toString();
}`

`

是的,絕對有可能。 我在自己的應用程序中對此進行了各種更改,您要做的是使用Java平台中的基本網絡API,例如URLConnection

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM