【发布时间】: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 个问题。
是否可以直接从客户端调用 Magento REST Api url
是否可以在没有 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”)解决了我的问题。