在我的代码中,我需要使用JSON在文本视图中显示普通文本。我正在使用排球库来解析JSON。而我的代码是Json对象无法转换为jsonarray
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“
我不知道我做了什么错误。我只是在烤面包片中发现这些错误。 请告诉我。
什么是Log.d的值(TAG,response.toString());? – kyogs 2014-10-09 12:08:28
JSONObject可以嵌套其他JSONObjects和JSONArrays ...查找导致异常并将其转换为调用attr.getJSONArray(“attr”)的句子; – 2014-10-09 12:08:44