I keep getting errors during Json parser, so I put it up like this! I keep seeing the null value 구글 j The site where json imports is imported through Google spreadsheet https://script.google.com/macros/s/AKfycbxW2X5zPvG1ED7opyJ7_1zbHOvHQdVGn2rUmfDdabTA8rEkGf5o/exec It's a json site.Image is using Picasso!
public class tools_main2extendsAppCompatActivity {
private GridView mGridView;
private tools_adapter2 mGridAdapter;
private ArrayList<tools_item2> mGridData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridbase);
mGridView = (GridView) findViewById(R.id.gridView_base);
//Initialize with empty data
mGridData = new ArrayList<>();
mGridAdapter = new tools_adapter2(this, R.layout.tools_grid_view, mGridData);
mGridView.setAdapter(mGridAdapter);
//Start download
new AsyncHttpTask().execute("");
}
//Downloading data asynchronously
public class AsyncHttpTask extends AsyncTask<String, Void, String> {
HttpURLConnection urlConnection;
StringBuilder result;
@Override
protected String doInBackground(String... params) {
result = new StringBuilder();
urlConnection = null;
try {
URL url = new URL("https://script.google.com/macros/s/AKfycbxW2X5zPvG1ED7opyJ7_1zbHOvHQdVGn2rUmfDdabTA8rEkGf5o/exec");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
String result = "";
while ((line = reader.readLine()) != null) {
result += line;
}
parseResult(line);
// // Close stream
if (null != in) {
in.close();
}
return result;
} } catch (Exception e) {
e.printStackTrace();
} } finally {
assert urlConnection != null;
urlConnection.disconnect();
}
return result.toString();
}
@Override
protected void onPostExecute(String result) {
// // Download complete. Lets update UI
mGridAdapter.setGridData(mGridData);
}
String streamToString(InputStream stream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
String line;
String result = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
// // Close stream
if (null != stream) {
stream.close();
}
return result;
}
/**
* * Parsing the feed results and get the list
*
* * @param result
*/
private void parseResult(String result) {
try {
JSONObject response = new JSONObject(result);
JSONArray posts = response.optJSONArray("posts");
tools_item2 item;
for (int i = 0; i < posts.length(); i++)
{
String title = posts.optJSONObject(i).getString("title");
String image = posts.optJSONObject(i).getString("image");
item = new tools_item2();
item.setTitle(title);
item.setImage(image);
mGridData.add(item);
}
} } catch (JSONException e) {
e.printStackTrace();
}
}
}
}
It's the main screen sauce.
//public class tools_adapter2 extensions ArrayAdapter<tools_item2> {
//private final ColorMatrixColorFilter grayscaleFilter;
private Context mContext;
private int layoutResourceId;
private ArrayList<tools_item2> mGridData = new ArrayList<tools_item2>();
public tools_adapter2(Context mContext, int layoutResourceId, ArrayList<tools_item2> mGridData) {
super(mContext, layoutResourceId, mGridData);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.mGridData = mGridData;
}
/**
* * Updates grid data and refresh grid items.
*
* * @param mGridData
*/
public void setGridData(ArrayList<tools_item2> mGridData) {
this.mGridData = mGridData;
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.titleTextView = (TextView) row.findViewById(R.id.tools_grid_textview);
holder.imageView = (ImageView) row.findViewById(R.id.tools_grid_image);
row.setTag(holder);
} } else {
holder = (ViewHolder) row.getTag();
}
tools_item2 item = mGridData.get(position);
holder.titleTextView.setText(mGridData.get(position).getTitle());
Picasso.with(mContext).load(item.getImage()).into(holder.imageView);
return row;
}
static class ViewHolder {
TextView titleTextView;
ImageView imageView;
}
}
Adapter source.
public class tools_item2 {
private String image;
private String title;
public tools_item2() {
super();
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
Item source.
04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err:java.lang.NullPointerException:Attempt to invest virtual method 'strengthening'
04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err: at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err: at org.json.JSONTokener.nextValue(JSONTokener.java:94)
04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err: at org.json.JSONObject.<init>(JSONObject.java:156)
04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err: at org.androidtown.ezcoffee.tools.tools_main2$AsyncHttpTask.parseResult(tools_main2.java:118)
04-27 02:50:19.700 12218-12762/org.androidtown.ezcoffee W/System.err: at org.androidtown.ezcoffee.tools.tools_main2$AsyncHttpTask.doInBackground(tools_main2.java:70)
04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err: at org.androidtown.ezcoffee.tools.tools_main2$AsyncHttpTask.doInBackground(tools_main2.java:50)
04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-27 02:50:19.701 12218-12762/org.androidtown.ezcoffee W/System.err: at java.lang.Thread.run(Thread.java:818)
Error log source Please solve this problem. Thank you.
java json httpurlconnection parser android
Currently, https://script.google.com/macros/s/AKfycbxW2X5zPvG1ED7opyJ7_1zbHOvHQdVGn2rUmfDdabTA8rEkGf5o/exec You said you were getting Json from this site, but looking at the Json format, there is no "posts" key value. Clear JSONARRAY posts = response.optJSONARRAY("posts");
and try again as shown below.
private void parseResult(String result) {
try {
JSONArray response = new JSONArray(result);
tools_item2 item;
for (int i = 0; i < response.length(); i++) {
String title = response.optJSONObject(i).getString("title");
String image = response.optJSONObject(i).getString("image");
item = new tools_item2();
item.setTitle(title);
item.setImage(image);
mGridData.add(item);
}
} } catch (JSONException e) {
e.printStackTrace();
}
}
}
© 2024 OneMinuteCode. All rights reserved.