且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的 - Magento的REST API无法正确响应

更新时间:2023-11-30 10:27:34

最后,我通过以下code,如果没有的Oauth这样做。任何人都希望使用REST API到Android可以使用this.To获得详细而不是产品,你必须使用OAuth来获得Magento的产品。

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

    }