Java Json不显示url数据
我正在尝试从Google电子表格中获取JSON数据,但它没有显示。我不知道为什么。我认为我的JSON响应与match类不匹配。请查看并帮助我解决问题 这是我的AsyncResult界面Java Json不显示url数据,java,android,listview,Java,Android,Listview,我正在尝试从Google电子表格中获取JSON数据,但它没有显示。我不知道为什么。我认为我的JSON响应与match类不匹配。请查看并帮助我解决问题 这是我的AsyncResult界面 package com.textview.android.jsonsuccessful; 导入org.json.JSONObject; 接口异步结果{ void onResult(JSONObject对象); } 这是我用来获取URL数据的类 package com.textview.android.jsonsu
package com.textview.android.jsonsuccessful;
导入org.json.JSONObject;
接口异步结果{
void onResult(JSONObject对象);
}
这是我用来获取URL数据的类
package com.textview.android.jsonsuccessful;
import android.os.AsyncTask;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadWebpageTask extends AsyncTask<String, Void, String> {
AsyncResult callback;
public DownloadWebpageTask(AsyncResult callback) {
this.callback = callback;
}
@Override
protected String doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to download the requested page.";
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
// remove the unnecessary parts from the response and construct a JSON
int start = result.indexOf("{", result.indexOf("{") + 1);
int end = result.lastIndexOf("}");
String jsonResponse = result.substring(start, end);
try {
JSONObject team = new JSONObject(jsonResponse);
callback.onResult(team);
} catch (JSONException e) {
e.printStackTrace();
}
}
private String downloadUrl(String urlString) throws IOException {
InputStream is = null;
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int responseCode = conn.getResponseCode();
is = conn.getInputStream();
String contentAsString = convertStreamToString(is);
return contentAsString;
} finally {
if (is != null) {
is.close();
}
}
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new
InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
这是我的适配器
package com.textview.android.jsonsuccessful;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class TeamsAdapter extends ArrayAdapter<Team> {
Context context;
private ArrayList<Team> teams;
public TeamsAdapter(Context context, int textViewResourceId, ArrayList<Team> items) {
super(context, textViewResourceId, items);
this.context = context;
this.teams = items;
}
@Override
public View getView(int Code, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.team, null);
}
Team o = teams.get(Code);
if (o != null) {
TextView Cod = (TextView) v.findViewById(R.id.Code);
TextView Name = (TextView) v.findViewById(R.id.Name);
TextView Father = (TextView) v.findViewById(R.id.Father);
TextView Comments = (TextView) v.findViewById(R.id.Comments);
TextView Sessional = (TextView) v.findViewById(R.id.Sessional);
Cod.setText(String.valueOf(o.getCode()));
Name.setText(String.valueOf(o.getName()));
Father.setText(String.valueOf(o.getFatherName()));
Sessional.setText(String.valueOf(o.getSessionalMarks()));
Comments.setText(String.valueOf(o.getComments()));
}
return v;
}
package com.textview.android.jsonsuccessful;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类TeamsAdapter扩展了ArrayAdapter{
语境;
私人ArrayList团队;
公共团队适配器(上下文、int textViewResourceId、ArrayList项){
super(上下文、textViewResourceId、项);
this.context=上下文;
此项=项目;
}
@凌驾
公共视图getView(int代码、视图转换视图、视图组父级){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.团队,空);
}
Team o=teams.get(代码);
如果(o!=null){
TextView Cod=(TextView)v.findViewById(R.id.Code);
TextView Name=(TextView)v.findViewById(R.id.Name);
TextView父亲=(TextView)v.findViewById(R.id.Father);
TextView注释=(TextView)v.findViewById(R.id.Comments);
TextView Sessional=(TextView)v.findviewbyd(R.id.Sessional);
Cod.setText(String.valueOf(o.getCode());
Name.setText(String.valueOf(o.getName());
父.setText(String.valueOf(o.getFatherName());
Sessional.setText(String.valueOf(o.getSessionalMarks());
Comments.setText(String.valueOf(o.getComments());
}
返回v;
}
}
这是我的主要xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.textview.android.jsonsuccessful.MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/btnDownload" />
<Button
android:id="@+id/btnDownload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:onClick="buttonClickHandler"
android:text="download table"
/>
</RelativeLayout>`
`
这是my team.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/Code"
android:text="201"
android:layout_width="0dp"
android:layout_weight=".35"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Name"
android:text="ankit"
android:layout_width="0dp"
android:layout_weight=".50"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Father"
android:text="Ramesh"
android:layout_width="0dp"
android:layout_weight=".70"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Sessional"
android:text="maths 15 ,english 20, physics 30,computer 7,Gk 12"
android:layout_width="0dp"
android:layout_weight="1.5"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Comments"
android:text="should put more efforts"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
解析JSON数据时,您正在解析JSON文件中不存在的一个额外对象“Comments”。 因此,您的阵列“团队”没有填充数据
public class MainActivity extends AppCompatActivity {
private static final String DEBUG_TAG = "HttpExample";
ArrayList<Team> teams = new ArrayList<Team>();
ListView listview;
Button btnDownload;
TeamsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.listview);
btnDownload = (Button) findViewById(R.id.btnDownload);
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
btnDownload.setEnabled(true);
} else {
btnDownload.setEnabled(false);
}
}
public void buttonClickHandler(View view) {
new DownloadWebpageTask(new AsyncResult() {
@Override
public void onResult(JSONObject object) {
processJson(object);
}
}).execute("https://spreadsheets.google.com/tq?key=1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU");
}
private void processJson(final JSONObject object) {
try {
JSONArray rows = object.getJSONArray("rows");
for (int r = 0; r < rows.length(); ++r) {
JSONObject row = rows.getJSONObject(r);
JSONArray columns = row.getJSONArray("c");
int Code = columns.getJSONObject(0).getInt("v");
String Name = columns.getJSONObject(1).getString("v");
String Father = columns.getJSONObject(3).getString("v");
String Sessional = columns.getJSONObject(4).getString("v");
//String Comments = columns.getJSONObject(5).getString("v");
String Comments = "Error";
Team team = new Team(Code, Name, Father, Sessional, Comments);
teams.add(team);
}
adapter = new TeamsAdapter(MainActivity.this, R.layout.team, teams);
listview.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public类MainActivity扩展了AppCompatActivity{
私有静态最终字符串DEBUG_TAG=“HttpExample”;
ArrayList团队=新建ArrayList();
列表视图列表视图;
按钮B卸载;
TeamsAdapter适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview=(listview)findViewById(R.id.listview);
btnDownload=(按钮)findViewById(R.id.btnDownload);
ConnectivityManager connMgr=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo NetworkInfo=connMgr.getActiveNetworkInfo();
if(networkInfo!=null&&networkInfo.isConnected()){
btnDownload.setEnabled(true);
}否则{
btnDownload.setEnabled(false);
}
}
公共无效按钮ClickHandler(视图){
新建下载WebPagetTask(新建AsyncResult()){
@凌驾
公共void onResult(JSONObject对象){
processJson(对象);
}
}).执行(”https://spreadsheets.google.com/tq?key=1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU");
}
私有void processJson(最终JSONObject对象){
试一试{
JSONArray rows=object.getJSONArray(“rows”);
对于(int r=0;r
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/Code"
android:text="201"
android:layout_width="0dp"
android:layout_weight=".35"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Name"
android:text="ankit"
android:layout_width="0dp"
android:layout_weight=".50"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Father"
android:text="Ramesh"
android:layout_width="0dp"
android:layout_weight=".70"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Sessional"
android:text="maths 15 ,english 20, physics 30,computer 7,Gk 12"
android:layout_width="0dp"
android:layout_weight="1.5"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Comments"
android:text="should put more efforts"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
public class MainActivity extends AppCompatActivity {
private static final String DEBUG_TAG = "HttpExample";
ArrayList<Team> teams = new ArrayList<Team>();
ListView listview;
Button btnDownload;
TeamsAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listview = (ListView) findViewById(R.id.listview);
btnDownload = (Button) findViewById(R.id.btnDownload);
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
btnDownload.setEnabled(true);
} else {
btnDownload.setEnabled(false);
}
}
public void buttonClickHandler(View view) {
new DownloadWebpageTask(new AsyncResult() {
@Override
public void onResult(JSONObject object) {
processJson(object);
}
}).execute("https://spreadsheets.google.com/tq?key=1424bS7kU8nJbHdu4QdoAFdIdWDSnmEnj2NqfMb6rPTU");
}
private void processJson(final JSONObject object) {
try {
JSONArray rows = object.getJSONArray("rows");
for (int r = 0; r < rows.length(); ++r) {
JSONObject row = rows.getJSONObject(r);
JSONArray columns = row.getJSONArray("c");
int Code = columns.getJSONObject(0).getInt("v");
String Name = columns.getJSONObject(1).getString("v");
String Father = columns.getJSONObject(3).getString("v");
String Sessional = columns.getJSONObject(4).getString("v");
//String Comments = columns.getJSONObject(5).getString("v");
String Comments = "Error";
Team team = new Team(Code, Name, Father, Sessional, Comments);
teams.add(team);
}
adapter = new TeamsAdapter(MainActivity.this, R.layout.team, teams);
listview.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}