【问题标题】:Android - Magento REST Api Cannot Respond ProperlyAndroid - Magento REST Api 无法正确响应
【发布时间】:2014-09-24 15:00:01
【问题描述】:

我想从 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 网址的结果是“服务暂时不可用”。我不知道这有什么问题。我有 2 个问题。

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

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

谁能知道 Magento-Android REST Api 连接请帮助我。

【问题讨论】:

  • 您的服务是本地托管的吗?什么是响应类型?json?
  • 响应类型 XML,不,它不是本地的。
  • 你的代码没问题,问题出在你的 api 上。确保 web.xml 文件进行映射。快速修复是在 Web 浏览器中输入 url,如果它应该在页面中显示结果是 json 还是 xml
  • 在哪里可以找到 web.xml?请指导我。
  • 您从 Magento 获得了哪个错误代码?我在获取 magento rest api 时也遇到了问题。我收到错误代码 500。通过设置请求标头参数(“Accept”、“application/json”)解决了我的问题。

标签: android api rest magento


【解决方案1】:

最后我通过以下代码做到了这一点,没有 Oauth。任何想要使用 REST Api to 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));
           }

    } 

【讨论】:

  • 嗨,我正在使用 magento 1.9.2.3,我想从 android 应用程序中获取产品。仅供参考 -> 我有消费者令牌、消费者密钥、oauth_token、oauth_token_secret。我如何使用rest api在android应用程序中使用它。 @Remees M Syde
猜你喜欢
  • 2018-02-23
  • 1970-01-01
  • 2016-09-12
  • 2015-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 2012-09-29
相关资源
最近更新 更多