Json 解析错误:Java.lang.String 类型的值 <?xml 无法转换为 JSONArray [重复]
Posted
技术标签:
【中文标题】Json 解析错误:Java.lang.String 类型的值 <?xml 无法转换为 JSONArray [重复]【英文标题】:Json parsing error: Value <?xml of type java.lang.String cannot be converted to JSONArray [duplicate] 【发布时间】:2018-01-08 15:58:35 【问题描述】:我是编程新手,我有一个小问题,我正在尝试制作一个使用 json 来读取 flickr api 的应用程序,但我在环顾四周的描述中收到错误提及,但没有一个解决方案有效,所以我过来问
public class JSONPareser
final String TAG = "JSONParser";
public String makeServiceCall(String reqUrl)
String response = null;
try
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
catch (MalformedURLException e)
Log.e(TAG, "MalformedURLException: " + e.getMessage());
catch (ProtocolException e)
Log.e(TAG, "ProtocolException: " + e.getMessage());
catch (IOException e)
Log.e(TAG, "IOException: " + e.getMessage());
catch (Exception e)
Log.e(TAG, "Exception: " + e.getMessage());
return response;
private String convertStreamToString(InputStream is) throws UnsupportedEncodingException
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
StringBuilder sb = new StringBuilder();
String line;
try
while ((line = reader.readLine()) != null)
sb.append(line).append('\n');
catch (IOException e)
e.printStackTrace();
finally
try
is.close();
catch (IOException e)
e.printStackTrace();
return sb.toString();
这是我的解析器类,这是我调用 json 对象的时候
public class getData extends AsyncTask<Void, Void, Void>
@Override
protected void onPreExecute()
super.onPreExecute();
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Please wait");
dialog.setCancelable(false);
dialog.show();
@Override
protected Void doInBackground(Void... params)
String url = "https://api.flickr.com/services/feeds/photos_public.gne";
JSONPareser pareser = new JSONPareser();
String jsonStr = pareser.makeServiceCall(url);
if (jsonStr != null)
try
JSONArray jsonArray = new JSONArray(jsonStr);
for (int i = 0; i < jsonArray.length(); i++)
JSONObject obj = jsonArray.getJSONObject(i);
FlickrModel model = new FlickrModel();
model.setLink(obj.getString("link"));
model.setDescription(obj.getString("description"));
model.setTitle(obj.getString("title"));
model.setAuthor(obj.getString("author"));
model.setTags(obj.getString("tags"));
flickrList.add(model);
catch (final JSONException e)
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable()
@Override
public void run()
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
);
else
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable()
@Override
public void run()
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
);
return null;
@Override
protected void onPostExecute(Void result)
super.onPostExecute(result);
if (dialog.isShowing())
dialog.dismiss();
非常感谢你
【问题讨论】:
给你一个提示,你不需要来自 onPostExecute 方法的 runOnUiThread; 您能否粘贴响应,因为它显示 xml 响应即将到来。我认为 url 架构不正确或服务器未启动,因此发送 xml 而不是 json 作为响应... JsonParser 的最低要求要解析的是响应字符串必须是 json 格式。 【参考方案1】:您的服务似乎返回的是 XML 而不是 JSON,请参阅标题
【讨论】:
以上是关于Json 解析错误:Java.lang.String 类型的值 <?xml 无法转换为 JSONArray [重复]的主要内容,如果未能解决你的问题,请参考以下文章