试图通过意图数据使用杰克逊解析并添加到 pojo 类并返回但无法发送和获取?
主要活动是..
public class MainActivity extends AppCompatActivity {
private Button getButton;
private TextView textView;
private String jsonData;
@Override
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity_json);
super.onCreate(savedInstanceState);
getButton = (Button) findViewById(R.id.getButton);
textView = (TextView) findViewById(R.id.textView);
getButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
new JSONTask().execute("http://192.168.11.75/Headspire/api.php");
}
});
}
public class JSONTask extends AsyncTask<String, String, String>
{
private String json_string = "";
@Override
protected String doInBackground(String... params) {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream stream = urlConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
while ((json_string = reader.readLine()) != null) {
buffer.append(json_string);
}
return buffer.toString();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(final String result) {
super.onPostExecute(result);
textView.setText(result);
jsonData = result;
}
}
public void parseJSON(View view)
{
if (jsonData == null)
{
Toast.makeText(getApplicationContext(),
"Get JSON data first", Toast.LENGTH_SHORT).show();
}
else
{
Intent intent = new Intent(this, ActivityListView.class);
intent.putExtra("json_data", jsonData);
startActivity(intent);
}
}
}
这是我的 ActivityListView 类...
public class ActivityListView extends AppCompatActivity
{
private String jsonData;
private ListView listView;
private PersonAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_list_view);
listView = (ListView) findViewById(R.id.listView);
adapter = new PersonAdapter(this, R.layout.row);
listView.setAdapter(adapter);
jsonData = getIntent().getExtras().getString("json_data");
ObjectMapper mapper = new ObjectMapper();
try
{
PersonModel model = mapper.readValue(jsonData, PersonModel.class);
model.getName();
model.getId();
model.getDescription();
model.getRating();
adapter.add(model);
}
catch (JsonParseException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
我的 PersonAdapeter 类看起来像这样
public class PersonAdapter extends ArrayAdapter
{
private List list = new ArrayList();
public PersonAdapter(Context context, int resource) {
super(context, resource);
}
@SuppressWarnings("unchecked")
public void add(PersonModel 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;
NameHolder nameHolder;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.row, parent, false);
nameHolder = new NameHolder();
nameHolder.nameTextView = (TextView) row.findViewById(R.id.nameTextView);
nameHolder.idTextView = (TextView) row.findViewById(R.id.idTextView);
nameHolder.descriptionTextView = (TextView) row.findViewById(R.id.descriptionTextView);
nameHolder.ratingTextView = (TextView) row.findViewById(R.id.ratingTextView);
row.setTag(nameHolder);
}
else {
nameHolder = (NameHolder)row.getTag();
}
PersonModel model = (PersonModel) this.getItem(position);
nameHolder.nameTextView.setText(model.getName());
nameHolder.idTextView.setText(model.getId());
nameHolder.descriptionTextView.setText(model.getDescription());
nameHolder.ratingTextView.setText(model.getRating());
return row;
}
static class NameHolder
{
TextView nameTextView, idTextView, descriptionTextView, ratingTextView;
}
}
这是我的 PersonModel 类...
@JsonIgnoreProperties(ignoreUnknown=true)
public class PersonModel extends AppCompatActivity
{
private String name;
private String id;
private String description;
private String rating;
public PersonModel(String name, String id, String description,
String rating)
{
this.name = name;
this.id = id;
this.description = description;
this.rating = rating;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
@Override
public String toString()
{
return "PersonModel [name=" + name + ", id=" + id + ",
description=" + description + ", " +
"rating=" + rating +"]";
}
}