我重新发布我这里为了得到一些帮助。JSON解析器低性能
我做了一个JSON解析器至极返回JSONArray(为了得到我的WebService的信息)。
我最后的代码扔了NetworkException错误(在版本2.3.3它是长,但良好的工作),当我测试IceScreamSandwich ..
我改变了我的代码,以阻止它,并尝试变得更好perfs ..但它仍然没有工作对我的ICS电话:现在没有更多的错误,但一个ioexcepetion:“失败的JSON的网址改为” ..
我告诉你我的活动:
public class TabNewsJSONParsingActivity extends ListActivity
{
// url to make request
private static String url = "http://developer.prixo.fr/API/GetEvents?zone=8";
//JSON names
private static final String TAG_content = "content";
private static final String TAG_zone = "zone";
private static final String TAG_id = "id";
private static final String TAG_area = "area";
private static final String TAG_title = "title";
private static final String TAG_date = "date";
private static final String TAG_author = "author";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.onglet_news);
// Hashmap for ListView
ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONArray json;
try {
json = jParser.getJSONFromUrl1(url);
for(int i=0; i < json.length(); i++)
{
JSONObject child = json.getJSONObject(i);
String id = child.getString(TAG_id);
String title = child.getString(TAG_title);
String content = child.getString(TAG_content);
String date = child.getString(TAG_date);
String author = child.getString(TAG_author);
String zone = child.getString(TAG_zone);
String area = child.getString(TAG_area);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_content, content);
map.put(TAG_title, title);
map.put(TAG_author, author);
// adding HashList to ArrayList
newsList.add(map);
}
}
catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, newsList,R.layout.onglet_news_listitem,new String[] { TAG_content, TAG_title, TAG_author }, new int[] {R.id.name, R.id.email, R.id.mobile });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), TabNewsSingleMenuItemActivity.class);
in.putExtra(TAG_content, name);
in.putExtra(TAG_title, cost);
in.putExtra(TAG_author, description);
startActivity(in);
}
});
}
public boolean onOptionsItemSelected(MenuItem item)
{
//On regarde quel item a été cliqué grâce à son id et on déclenche une action
switch (item.getItemId())
{
case R.id.credits:
//pop up
Toast.makeText(TabNewsJSONParsingActivity.this, "Un delire", Toast.LENGTH_SHORT).show();
return true;
case R.id.quitter:
//Pour fermer l'application il suffit de faire finish()
finish();
return true;
}
return false;
}
}
我的解析器:
公共类JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String jsonstr = "";
public JSONParser() {}
// throws IOException just to tell the caller that something bad happened (and
// what) instead of simply returning 'null' without any more information.
public JSONArray getJSONFromUrl1(String url) throws IOException
{
try
{
// should be a member of the parser to allow multiple calls without recreating the client every time.
DefaultHttpClient httpClient = new DefaultHttpClient();
// Using POST means sending data (or it its not following HTTP RFCs)
//HttpPost httpPost = new HttpPost(url);
HttpGet httpGet = new HttpGet(url);
// Here the client may not be entirely initialized (no timeout, no agent-string).
HttpResponse httpResponse = httpClient.execute(httpGet);
//HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// The native utility function is also handling other charsets
String httpString = EntityUtils.toString(httpEntity);
return new JSONArray(httpString);
} catch (IOException ioe) {
throw ioe;
} catch (Exception ex) {
throw new IOException("Failed to read JSON from Url");
}
}
}
谁知道得到更好的perfs并使其朗姆酒4.0吗? 如何将它与异步任务一起使用? 感谢
谢谢,我尝试过,但我在ListAdapter适配器=此编译错误新SimpleAdapter(..):“SimpleAdapter构造是不确定的” – eento 2012-07-12 14:01:24
@eento请找到更新的代码。这是由'this'参数引起的。而是请使用'TabNewsJSONParsingActivity.this' – sunil 2012-07-13 05:21:47