JSON 正在 android 中接收,但不会解析。我的问题是什么?
JSON is being received in android, but it won't parse. What is my issue?
如标题所示,JSON 的格式如下:
http://prntscr.com/elb4fz。现在我不确定我的解析方法是否正常工作,因为我认为 SearchResults 是数组,它会将每个单独的结果显示为 object。
我有这样的 networkutils 代码:
package companionsdirect.practice;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Albedo on 3/17/2017.
*/
public class NetworkingUtil {
private static final String TAG = "NetworkUtils";
List<Product> items = new ArrayList<>();
public byte[] getUrlBytes(String urlSpec) throws IOException {
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException(connection.getResponseMessage() +
": with " + urlSpec);
}
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
} finally {
connection.disconnect();
}
}
public String getUrlString(String urlSpec) throws IOException {
return new String(getUrlBytes(urlSpec));
}
public List<Product> fetchItems(String url) {
try {
String jsonString = getUrlString(url);
Log.i(TAG, "Received JSON: " + jsonString);
JSONObject jsonBody = new JSONObject(jsonString);
parseItems(items, jsonBody);
} catch (IOException ioe) {
Log.i(TAG, "Failed to fetch items", ioe);
} catch (JSONException je) {
Log.e(TAG, "Failed to parse JSON", je);
}
return items;
}
public List<Product> getProdList() {
return items;
}
private void parseItems(List<Product> items, JSONObject jsonBody) throws IOException, JSONException {
//JSONObject reviewItem = jsonBody.getJSONObject();
JSONArray reviewArray = jsonBody.getJSONArray("SearchResults");
for(int i = 0;i<reviewArray.length();i++) {
JSONObject reviewItem = reviewArray.getJSONObject(i);
Product currentProductReview = new Product();
currentProductReview.setmSummary(reviewItem.getString("Summary"));
currentProductReview.setmTitle(reviewItem.getString("Title"));
currentProductReview.setmRecordType(reviewItem.getString("RecordType"));
items.add(currentProductReview);
}
}
}
这是带有异步任务的 activity:
package companionsdirect.practice;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ReturnReviewsActivity extends AppCompatActivity {
public String builtURL;
List<Product> prodList;
private static final String TAG = "ReturnReviewsActivity";
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_return_reviews);
Intent intent = getIntent();
String url = intent.getStringExtra(MainActivity.EXTRA_MSG);
prodList = new ArrayList<Product>();
builtURL = url;
new FetchItemsTask().execute();
tv = (TextView) findViewById(R.id.tv);
for(Product x : prodList) {
tv.setText(x.getmTitle() + " " + x.getRating() + " \n");
}
}
private class FetchItemsTask extends AsyncTask<Void, Void, Void> {
private List<Product> list;
@Override
protected Void doInBackground(Void... params) {
NetworkingUtil productData = new NetworkingUtil();
list = productData.fetchItems(builtURL);
return null;
}
}
}
什么给了?日志显示正在接收 json,但我尝试将 textview 设置为列表中的任何内容,但没有任何显示。
试试这个
tv = (TextView) findViewById(R.id.tv);
for(Product x : prodList) {
//append the text view
tv.append(x.getmTitle() + " " + x.getRating() + " \n");
}
如标题所示,JSON 的格式如下: http://prntscr.com/elb4fz。现在我不确定我的解析方法是否正常工作,因为我认为 SearchResults 是数组,它会将每个单独的结果显示为 object。
我有这样的 networkutils 代码:
package companionsdirect.practice;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Albedo on 3/17/2017.
*/
public class NetworkingUtil {
private static final String TAG = "NetworkUtils";
List<Product> items = new ArrayList<>();
public byte[] getUrlBytes(String urlSpec) throws IOException {
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new IOException(connection.getResponseMessage() +
": with " + urlSpec);
}
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
} finally {
connection.disconnect();
}
}
public String getUrlString(String urlSpec) throws IOException {
return new String(getUrlBytes(urlSpec));
}
public List<Product> fetchItems(String url) {
try {
String jsonString = getUrlString(url);
Log.i(TAG, "Received JSON: " + jsonString);
JSONObject jsonBody = new JSONObject(jsonString);
parseItems(items, jsonBody);
} catch (IOException ioe) {
Log.i(TAG, "Failed to fetch items", ioe);
} catch (JSONException je) {
Log.e(TAG, "Failed to parse JSON", je);
}
return items;
}
public List<Product> getProdList() {
return items;
}
private void parseItems(List<Product> items, JSONObject jsonBody) throws IOException, JSONException {
//JSONObject reviewItem = jsonBody.getJSONObject();
JSONArray reviewArray = jsonBody.getJSONArray("SearchResults");
for(int i = 0;i<reviewArray.length();i++) {
JSONObject reviewItem = reviewArray.getJSONObject(i);
Product currentProductReview = new Product();
currentProductReview.setmSummary(reviewItem.getString("Summary"));
currentProductReview.setmTitle(reviewItem.getString("Title"));
currentProductReview.setmRecordType(reviewItem.getString("RecordType"));
items.add(currentProductReview);
}
}
}
这是带有异步任务的 activity:
package companionsdirect.practice;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ReturnReviewsActivity extends AppCompatActivity {
public String builtURL;
List<Product> prodList;
private static final String TAG = "ReturnReviewsActivity";
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_return_reviews);
Intent intent = getIntent();
String url = intent.getStringExtra(MainActivity.EXTRA_MSG);
prodList = new ArrayList<Product>();
builtURL = url;
new FetchItemsTask().execute();
tv = (TextView) findViewById(R.id.tv);
for(Product x : prodList) {
tv.setText(x.getmTitle() + " " + x.getRating() + " \n");
}
}
private class FetchItemsTask extends AsyncTask<Void, Void, Void> {
private List<Product> list;
@Override
protected Void doInBackground(Void... params) {
NetworkingUtil productData = new NetworkingUtil();
list = productData.fetchItems(builtURL);
return null;
}
}
}
什么给了?日志显示正在接收 json,但我尝试将 textview 设置为列表中的任何内容,但没有任何显示。
试试这个
tv = (TextView) findViewById(R.id.tv);
for(Product x : prodList) {
//append the text view
tv.append(x.getmTitle() + " " + x.getRating() + " \n");
}