首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在android程序中调用Assembla REST apis

如何在android程序中调用Assembla REST apis
EN

Stack Overflow用户
提问于 2011-08-19 00:02:34
回答 1查看 303关注 0票数 0

我想从我的android代码中调用assembla apis。我能够使用下面给出的代码连接到汇编,我得到了HTMl格式的输出。但是不能理解如何使用这些数据。

代码片段:

HttpURLConnection连接=空;尝试{

代码语言:javascript
复制
        String authentication = "username:password";
        String encoding = Base64.encodeToString(authentication.getBytes(), Base64.NO_WRAP);
        URL url = new URL("https://www.assembla.com/spaces/my_spaces");
        //URL url = new URL("https://www.assembla.com/");
         conn = (HttpURLConnection) url.openConnection();
         conn.setRequestMethod("GET");
         conn.setRequestProperty("Authorization", "Basic " + encoding);
         conn.setDoOutput(true);
         conn.connect();

        System.out.println(conn.getResponseCode()); 
        System.out.println(conn.getResponseMessage());

        InputStreamReader isr =
            new InputStreamReader(conn.getInputStream());
        BufferedReader br = new BufferedReader(isr);

        String inputLine;

        while ((inputLine = br.readLine()) != null)
            System.out.println(inputLine);

        br.close();

我刚接触android,所以不知道我用来调用rest api的方法是否正确。敬请指教。

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-19 00:14:33

这就是我发出api请求的主要方式。

代码语言:javascript
复制
        HttpClient c = new DefaultHttpClient();
        HttpPost post = new HttpPost("URL");

        post.addHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");
        List<NameValuePair> httpparams = new ArrayList<NameValuePair>();

        httpparams.add(new BasicNameValuePair("password", password));

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(httpparams);
        post.setEntity(entity);

        HttpResponse resp = c.execute(post);


        InputStream i = resp.getEntity().getContent();
        InputStreamReader ir = new InputStreamReader(i);
        BufferedReader br = new BufferedReader(ir);

        StringBuilder sb = new StringBuilder();
        String line = null;

        while ((line = br.readLine()) != null) {
            sb.append(line);
        }

        ir.close();
        br.close();

        JSONObject user = new JSONObject(sb.toString());

如果需要,您可以添加或删除参数。我假设你收到了一个JSON对象(sb.toString())

编辑:在这种情况下,它是一个帖子,你可以很容易地将它更改为GET (HttpGet)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7110501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档