我从URL获取JSON - WooCommerce API。Java Android Studio restTemplate解析JSON
问题是我无法在应用程序中解析/输出它们。
没有错误,但它没有显示数据 - 只是标签。
MainActivity.java:
package at.copy_cat.app.rest;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
protected void onStart() {
super.onStart();
new HttpRequestTask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_refresh) {
new HttpRequestTask().execute();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
private class HttpRequestTask extends AsyncTask<Void, Void, Greeting> {
@Override
protected Greeting doInBackground(Void... params) {
try {
final String url = "http://copy-cat.at/api/index.json";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
/**Greeting greeting = restTemplate.getForObject(url, Greeting[].class);**/
Greeting greeting = restTemplate.getForObject(url, Greeting.class);
/** Greeting greeting = Arrays.asList(restTemplate.getForObject(url, Greeting[].class));**/
RestTemplate rest = new RestTemplate();
return greeting;
} catch (Exception e) {
Log.e("MainActivity", e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(Greeting greeting) {
TextView greetingIdText = (TextView) findViewById(R.id.id_value);
TextView greetingContentText = (TextView) findViewById(R.id.content_value);
greetingIdText.setText(greeting.getId());
greetingContentText.setText(greeting.getContent());
}
}
}
Greeting.java:
package at.copy_cat.app.rest;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
/**
* Created by Michael on 17.10.2017.
*/
@JsonIgnoreProperties(ignoreUnknown=true)
public class Greeting {
private String products;
public String title;
public String getId() {
return this.products;
}
public String getContent() {
return this.title;
}
}
用一个简单的JSON这样的: { “ID”:161, “内容”:“你好,世界!“} 它的工作,但没有与此生成的JSON here。
是它的一个PHP文件,但我在Java代码中包含PHP脚本相同的输出以.json使用...
的问题是:我怎么能解析同类商品的ID等信息
非常感谢!
编辑/ UPDATE: 现在,它看起来像这样
package at.copy_cat.app.rest;
import android.util.Log;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.List;
/**
* Created by Michael on 17.10.2017.
*/
@JsonIgnoreProperties(ignoreUnknown=true)
public class Greeting {
// getters and setters
public List products;
public String getContent(){
String convertedToString = "" + products;
/**String[] value_split = convertedToString.split(",");**/
String[] value_split = convertedToString.split("\\,");
try {
JSONArray json = new JSONArray(products);
for(int i=0;i<json.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = json.getJSONObject(i);
/** Log.v("TEST23243235", "TESTOBJ" + e);**/
/**JSONArray people = e.getJSONArray("products");
int numOfPeople = e.getInt("images");**/
map.put("id", String.valueOf(i));
map.put("productid", "Product ID:" + e.getString("id"));
map.put("title", "Title: " + e.getString("title"));
map.put("permalink", "Permalink: " + e.getString("permalink"));
map.put("status", "Status: " + e.getString("status"));
map.put("regular_price", "Regular Price: " + e.getString("regular_price"));
map.put("price", "Price: " + e.getString("price"));
// map.put("description", "Description: " + e.getString("description"));
// map.put("categories", "Categories: " + e.getString("categories"));
// map.put("images", "Images: " + e.getString("images"));
Log.v("TEST23243235", "TESTOBJ" + map);
Log.v("TEST23243235", "TESTOBJ" + json);
Log.v("TEST23243235", "TESTOBJI" + i);
return map.toString();
}
// Extract data from json and store into ArrayList as class objects
/**JSONObject json_data = jArray.getJSONObject(i);
String id = jObj.getString("id");
Log.d("TEST23243235", "TEST" + id);**/
} catch (JSONException e) {
Log.e("TEST23243235", "unexpected JSON exception", e);
// Do something to recover ... or kill the app.
}
return "";
}
public void setProducts(List products){
this.products = products;
}
public class Model {
private String title;
//other fields
//getters and setters
}
public String getId() {
return "1";
}
/** public String getContent() {
return this.convertedToString;
}**/
}
输出在logcat的
TESTOBJ{price=Price: 30.00, regular_price=Regular Price: 0.00, title=Title: Camera DS, status=Status: publish, permalink=Permalink: http://copy-cat.at/produkt/camera-ds-2, id=0, productid=Product ID:590}
TESTOBJI0
所以,现在我可以1个产品,但输出JSON的不是两个或更多.. .. 我能做些什么来解析所有产品不只是一个?
是的代码是不是干净的 - 关于我学习很抱歉...... :)
谢谢,但它不是解决方案:( – user2806316
@ user2806316我刚刚检查过.json版本不返回json它重定向到html版本。你确定你的URL吗? – Valentun
url是http:// copy -cat.at/api/index.json – user2806316