我已经解析了JSON解析收到的数据。现在我想使用arrayadapter在listview中显示已解析的数据。但应用程序崩溃。当我删除JP类的功能,即片段活动中的parseJSON和getJSON,应用程序运行但列表视图中没有任何内容.pls帮助 代码: JP.class
package secondslash.secondslash;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Base64;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by alokit nigam on 12/5/2015.
*/
public class JP extends Activity {
String JSON_STRING,json_string;
public Bitmap bmp;
CustomListViewAdapter customListViewAdapter;
public void getJSON(){
new BackgroundTask1().execute();
}
public void parseJSON(){
try {
JSONObject jsonObject=new JSONObject(json_string);
JSONArray jsonArray=jsonObject.getJSONArray("server_response");
int count=0;
String articleId,topicId,headline,article,image;
while(count<jsonArray.length()){
JSONObject JO=jsonArray.getJSONObject(count);
articleId=JO.getString("article_id");
topicId=JO.getString("topic_id");
headline=JO.getString("headline");
article=JO.getString("article");
image=JO.getString("image");
byte[] qrimage = Base64.decode(image.getBytes(), count);
bmp = BitmapFactory.decodeByteArray(qrimage, 0, qrimage.length);
newsDetails nd=new newsDetails(articleId,topicId,headline,article,bmp);
customListViewAdapter.add(nd);
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
class BackgroundTask1 extends AsyncTask<Void,Void,String>
{
String json_url;
@Override
protected void onPreExecute() {
json_url="http://secondslash.site88.net/json_get_data.php";
}
@Override
protected String doInBackground(Void... params) {
try {
URL url=new URL(json_url);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder=new StringBuilder();
while ((JSON_STRING=bufferedReader.readLine())!=null){
stringBuilder.append(JSON_STRING+"\n");
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return stringBuilder.toString().trim();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
json_string=result;
}
}
}
CustomAdapter.java
package secondslash.secondslash;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
import secondslash.secondslash.mainActivity.topnewsFragment;
public class CustomListViewAdapter extends ArrayAdapter {
List list=new ArrayList();
public CustomListViewAdapter(Context context, int resource) {
super(context, resource);
}
public void add(newsDetails object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row=convertView;
ContactHolder contactHolder;
if(row==null){
LayoutInflater layoutInflater=(LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row=layoutInflater.inflate(R.layout.list_news,parent,false);
contactHolder=new ContactHolder();
contactHolder.title=(TextView)row.findViewById(R.id.title);
contactHolder.desc=(TextView)row.findViewById(R.id.desc);
contactHolder.icon=(ImageView)row.findViewById(R.id.icon);
row.setTag(contactHolder);
}
else {
contactHolder=(ContactHolder)row.getTag();
}
newsDetails contacts=(newsDetails)this.getItem(position);
contactHolder.title.setText(contacts.getheadline());
contactHolder.desc.setText(contacts.gettopicId());
contactHolder.icon.setImageBitmap(contacts.getImage());
return row;
}
static class ContactHolder{
TextView title,desc;
ImageView icon;
}
}
newsDetails.java
package secondslash.secondslash;
import android.graphics.Bitmap;
import android.media.Image;
import android.widget.ImageView;
/**
* Created by alokit nigam on 12/3/2015.
*/
public class newsDetails {
private String articleId,topic_id,headline,article;
Bitmap image;
public newsDetails(String articleId,String topic_id,String headline,String article,Bitmap image){
this.setarticleId(articleId);
this.settopicId(topic_id);
this.setheadline(headline);
this.setArticle(article);
this.setImage(image);
}
public String getarticleId() {
return articleId;
}
public void setarticleId(String articleId) {
this.articleId = articleId;
}
public String gettopicId() {
return topic_id;
}
public void settopicId(String topic_id) {
this.topic_id = topic_id;
}
public String getheadline() {
return headline;
}
public void setheadline(String headline) {
this.headline = headline;
}
public String getArticle() {
return article;
}
public void setArticle(String article) {
this.article = article;
}
public Bitmap getImage(){
return image;
}
public void setImage(Bitmap image){
this.image=image;
}
}
topnews.java
package secondslash.secondslash.mainActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import secondslash.secondslash.Adapter;
import secondslash.secondslash.CustomListViewAdapter;
import secondslash.secondslash.JP;
import secondslash.secondslash.R;
import secondslash.secondslash.RowItem;
import secondslash.secondslash.feedsActivity;
/**
* Created by alokit nigam on 7/29/2015.
*/
public class topnewsFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.topnews_layout, null);
ListView list = (ListView) view.findViewById(R.id.list);
CustomListViewAdapter cus = new CustomListViewAdapter(getContext(), R.layout.list_news);
list.setAdapter(cus);
JP jp = new JP();
jp.getJSON();
jp.parseJSON();
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "You clicked on position : " + pos + " and id : " + arg3, Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(getContext(), feedsActivity.class);
startActivity(myIntent);
}
});
return view;
}
}
答案 0 :(得分:0)
解析必须在执行后的AsyncTask中完成。 AsyncTask(获取JSON数据)在后台线程中完成。同时,你的主线程继续你的getJson方法,你希望son_string对象从你的JSON请求中收到所有信息,但这可能还没有完成