Java 在尝试解析JSON数据时,通过递归调用在Android中获取StackOverflowerError
我试图学习如何在Android中解析JSON数据。我在这里提到了一个教程: 我得到的只是一个StackOverflower错误,我无法理解它的解决方案。在此方面的任何帮助都将不胜感激。您可以在下面找到所有相关的Java代码。如果您还需要XML文件的代码,请随时告诉我 ServiceHandler类的代码:Java 在尝试解析JSON数据时,通过递归调用在Android中获取StackOverflowerError,java,android,json,Java,Android,Json,我试图学习如何在Android中解析JSON数据。我在这里提到了一个教程: 我得到的只是一个StackOverflower错误,我无法理解它的解决方案。在此方面的任何帮助都将不胜感激。您可以在下面找到所有相关的Java代码。如果您还需要XML文件的代码,请随时告诉我 ServiceHandler类的代码: import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.a
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
/**
* Created by on 02-07-2015.
*/
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public ServiceHandler() {
}
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method);
}
public String makeServiceCall(String url, int method, List<NameValuePair> params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
if (params != null) {
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
} catch (UnsupportedEncodingException ue) {
ue.printStackTrace();
} catch (ClientProtocolException ce) {
ce.printStackTrace();
} catch (IOException io) {
io.printStackTrace();
}
return response;
}
}
import org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.client.utils.URLEncodedUtils;
导入org.apache.http.impl.client.DefaultHttpClient;
导入java.io.IOException;
导入java.io.UnsupportedEncodingException;
导入java.util.List;
/**
*创建人:2015年7月2日。
*/
公共类ServiceHandler{
静态字符串响应=null;
公共最终静态int GET=1;
公共最终静态int POST=2;
公共服务处理程序(){
}
公共字符串makeServiceCall(字符串url,int方法){
返回此.makeServiceCall(url,方法);
}
公共字符串makeServiceCall(字符串url、int方法、列表参数){
试一试{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpEntity HttpEntity=null;
HttpResponse HttpResponse=null;
if(方法==POST){
HttpPost HttpPost=新的HttpPost(url);
如果(参数!=null){
setEntity(新的UrlEncodedFormEntity(参数));
}
httpResponse=httpClient.execute(httpPost);
}else if(方法==GET){
如果(参数!=null){
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
}
HttpGet HttpGet=新的HttpGet(url);
httpResponse=httpClient.execute(httpGet);
}
}捕获(不支持的编码异常){
ue.printStackTrace();
}catch(客户端协议异常ce){
printStackTrace();
}捕获(io异常){
io.printStackTrace();
}
返回响应;
}
}
JSONActivity活动的代码:
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by on 03-07-2015.
*/
public class JSONActivity extends ListActivity {
private ProgressDialog pDialog;
private static String url = "http://api.androidhive.info/contacts/";
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
JSONArray contacts=null;
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webservicestrial);
contactList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
new GetContacts().execute();
}
private class GetContacts extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(JSONActivity.this);
pDialog.setMessage("Fetching...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
JSONActivity.this, contactList,
R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
TAG_PHONE_MOBILE }, new int[] { R.id.name,
R.id.email, R.id.mobile });
setListAdapter(adapter);
}
@Override
protected Void doInBackground(Void... params) {
ServiceHandler sh = new ServiceHandler();
String jsonStr = sh.makeServiceCall(url,ServiceHandler.GET);
Log.d("Response:", ">"+jsonStr);
if (jsonStr!=null) {
try {
JSONObject jsonObject = new JSONObject(jsonStr);
contacts = jsonObject.getJSONArray(TAG_CONTACTS);
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone node is JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_EMAIL, email);
contact.put(TAG_PHONE_MOBILE, mobile);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
}else {Log.e("ServiceHandler", "Couldn't get any data from the url");}
return null;
}
}
}
导入android.app.ListActivity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.ListAdapter;
导入android.widget.ListView;
导入android.widget.simpledapter;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
导入java.util.HashMap;
/**
*创建于2015年7月3日。
*/
公共类JSONActivity扩展了ListActivity{
私人对话;
专用静态字符串url=”http://api.androidhive.info/contacts/";
专用静态最终字符串标记_CONTACTS=“CONTACTS”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串标记\u EMAIL=“EMAIL”;
私有静态最终字符串标记_ADDRESS=“ADDRESS”;
私有静态最终字符串标记_GENDER=“GENDER”;
专用静态最终字符串标记_PHONE=“PHONE”;
专用静态最终字符串标记\u PHONE\u MOBILE=“MOBILE”;
专用静态最终字符串标记\u PHONE\u HOME=“HOME”;
专用静态最终字符串标记\u PHONE\u OFFICE=“OFFICE”;
JSONArray联系人=null;
ArrayList联系人列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.webservicestrial);
contactList=新的ArrayList();
ListView lv=getListView();
新建GetContacts().execute();
}
私有类GetContacts扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(JSONActivity.this);
setMessage(“获取…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
if(pDialog.isShowing())
pDialog.disclose();
ListAdapter=新的SimpleAdapter(
JSONActivity.this、contactList、,
R.layout.list_项,新字符串[]{TAG_NAME,TAG_EMAIL,
TAG_PHONE_MOBILE},新int[]{R.id.name,
R.id.email,R.id.mobile});
setListAdapter(适配器);
}
@凌驾
受保护的Void doInBackground(Void…参数){
ServiceHandler sh=新的ServiceHandler();
字符串jsonStr=sh.makeServiceCall(url,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject JSONObject=新的JSONObject(jsonStr);
contacts=jsonObject.getJSONArray(TAG_contacts);
对于(int i=0;ipublic String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method);
}
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, new ArrayList<NameValuePair>());
}
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method);
}