我正在使用消费者从我的 android 应用程序访问 magento 后端。并且在手动完成身份验证后,我收到了来自 magento 的以下响应。
object(Zend_Http_Response)#16 (5) {
["version":protected] => string(3) "1.1"
["code":protected] => int(200)
["message":protected] => string(2) "OK"
["headers":protected] => array(6) {
["Date"] => string(29) "Mon, 10 Mar 2014 05:55:59 GMT"
["Server"] => string(45) "Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.6"
["X-powered-by"] => string(9) "PHP/5.5.6"
["Connection"] => string(5) "close"
["Transfer-encoding"] => string(7) "chunked"
["Content-type"] => string(31) "application/json; charset=utf-8"
}
["body":protected] => string(27184) "6a23
{"16": {"entity_id":"16","attribute_set_id":"38","type_id":"simple","sku":"n2610","model":"2610","dimension":"4.1 x 1.7 x 0.7 inches ","description":"something"}}0"}
如何使用signpost jar在android应用程序中解析上述响应。在android中,我正在使用SignPost for java OAuth库,并在那里手动传递消费者密钥、消费者密钥、访问令牌和访问令牌秘密。我的java文件在下面
package com.example.dukano;
import java.io.BufferedReader;
import java.io.Console;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
OAuthConsumer consumer;
public JSONParser(){
consumer = new CommonsHttpOAuthConsumer("4fd325e8273924df5f48f978a3f8e556","46be40812031f40645788ee90894efbe");
consumer.setTokenWithSecret("ca269640a3629a1d65af59d16058d91c", "f23ad8ba9646dbb28be5255c9faca0e1");
} //constructor
public JSONObject getJSONFromURL(String url)
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
request.setHeader("Accept", "text/xml");
consumer.sign(request);
org.apache.http.HttpResponse response = httpClient.execute(request); // error caught here
HttpEntity httpEntity = response.getEntity();
//String responsebody= EntityUtils.toString(httpEntity);
//System.out .println(responsebody+"hello world");
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Log.i("JSON", json);
}
catch(Exception e)
{}
try {
jObj = new JSONObject(json); //showing parse error here
} catch (JSONException e) {
Log.e("my JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}
我在调试模式下发现
org.apache.http.HttpResponse response = httpClient.execute(request);
执行时上面的语句然后它会尝试捕获以下内容:
try {
jObj = new JSONObject(json); //showing parse error here
} catch (JSONException e) {
Log.e("my JSON Parser", "Error parsing data " + e.toString());
}