Android - Magento REST Api 无法正确响应 [英] Android - Magento REST Api Cannot Respond Properly

查看:25
本文介绍了Android - Magento REST Api 无法正确响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to call Magento REST API from Android, Used the code below.

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个问题.

The url is not empty, By using the same code I got response from other sample url's.But the Magento url is gave the result "Service temporary unavailable". I don't know what is wrong in this. I have 2 questions.

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

  1. Is it possible to call a Magento REST Api url directly from client

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

Is it possible to call Magento REST API without OAuth(I called without OAuth).

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

Can anyone know Magento-Android REST Api connection please help me.

推荐答案

最后我通过下面的代码做到了,没有 Oauth.任何希望使用 Android REST Api 获取 magento 产品的人都可以使用它.要获取详细信息而不是产品,您必须使用 Oauth.

Finally I did this by the following code,Without Oauth. Anyone looking to get magento products using REST Api to Android can use this.To get the detail rather than product you have to use 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));
           }

    } 

这篇关于Android - Magento REST Api 无法正确响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆