Android - Magento REST Api无法正常回应

时间:2014-08-01 11:53:21

标签: android api rest magento

我想从Android调用Magento REST API,使用下面的代码。

new HttpAsyncTask().execute("http://myweb.com/api/rest/products");
public static String GET(String url){
        InputStream inputStream = null;
        String result = "";
        try {

            // create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // make GET request to the given URL
            HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

            // receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();

            // convert inputstream to string
            if(inputStream != null)
                result = convertInputStreamToString(inputStream);
            else
                result = "Did not work!";

        } catch (Exception e) {
            Log.d("InputStream", e.getLocalizedMessage());
        }

        return result;
    }

    private static String convertInputStreamToString(InputStream inputStream) throws IOException{
        BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
        String line = "";
        String result = "";
        while((line = bufferedReader.readLine()) != null)
            result += line;

        inputStream.close();
        return result;

    }

    private class HttpAsyncTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {

            return GET(urls[0]);
        }
        // onPostExecute displays the results of the AsyncTask.
        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
            etResponse.setText(result);
       }  

网址不是空的,通过使用相同的代码,我得到了来自其他示例网址的响应。但是Magento网址的结果是“服务暂时不可用”。我不知道这有什么问题。我有两个问题。

  1. 是否可以直接从客户端调用Magento REST Api网址

  2. 是否可以在没有OAuth的情况下调用Magento REST API(我在没有OAuth的情况下调用)。

  3. 有谁知道Magento-Android REST Api连接请帮助我。

1 个答案:

答案 0 :(得分:1)

最后我通过以下代码完成了这个,没有Oauth。任何想要使用REST Api到Android获取magento产品的人都可以使用它。要获得细节而不是产品,你必须使用Oauth。

 new HttpAsyncTask().execute("http://myweb.com/api/rest/products");
     private class HttpAsyncTask extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... urls) {

                 String host = "myweb.com";

                HttpClient client = new DefaultHttpClient();

                BasicHttpContext localContext = new BasicHttpContext();
                HttpHost targetHost = new HttpHost(host, 443, "https");
                Log.d("url ",  urls[0]);
                HttpGet httpget = new HttpGet(urls[0]);
                httpget.setHeader("Content-Type", "application/json");
                httpget.setHeader("Accept", "application/json");
                HttpResponse response;
                Object content = null;
                JSONObject json = null;
                try {
                    response = client.execute(targetHost, httpget,
                            localContext);
                        HttpEntity entity = response.getEntity();

                        content = EntityUtils.toString(entity);

                        json = new JSONObject(content.toString());

                        Log.d("result", "OK: " + json.toString(1));
           }

    }