私はこのクラスと単純な Static {} メソッドに問題があります:
package com.example.tabletapp1.dummy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.StrictMode;
import android.util.Log;
public class DummyContent {
public static List<DummyItem> ITEMS = new ArrayList<DummyItem>();
public static Map<String, DummyItem> ITEM_MAP = new HashMap<String, DummyItem>();
public static ArrayList<HashMap<String, String>> stireList = new ArrayList<HashMap<String, String>>();
static { /////// THIS FUNCTION
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
String url = "http://kosalis.beclenar.ro/stiri.txt";
// JSON Node names
String TAG_STIRE = "stire";
String TAG_ID = "ID";
String TAG_NUME = "Nume";
String TAG_DESCRIERE = "Descriere";
String TAG_POZA = "Poza";
String TAG_CORP_STIRE_HTML = "Corp_stire_html";
// contacts JSONArray
JSONArray news = null;
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
news = json.getJSONArray(TAG_STIRE);
// looping through All Contacts
for (int i = 0; i < news.length(); i++) {
JSONObject c = news.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NUME);
String description = c.getString(TAG_DESCRIERE);
String poza = c.getString(TAG_POZA);
String body_html = c.getString(TAG_CORP_STIRE_HTML);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NUME, name);
map.put(TAG_DESCRIERE, description);
map.put(TAG_POZA, poza);
// adding HashList to ArrayList
stireList.add(map);
addItem(new DummyItem(id, name, description, poza, body_html));
}
} catch (JSONException e) {
Log.e("Error2", TAG_CORP_STIRE_HTML);
e.printStackTrace();
}
}
private static void addItem(DummyItem item) {
ITEMS.add(item);
ITEM_MAP.put(item.id, item);
}
public static class DummyItem {
public String id;
public String title;
public String description;
public String content;
public String photo;
public DummyItem(String id, String title, String description,
String photo, String content) {
this.id = id;
this.title = title;
this.description = description;
this.photo = photo;
this.content = content;
}
@Override
public String toString() {
return title;
}
}
}
しかし、MainActivity の static { } 関数にあるものをどうやって実行するかはわかりません。アプリが起動するたびに UI がフリーズするため、mainactivity から asynctask を呼び出したいと思います。JSONPArser クラスを作成する静的関数が原因であると確信しています。
package com.example.tabletapp1.dummy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "ISO-8859-2"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
もう 1 つ質問があります。AsyncTask があり、doInBackground() で JSONParser クラスを呼び出した場合、UI がフリーズするのでしょうか?