S
S
SanyD2018-04-14 18:50:15
Android
SanyD, 2018-04-14 18:50:15

How to load local files into parseJSON?

I am developing a directory application in which pages and sections are loaded from the Internet.
Please tell me how to make it so that the download is from the internal files of the application?
(I'm new to android development, so don't judge too harshly if I don't express myself well)
Code


public class MainActivityBi1 extends AppCompatActivity implements ExampleAdapter.OnItemClickListener {
public static final String EXTRA_URL = "imageUrl";
public static final String EXTRA_CREATOR = "creatorName";
public static final String EXTRA_LIKES = "likeCount";
private RecyclerView mRecyclerView;
private ExampleAdapter mExampleAdapter;
private ArrayList mExampleList;
private RequestQueue mRequestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_bi1);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mExampleList = new ArrayList<>();
mRequestQueue = Volley.newRequestQueue(this);
parseJSON();
ActionBar actionBar =getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
private void parseJSON() {
String url = " treas.ru/Business/Bi/1/Bi1.txt ";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("hits");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String creatorName = hit.getString("user");
String imageUrl = hit.getString("webformatURL");
String likeCount = hit.getString("likes");
mExampleList.add(new ExampleItem(imageUrl, creatorName, likeCount));
}
mExampleAdapter = new ExampleAdapter(MainActivityBi1.this, mExampleList);
mRecyclerView.setAdapter(mExampleAdapter);
mExampleAdapter.setOnItemClickListener(MainActivityBi1.this);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mRequestQueue.add(request);
}
@Override
public void onItemClick(int position) {
Intent detailIntent = new Intent(this, DetailActivity.class);
ExampleItem clickedItem = mExampleList.get(position);
detailIntent.putExtra(EXTRA_URL, clickedItem.getImageUrl());
detailIntent.putExtra(EXTRA_CREATOR, clickedItem.getCreator());
detailIntent. putExtra(EXTRA_LIKES, clickedItem.getLikeCount());
startActivity(detailIntent);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

ExampleAdapter

public class ExampleAdapter extends RecyclerView.Adapter {
private Context mContext;
private ArrayList mExampleList;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void onItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
public ExampleAdapter(Context context, ArrayList exampleList) {
mContext = context;
mExampleList = exampleList;
}
@Override
public ExampleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.example_item, parent, false);
return new ExampleViewHolder(v);
}
@Override
public void onBindViewHolder(ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
String imageUrl = currentItem.getImageUrl();
String creatorName = currentItem.getCreator();
holder.mTextViewCreator.setText(creatorName);
Picasso.with(mContext).load(imageUrl).fit().centerInside().into(holder.mImageView);
}
@Override
public int getItemCount() {
return mExampleList.size();
}
public class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextViewCreator;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.image_view);
mTextViewCreator = itemView.findViewById(R.id.text_view_creator);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mListener != null) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
mListener.onItemClick(position);
}
}
}
});
}
}
}

ExampleItem
public class ExampleItem {
private String mImageUrl;
private String mCreator;
private String mLikes;
public ExampleItem(String imageUrl, String creator, String likes) {
mImageUrl = imageUrl;
mCreator = creator;
mLikes = likes;
}
public String getImageUrl() {
return mImageUrl;
}
public String getCreator() {
return mCreator;
}
public String getLikeCount() {
return mLikes;
}
}

List code (its files also need to be transferred to the application itself)
{
"totalHits":500,
"hits":[
{
"likes":" treas.ru/Business/Bi/1/Bi1_1.html ",
"webformatURL":" treas.ru/Business/Bi/1/Bi1_1. jpg ",
"user":"Photographer"
},
{
"likes":" treas.ru/Business/Bi/1/Bi1_2.html ",
"webformatURL":" treas.ru/Business/Bi/1/Bi1_2. jpg ",
"user":"Apartment renovation"
},
{
"likes":" treas.ru/Business/Bi/1/Bi1_3.html ",
"webformatURL": " treas.ru/Business/Bi/1/Bi1_3.jpg ",
"user":"Baking cakes"
},
{
"likes":" treas.ru/Business/Bi/1/Bi1_4.html ",
"webformatURL":" treas.ru/Business/Bi/1/Bi1_4.jpg " ,
"user":"Vending business"
},
{
"likes":" treas.ru/Business/Bi/1/Bi1_5.html ",
"webformatURL":" treas.ru/Business/Bi/1/Bi1_5.jpg ",
"user":"Corporate Management"
},
{
"likes":" treas.ru/Business/Bi/1/Bi1_6.html ",
"webformatURL":" treas.ru/Business/Bi/1/Bi1_6.jpg",
"user":"Fast food outlet"
},
{
"likes":" treas.ru/Business/Bi/1/Bi1_7.html ",
"webformatURL":" treas.ru/Business/Bi/1/Bi1_7 .jpg ",
"user":"Real estate services"
},
{
"likes":" treas.ru/Business/Bi/1/Bi1_8.html ",
"webformatURL":" treas.ru/Business/Bi/1/ Bi1_8.jpg ",
"user":"Organization of dates"
}
],
"total":3106
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-04-15
@zagayevskiy

asset manager. You open a file, read a line from it and create your own JSONObject from it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question