Android 如何在发布listview适配器的数据之前解析json
我正在尝试为我的android应用程序制作json解析器 我需要在为我的listview适配器获取json对象后发布一些数据。我正在使用这段代码,但它总是出错。我真的很努力,因为我改变了很多次,但我做不到。(我可以很容易地获得json数据,但在这种情况下,我想在json解析之前发表文章。)我应该如何编辑我的代码?谢谢你的帮助,文件 我得到了这个错误:Android 如何在发布listview适配器的数据之前解析json,android,json,http-post,Android,Json,Http Post,我正在尝试为我的android应用程序制作json解析器 我需要在为我的listview适配器获取json对象后发布一些数据。我正在使用这段代码,但它总是出错。我真的很努力,因为我改变了很多次,但我做不到。(我可以很容易地获得json数据,但在这种情况下,我想在json解析之前发表文章。)我应该如何编辑我的代码?谢谢你的帮助,文件 我得到了这个错误: java.lang.RuntimeException: An error occured while executing doInBackgro
java.lang.RuntimeException: An error occured while executing doInBackground()
这是我的代码:
public class veritabaniKayit extends AsyncTask<String, Integer, JSONObject> {
private ProgressDialog pDialog;
JSONObject json2;
@Override
protected void onPreExecute() {
super.onPreExecute();
yayDescription = (TextView)findViewById(R.id.yayDescription);
yayDistance = (TextView)findViewById(R.id.yayDistance);
yayCreatedTime = (TextView)findViewById(R.id.yayCreatedTime);
pDialog = new ProgressDialog(HomeListview.this);
pDialog.setMessage("Wait a min ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... params) {
// TODO Auto-generated method stub
postData(params[0],params[1],params[2]);
JSONParser jParser = new JSONParser();
// Getting JSON from URL
url = "11.22.11.22/messages/list";
System.out.println("THISISMYURL"+url);
json2 = jParser.getJSONFromUrl(url);
return json2;
}
protected void onPostExecute(JSONObject result){
// pb.setVisibility(View.GONE);
}
protected void onProgressUpdate(Integer... progress){
// pb.setProgress(progress[0]);
}
public void postData(String personName, String username, String email) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://11.22.11.22/messages/list");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("token", "zyob3xkdnv"));
nameValuePairs.add(new BasicNameValuePair("lat", "37"));
nameValuePairs.add(new BasicNameValuePair("long", "-122"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// String json2 = EntityUtils.toString(response.getEntity());
/* JSONObject jObject = null;
try {
jObject = new JSONObject(json);
System.out.println("jObject"+jObject);
String tokenValue=jObject.getString("token");
System.out.println("tokenValue"+tokenValue);
edit = pref.edit();
edit.putString("sharedUsername", personName.toString());
edit.putString("sharedEmail", email.toString());
edit.putString("sharedToken", tokenValue);
edit.commit();
}
catch (JSONException e) {
e.printStackTrace();
} */
try {
JSONObject json2;
JSONParser jParser = new JSONParser();
json2 = jParser.getJSONFromUrl(url);
// Getting JSON Array from URL
android = json2.getJSONArray(TAG_OS);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
// String JSON item in a Variable
String yayDescription = c.getString(text);
String yayDistance = c.getString(distance);
String yayCreatedTime = c.getString(created_at);
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put(text, yayDescription);
map.put(distance, yayDistance);
map.put(created_at, yayCreatedTime);
oslist.add(map);
myOwnPost =(ListView)findViewById(R.id.listView);
myOwnPost.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
// myOwnPost.setRotation(-90);
ListAdapter adapter = new SimpleAdapter(HomeListview.this, oslist,
R.layout.homelistviewitem,
new String[] { text, distance, created_at }, new int[] { R.id.yayDescription,R.id.yayDistance, R.id.yayCreatedTime });
myOwnPost.setAdapter(adapter);
myOwnPost.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String whatIsSoundName = oslist.get(+position).get("yayDescription");
Toast.makeText(HomeListview.this, "You Clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
pDialog.dismiss();
int responseCode = response.getStatusLine().getStatusCode();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
}
公共类veritabaniKayit扩展异步任务{
私人对话;
JSONObject json2;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
yayDescription=(TextView)findViewById(R.id.yayDescription);
yayDistance=(TextView)findViewById(R.id.yayDistance);
yayCreatedTime=(TextView)findViewById(R.id.yayCreatedTime);
pDialog=newprogressdialog(HomeListview.this);
setMessage(“等待一分钟…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(字符串…参数){
//TODO自动生成的方法存根
postData(参数[0],参数[1],参数[2]);
JSONParser jParser=新的JSONParser();
//从URL获取JSON
url=“11.22.11.22/消息/列表”;
System.out.println(“thismyurl”+url);
json2=jParser.getJSONFromUrl(url);
返回json2;
}
受保护的void onPostExecute(JSONObject结果){
//pb.setVisibility(View.GONE);
}
受保护的void onProgressUpdate(整数…进度){
//pb.setProgress(progress[0]);
}
public void postData(字符串personName、字符串用户名、字符串电子邮件){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://11.22.11.22/messages/list");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“令牌”,“zyob3xkdnv”);
添加(新的BasicNameValuePair(“lat”、“37”);
添加(新的BasicNameValuePair(“long”和“-122”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
//字符串json2=EntityUtils.toString(response.getEntity());
/*JSONObject jObject=null;
试一试{
jObject=新的JSONObject(json);
System.out.println(“jObject”+jObject);
String tokenValue=jObject.getString(“令牌”);
System.out.println(“tokenValue”+tokenValue);
编辑=首选编辑();
edit.putString(“sharedUsername”,personName.toString());
edit.putString(“sharedEmail”,email.toString());
edit.putString(“sharedToken”,tokenValue);
edit.commit();
}
捕获(JSONException e){
e、 printStackTrace();
} */
试一试{
JSONObject json2;
JSONParser jParser=新的JSONParser();
json2=jParser.getJSONFromUrl(url);
//从URL获取JSON数组
android=json2.getJSONArray(TAG_OS);
for(int i=0;ivalue
HashMap=newHashMap();
map.put(文本,yayDescription);
地图放置(距离、距离);
map.put(创建时间,yayCreatedTime);
添加(地图);
myOwnPost=(ListView)findViewById(R.id.ListView);
myOwnPost.setChoiceMode(ListView.CHOICE\u MODE\u SINGLE);
//设置旋转(-90);
ListAdapter=new SimpleAdapter(HomeListview.this,oslist,
R.layout.homelistviewitem,
新字符串[]{text,distance,created_at},new int[]{R.id.yayDescription,R.id.yayDistance,R.id.yayCreatedTime});
myOwnPost.setAdapter(适配器);
myOwnPost.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
字符串whatIsSoundName=oslist.get(+position.get(“yayDescription”);
Toast.makeText(HomeListview.this,“您在“+位置单击,Toast.LENGTH_SHORT.show()”;
}
});
}
}捕获(JSONException e){
e、 printStackTrace();
}
pDialog.disclose();
int responseCode=response.getStatusLine().getStatusCode();
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
}
尝试将此代码放入
@Override
protected void onPostExecute(String result) {
myOwnPost =(ListView)findViewById(R.id.listView);
myOwnPost.setChoiceMode(ListView.CHOICE_MODE_SINGLE);ListAdapter adapter = new SimpleAdapter(HomeListview.this, oslist,
R.layout.homelistviewitem,
new String[] { text, distance, created_at }, new int[] { R.id.yayDescription,R.id.yayDistance, R.id.yayCreatedTime });
myOwnPost.setAdapter(adapter);
myOwnPost.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String whatIsSoundName = oslist.get(+position).get("yayDescription");
Toast.makeText(HomeListview.this, "You Clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
}
@覆盖
受保护的void onPostExecute(字符串结果){
myOwnPost=(ListView)findViewById(R.id.ListView);
myOwnPost.setChoiceMode(ListView.CHOICE_MODE_SINGLE);ListAdapter=new SimpleAdapter(HomeListview.this,oslist,
荷马