Android - Magento REST Api Cannot Respond Properly
Asked Answered
L

1

1

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

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. Is it possible to call a Magento REST Api url directly from client

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

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

Lheureux answered 1/8, 2014 at 11:53 Comment(6)
is ur service locally hosted? what is reponse type?json?Beacon
Response type XML, No its not local.Lheureux
Your code is fine , the problem is with your rest api..ensure the web.xml file for mapping.. Quick fix is enter the url in web browser it should display the result in page if it was json or xmlCrenel
Where I can find web.xml? Plz guide me.Lheureux
Which error code you were getting from Magento? I had also problem with getting magento rest api. I was getting error code 500. By setting request header parameter ("Accept", "application/json") solved my problem.Sepalous
@vsvankhede Actually I forgot what the error code is :P .Lheureux
L
1

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

    } 
Lheureux answered 2/8, 2014 at 10:46 Comment(1)
Hi, i am using magento 1.9.2.3 and i want to get products from android application. FYI -> i have consumer token, consumer secret key, oauth_token, oauth_token_secret. How can i use it in android application using rest api. @Remees M SydeFlapjack

© 2022 - 2024 — McMap. All rights reserved.