我正在尝试从此链接解析json响应: https://www.move2mart.com/wp-json/wp/v2/pages?slug=watches 无论我做什么,这都是我得到的。 W / System.err:位置0处的意外字符()。
public class MainActivity extends AppCompatActivity {
TextView textView;
String url="https://www.move2mart.com/wp-json/wp/v2/pages?slug=mens-fashion";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.text);
final RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
textView.setText(response);
JSONParser jsonParser = new JSONParser();
Log.d("Inside","1");
try {
Log.d("Inside","2");
org.json.JSONArray jsonArray = (org.json.JSONArray) jsonParser.parse(response);
try {
//org.json.JSONArray jsonObject=jsonArray.getJSONArray(0);
JSONObject jsonObject2 = (JSONObject) jsonParser.parse(new InputStreamReader(new FileInputStream(jsonArray.getString(0))));
String t1=jsonObject2.getString("id");
Log.d("Inside3",t1);
} catch (JSONException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (ParseException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(stringRequest);
}
}
日志响应:08-15 08:40:46.266 18845-18845 / com.example.princ.learningjsonvolly D / Inside:1
08-15 08:40:46.266 18845-18845 / com.example.princ.learningjsonvolly D /内部:2
08-15 08:40:46.267 18845-18845 / com.example.princ.learningjsonvolly W / System.err:位置0处的意外字符()。
08-15 08:40:46.268 18845-18845 / com.example.princ.learningjsonvolly W / System.err:at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
08-15 08:40:46.268 18845-18845 / com.example.princ.learningjsonvolly W / System.err:at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
答案 0 :(得分:0)
看起来像你在gzip编码中的响应,所以让make GzipRequest:
public class GZipRequest extends StringRequest {
public GZipRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
public GZipRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(url, listener, errorListener);
}
// parse the gzip response using a GZIPInputStream
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String output = ""; // note: better to use StringBuilder
try {
final GZIPInputStream gStream = new GZIPInputStream(new ByteArrayInputStream(response.data));
final InputStreamReader reader = new InputStreamReader(gStream);
final BufferedReader in = new BufferedReader(reader);
String read;
while ((read = in.readLine()) != null) {
output += read;
}
reader.close();
in.close();
gStream.close();
} catch (IOException e) {
return Response.error(new ParseError());
}
return Response.success(output, HttpHeaderParser.parseCacheHeaders(response));
}
}
在代码中修改一下:
GZipRequest stringRequest = new GZipRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
textView.setText(response);
try {
org.json.JSONObject jsonObject = new org.json.JSONObject(response);
String t1 = jsonObject.getString("id");
} catch (ParseException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});