トーストで JSON が返す内容に問題があります。トーストは、クエリが成功したことを知らせるためにあります。問題は、トーストがトリガーされることです。これは、クエリが成功したことを意味しますが、Success [] 以外は何も含まれていません。成功したクエリは、Success [JSON RESULTS HERE] として返されるはずです。
--
更新: logCat を実行すると、org.json.JSONException: No value for League at org.json.JSONObject.get(JSONObject.java:354) が発生します。
何が起こっている可能性がありますか?誰にも解決策はありますか?
JSON
{"version":"1.0","leagues":{"league":[{"homeURL":"http://www.myfantasyleague.com/2013/home/18752","name":"Northern Wisconsin Premier FFL","id":"18752"},{"homeURL":"http://www.myfantasyleague.com/2013/home/36678","name":"Central Wisconsin Fantasy Football League","id":"36678"},{"homeURL":"http://www.myfantasyleague.com/2013/home/37766","name":"On Wisconsin","id":"37766"},{"homeURL":"http://www.myfantasyleague.com/2013/home/49677","name":"Wisconsin's Premier Dynasty Football League","id":"49677"}]},"encoding":"ISO-8859-1"}
主な活動
public final static String BaseUrl="DUMMYURL";
ArrayList<DEPT_HOLD> deptList=new ArrayList<DEPT_HOLD>();
private class GetDeptAyncTask extends AsyncTask<Hashtable<String,String>,Void,String>{
// Parse in background
@Override
protected String doInBackground(Hashtable<String,String>... params) {
@SuppressWarnings("rawtypes")
Hashtable ht=params[0];
@SuppressWarnings("unchecked")
String json=HelperHttp.getJSONResponseFromURL(BaseUrl+"ENDOFDUMMYURL", ht);
if(json!=null) parseJsonString(deptList,json);
else {
return "Invalid Company Id";
}
return "SUCCESS";
}
// Parse JSON
protected void parseJsonString(ArrayList<DEPT_HOLD> deptList,String json) {
try {
JSONObject top = new JSONObject(json);
JSONObject leagues = (JSONObject) top.get("leagues");
JSONArray array = (JSONArray) leagues.get("league");
for(int i = 0; i < array.length(); i++) {
JSONObject j = array.getJSONObject(i);
DEPT_HOLD d = new DEPT_HOLD();
d.two = j.optString("name","");
d.one = j.optString("id","");
deptList.add(d);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// Execute post
@Override
protected void onPostExecute(String result) {
if("SUCCESS".equals(result)) {
Toast.makeText(LeaguesTemporary.this, "Success "+deptList.size, Toast.LENGTH_LONG).show();
DeptArrayAdapter adapter=new DeptArrayAdapter(LeaguesTemporary.this,R.id.text1,deptList);
ListView listv=(ListView)findViewById(R.id.lv);
listv.setAdapter(adapter);
}
else{}
}
}
トースト
Toast.makeText(MainActivity.this, "Success "+deptList.size(), Toast.LENGTH_LONG).show();
DEPT_HOLD.java
public class DEPT_HOLD {
public String two;
public String one;
}
DeptArrayAdapter
public class DeptArrayAdapter extends ArrayAdapter<DEPT_HOLD>{
private Context context;
ArrayList<DEPT_HOLD> dataObject;
public DeptArrayAdapter(Context context, int textViewResourceId,
ArrayList<DEPT_HOLD> dataObject) {
super(context, textViewResourceId, dataObject);
this.context=context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView=convertView;
if(rowView==null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.row_layout, parent, false);
}
TextView textView = (TextView) rowView.findViewById(R.id.text1);
TextView textView1 = (TextView) rowView.findViewById(R.id.text2);
textView.setText(""+getItem(position).one);
textView1.setText(""+getItem(position).two);
return rowView;
}
}
HelperHttp
public class HelperHttp {
public static HttpClient httpclient;
private static List<NameValuePair> buildNameValuePair(Hashtable<String, String> httpPost){
if(httpPost==null) return null;
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
Enumeration<String> keys=httpPost.keys();
while(keys.hasMoreElements()){
String key = (String)keys.nextElement();
String value = (String)httpPost.get(key);
BasicNameValuePair nv=new BasicNameValuePair(key,value);
nvps.add(nv);
}
return nvps;
}
private static String buildGetUrl(List<NameValuePair> params, String url){
String paramString = URLEncodedUtils.format(params, "utf-8");
if(!url.endsWith("?"))
url += "?";
url+=paramString;
return url;
}
public static DefaultHttpClient getThreadSafeClient() {
if (httpclient != null)
return (DefaultHttpClient) httpclient;
HttpParams params = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(params, 100);
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
// Create and initialize scheme registry
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(
new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
httpclient = new DefaultHttpClient(cm, params);
return (DefaultHttpClient) httpclient;
}
public static String getJSONResponseFromURL(String url, Hashtable<String, String> httpGetParams){
String json_string="";
List<NameValuePair> nvps=buildNameValuePair(httpGetParams);
url=buildGetUrl(nvps,url);
System.out.println("URL==>"+url);
InputStream is = null;
try{
HttpGet httpget = new HttpGet(url);
HttpResponse response = getThreadSafeClient().execute(httpget);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(is),8192);
String line=null;
while((line=reader.readLine())!=null){
json_string=json_string+line;
}
response.getEntity().consumeContent();
System.out.println("Json Response==>"+json_string);
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
return null;
}
return json_string;
}
}