Android Json对象无法转换为jsonarray
在我的代码中,我需要使用JSON在文本视图中显示普通文本。我正在使用volley库解析JSON。我的代码是Android Json对象无法转换为jsonarray,android,json,android-volley,Android,Json,Android Volley,在我的代码中,我需要使用JSON在文本视图中显示普通文本。我正在使用volley库解析JSON。我的代码是 public class PrincipalSpeechFragment extends Fragment implements OnClickListener { public PrincipalSpeechFragment(){} private String urlJsonArry = "http://imaginetventures.net/sample/everwin_vid
public class PrincipalSpeechFragment extends Fragment implements OnClickListener {
public PrincipalSpeechFragment(){}
private String urlJsonArry = "http://imaginetventures.net/sample/everwin_vidhyashram/webservice/rest/?module=speech&from=1-9-2014&to=30-9-2014";
private static String TAG = PrincipalSpeechFragment.class.getSimpleName();
// JSON Node names
private static final String TAG_PRINCIPAL_SPEECH ="Principal Speech";
private static final String TAG_SPEECH= "speech";
private static final String TAG_DESC = "desc";
String tag_json_obj = "json_obj_req";
private Button getPrincipalSpeech;
// Progress dialog
//private ProgressDialog pDialog;
private TextView txtResponse;
// temporary string to show the parsed response
private String jsonResponse;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_principal_speech, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
getPrincipalSpeech = (Button) getActivity().findViewById(R.id.idForPrinciSpeech);
txtResponse = (TextView) getActivity().findViewById(R.id.txtResponse);
getPrincipalSpeech.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
makeJsonArrayRequest();
}
private void makeJsonArrayRequest() {
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
// Parsing json array response
// loop through each json object
jsonResponse = "";
for (int i = 0; i < response.length(); i++) {
JSONObject speechobj = (JSONObject) response
.get(i);
/*String speech = speechobj.getString("speech");
String email = speech.getString("email");
JSONObject phone = speech
.getJSONObject("speech");
String home = phone.getString("home");
String mobile = phone.getString("mobile");*/
/*jsonResponse += "Speech: " + speech + "\n\n";
/*jsonResponse += "Email: " + email + "\n\n";*/
/*jsonResponse += "Home: " + home + "\n\n";
jsonResponse += "Mobile: " + mobile + "\n\n\n";*/
}
txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
//hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getActivity().getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
//hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
}
当我点击按钮获得结果时,它只是显示org.json.json.EXCEPTION。和值:--Json对象无法转换为jsonarray
我不知道我犯了什么错误。我只是在吐司中发现了这些错误。
请告诉我
private String urlJsonArry = "http://imaginetventures.net/sample/everwin_vidhyashram/webservice/rest/?module=speech&from=1-9-2014&to=30-9-2014";
您正在请求代码中指定的url,如果我将其粘贴到浏览器中:
答复:
{
"Principal Speech": [
{
"speech": "Lorem (... et cetera)."
}
]
}
这是一个JSONObject,不是JSONArray。因此,您可以将代码中的JSONArray设置为JSONObject,然后提取您可能需要的JSONArray:JSONObject.getJSONArrayPrincipal Speech
编辑:添加代码
private void makeJSONObjectRequest() {
JsonObjectRequest req = new JsonObjectRequest(urlJsonArry, //Dont know where that came from, but JsonArrayRequest as classname will probably still work. Custom/your own class, I guess. As long as the type JSONArray is changed to JSONObject
new Response.Listener<JSONObject>() { //JSONObject instead of JSONArray
@Override
public void onResponse(JSONObject response) {//JSONObject instead of JSONArray
JSONArray speechArray = response.getJSONArray("Principal Speech"); //get the array from the JSONObject
Log.d(TAG, response.toString());
// The rest is the same
try {
// Parsing json array response
// loop through each json object
jsonResponse = "";
for (int i = 0; i < response.length(); i++) {
JSONObject speechobj = (JSONObject) response
.get(i);
/*String speech = speechobj.getString("speech");
String email = speech.getString("email");
JSONObject phone = speech
.getJSONObject("speech");
String home = phone.getString("home");
String mobile = phone.getString("mobile");*/
/*jsonResponse += "Speech: " + speech + "\n\n";
/*jsonResponse += "Email: " + email + "\n\n";*/
/*jsonResponse += "Home: " + home + "\n\n";
jsonResponse += "Mobile: " + mobile + "\n\n\n";*/
}
txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
//hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getActivity().getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
//hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
在您的回复中,您得到了JSONObject,所以请使用下面的代码
JSONObject jsnobject = new JSONObject(response);
之后
JSONArray jsonArray = jsnobject.getJSONArray("Principal Speech");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
}
编辑2:
我检查了源代码,如下所示
public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener) {
super(Method.GET, url, null, listener, errorListener);
}
Log.dTAG、response.toString;的值是多少;?JSONObject可以嵌套其他JSONObject和JSONArray。。。查找导致异常的句子,并调用attr.getjsonarrayatr对其进行转换;我需要打印演讲内容,怎么做。你能编辑我的代码吗?@sanjayAD那么我需要jsonarrayRequestSource的源代码;非常感谢兄弟。你的回答对我很有帮助。但是在JSONObject speechobj=JSONObject response.geti中;get显示错误。不知道为什么。我还想完全学习截击。“你能给我推荐一些图坦卡蒙吗?”桑贾亚德,对不起,我的错。该行应该是JSONObject speechobj=JSONObjectspeechArray.geti;或者speechArray.getJSONObjecti;。使用edit2中的代码
JSONArray jsonArray = jsnobject.getJSONArray("Principal Speech");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
}
Response.Listener<JSONArray> listener = new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response) {
// TODO Auto-generated method stub
}
};
Response.ErrorListener errorListener = new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
};
JsonArrayRequest jr =new JsonArrayRequest(Request.Method.GET,url, listener, errorListener) ;
public JsonArrayRequest(String url, Listener<JSONArray> listener, ErrorListener errorListener) {
super(Method.GET, url, null, listener, errorListener);
}