在我的应用程序中实现JSON解析器之后,应用程序表现得很奇怪。当我运行应用程序时它在进度对话框中冻结时没有给出任何错误,它无法获取数据,如果我删除progressdialog它说recyclerview没有附加跳帧
这是我的代码
MainActivity
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Config;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static com.android.volley.Response.ErrorListener;
public class MainActivity extends AppCompatActivity {
CollapsingToolbarLayout collapsingToolbarLayout;
ImageView image;
TextView personName;
TextView personProf;
private RecyclerView rv;
private List<Person> persons;
ImageView images;
RecyclerViewHeader header;
public ImageLoader imageLoader;
ImageView iv;
private RecyclerView.Adapter adapter;
public String strurl;
URL url;
URI uri;
private RecyclerView.LayoutManager llm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personName= (TextView)findViewById(R.id.textView);
personProf=(TextView)findViewById(R.id.textView2);
images= (ImageView)findViewById(R.id.imageView);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbarLayout.setTitle("Capsule");
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(android.R.color.transparent));
//setPalette();
strurl = "http://www.humanfox.com/capsule/assets/img/jawed_headshot.jpg";
Typeface face2 = Typeface.createFromAsset(getAssets(),"fonts/OpenSans-Regular.ttf");
Typeface face4= Typeface.createFromAsset(getAssets(),"fonts/OpenSans-Bold.ttf");
image = (ImageView) findViewById(R.id.image);
Picasso.with(this)
.load("http://d152j5tfobgaot.cloudfront.net/wp-content/uploads/2013/06/Javed-Habib_11.jpg")
.into(image);
rv =(RecyclerView)findViewById(R.id.rv);
rv.setHasFixedSize(true);
llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
header = (RecyclerViewHeader) findViewById(R.id.header);
header.attachTo(rv,true);
//initializeData();
persons = new ArrayList<>();
getData();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
private void getData(){
//Showing a progress dialog
final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false);
//Creating a json array request
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(COnfig.DATA_URL,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
//Dismissing progress dialog
loading.dismiss();
//calling method to parse json array
parseData(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(jsonArrayRequest);
}
private void parseData(JSONArray array){
for(int i = 0; i<array.length(); i++) {
Person person = new Person();
JSONObject json = null;
try {
json = array.getJSONObject(i);
person.setImageUrl(json.getString(COnfig.TAG_IMAGE_URL));
person.setName(json.getString(COnfig.TAG_NAME));
person.setPassion(json.getString(COnfig.TAG_PROFESSION));
} catch (JSONException e) {
e.printStackTrace();
}
persons.add(person);
}
//Finally initializing our adapter
adapter = new RVAdapter(persons, this);
//Adding adapter to recyclerview
rv.setAdapter(adapter);
}
}
适配器
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.media.Image;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.squareup.picasso.Picasso;
import org.w3c. dom.Text;
import java.util.ArrayList;
import java.util.List;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder> {
private ImageLoader imageloader;
private Context context;
List<Person> persons;
public RVAdapter(List<Person> persons,Context context){
super();
this.persons = persons;
this.context = context;
}
@Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item,null);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
Person person = persons.get(i);
imageloader=CustomVolleyRequest.getInstance(context).getImageLoader();
imageloader.get(person.getImageUrl(),ImageLoader.getImageListener(personViewHolder.personPhoto,R.drawable.ic_launcher,android.R.drawable.ic_dialog_alert));
personViewHolder.personPhoto.setImageUrl(person.getImageUrl(),imageloader);
personViewHolder.personName.setText(person.getName());
personViewHolder.personProf.setText(person.getPassion());
}
@Override
public int getItemCount() {
return persons.size();
}
class PersonViewHolder extends RecyclerView.ViewHolder {
public CardView cv;
public TextView personName;
public TextView personProf;
public NetworkImageView personPhoto;
//public Context ctx;
public PersonViewHolder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cv);
personName = (TextView) itemView.findViewById(R.id.textView);
personProf = (TextView) itemView.findViewById(R.id.textView2);
personPhoto=(NetworkImageView) itemView.findViewById(R.id.imageView);
}
}
}