开发者

calling rest web service?

i'm new to android development. i want to call a rest web service and show a simple txt-file or and xls-file. is that pos开发者_Go百科sible? and if it is, how? thank you in advance...


You can send a POST for the server and read the result.

This is my implementation with a JSON object, you should do something like it:

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


It is better to do this in an async call so that the main UI thread is no blocked. You could consider using this library.

It provides an async wrapper to Apache httpclient available in Android.

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


Yes, definitely possible. I do variations of this in my own app and what you want to do is use basic networking APIs from the Java platform such as URLConnection.


I have written an async http library. You can check it out https://github.com/darko1002001/android-rest-client.

I have also a simple example added for doing requests with parameters.


You just have to use following method if you are sending parameters in json format`and thats it then you just need to parse the json response .

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();
}`

`

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜