我正在尝试学习如何解析Android中的JSON数据。我在这里提到了一个教程:http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
我得到的只是一个StackOverFlowError,其分辨率我无法弄清楚。任何帮助都将不胜感激。您可以在下面找到所有相关的Java代码。如果您还需要XML文件的代码,请随时告诉我。
ServiceHandler类的代码:
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;
}
}
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;
}
}
}
答案 0 :(得分:2)
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method);
}
此方法调用自身,因此导致堆栈溢出。您可能打算访问其他makeServiceCall
方法。因此,您必须添加List<NameValuePair>
参数
答案 1 :(得分:1)
/*STEP 1 - Parameter decloration*/
%let yyyymm0 = 201506;
/*STEP 1.1 - parm*/
data parm;
format yyyymm0 $6.;
yyyymm0 = &yyyymm.;
format yymm0 $4.;
yymm0 = substr(yyyymm0, 3, 4);
format yyyy0 $4.;
yyyy0 = substr(yyyymm0, 1, 4);
format mm0 $2.;
mm0 = substr(yyyymm0, 5, 2);
format yyyymm1-yyyymm11 $6.;
format yymm1-yymm11 $6.;
yyyymm1 = put(intnx('month', input(yyyymm, yymmn6.), -1, 'beginning'), yymmn6.);
yyyymm2 = put(intnx('month', input(yyyymm, yymmn6.), -2, 'beginning'), yymmn6.);
yyyymm3 = put(intnx('month', input(yyyymm, yymmn6.), -3, 'beginning'), yymmn6.);
yyyymm4 = put(intnx('month', input(yyyymm, yymmn6.), -4, 'beginning'), yymmn6.);
yyyymm5 = put(intnx('month', input(yyyymm, yymmn6.), -5, 'beginning'), yymmn6.);
yyyymm6 = put(intnx('month', input(yyyymm, yymmn6.), -6, 'beginning'), yymmn6.);
yyyymm7 = put(intnx('month', input(yyyymm, yymmn6.), -7, 'beginning'), yymmn6.);
yyyymm8 = put(intnx('month', input(yyyymm, yymmn6.), -8, 'beginning'), yymmn6.);
yyyymm9 = put(intnx('month', input(yyyymm, yymmn6.), -9, 'beginning'), yymmn6.);
yyyymm10 = put(intnx('month', input(yyyymm, yymmn6.), -10, 'beginning'), yymmn6.);
yyyymm11 = put(intnx('month', input(yyyymm, yymmn6.), -11, 'beginning'), yymmn6.);
yymm1 = substr(yyyymm1,3,4);
yymm2 = substr(yyyymm2,3,4);
yymm3 = substr(yyyymm3,3,4);
yymm4 = substr(yyyymm4,3,4);
yymm5 = substr(yyyymm5,3,4);
yymm6 = substr(yyyymm6,3,4);
yymm7 = substr(yyyymm7,3,4);
yymm8 = substr(yyyymm8,3,4);
yymm9 = substr(yyyymm9,3,4);
yymm10 = substr(yyyymm10,3,4);
yymm11 = substr(yyyymm11,3,4);
yyyy1 = substr(yyyymm1,1,4);
yyyy2 = substr(yyyymm2,1,4);
yyyy3 = substr(yyyymm3,1,4);
yyyy4 = substr(yyyymm4,1,4);
yyyy5 = substr(yyyymm5,1,4);
yyyy6 = substr(yyyymm6,1,4);
yyyy7 = substr(yyyymm7,1,4);
yyyy8 = substr(yyyymm8,1,4);
yyyy9 = substr(yyyymm9,1,4);
yyyy10 = substr(yyyymm10,1,4);
yyyy11 = substr(yyyymm11,1,4);
run;
这是一个无限循环。调用方法时必须添加第三个参数(public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method);
}
),例如:
List<NameValuePair>