通过 URL 从 JSON 数据中获取数据并使用列表视图或回收器视图将其显示在 android 应用程序中
Posted
技术标签:
【中文标题】通过 URL 从 JSON 数据中获取数据并使用列表视图或回收器视图将其显示在 android 应用程序中【英文标题】:fetch the data from JSON data through URL and displaying it in android app using list view or recycler view 【发布时间】:2020-05-19 03:37:02 【问题描述】:我想在没有任何第三方库(如 Volley、Retrofit 等)的情况下显示 JSON URL 中存在的总数据。但我无法获取完整数据并将其显示在应用程序中,我只能显示对象。我的要求是调用我无法做到的每个数组及其内部对象。请帮助我,任何人。
-------XML 代码---
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
tools:context=".MainActivity">
<Button
android:layout_
android:layout_
android:text="Click Me"
android:layout_marginBottom="10dp"
android:id="@+id/button"/>
<ScrollView
android:layout_
android:layout_
android:layout_below="@+id/button">
<TextView
android:layout_
android:layout_
android:padding="5dp"
android:textSize="24sp"
android:id="@+id/fetcheddata"
android:hint="Feteched Text Here"/>
</ScrollView>
</RelativeLayout>
-------JSON数据-----
"Table":[
"Error_code":"0","Error_Message":"Success","EncryptionFlag":"N"
],
"Table1":[
"Fund_Id":"128","FN_FundDescription":"ABCD FUND","Fund_Id":"178","FN_FundDescription":"XYZ FUND","Fund_Id":"116","FN_FundDescription":"Pritham FUND","Fund_Id":"118","FN_FundDescription":"Ram FUND","Fund_Id":"130","FN_FundDescription":"Mannu FUND"]
-------FETCHED.JAVA------
public class fetchData extends AsyncTask<Void,Void,Void>
String data="";
String dataParsed="";
String singleParsed="";
@Override
protected Void doInBackground(Void... voids)
try
URL url=new URL("https://api.myjson.com/bins/baobi");
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line="";
while (line!=null)
line=bufferedReader.readLine();
data =data + line;
JSONArray JA = new JSONArray(data);
for (int i=0; i<JA.length();i++)
JSONObject JO= (JSONObject) JA.get(i);
singleParsed="Fund Id:" + JO.get("Fund_Id") +"\n" +
"Fund Description:" + JO.get("FN_FundDescription") +"\n";
dataParsed = dataParsed + singleParsed + "\n";
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (JSONException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(Void aVoid)
super.onPostExecute(aVoid);
MainActivity.data.setText(this.dataParsed);
-----主要活动.JAVA-----
public class MainActivity extends AppCompatActivity
Button click;
public static TextView data;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click=findViewById(R.id.button);
data=(TextView)findViewById(R.id.fetcheddata);
click.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
fetchData process=new fetchData();
process.execute();
);
【问题讨论】:
使用这个 runOnUiThread(new Runnable() @Override public void run() ); 执行后 您遇到任何错误? 就像@Varun Raj 建议的那样,加上使用 Recyclerview 列出来自 array 的数据会更好。这些是 youtube 和博客中的大量教程。 我没有收到任何错误,问题是我想通过调用它的数组名称和它有很多数组来显示 json 对象,我没有得到 【参考方案1】:试试这个,它适用于当前响应
public class fetchData extends AsyncTask<Void,Void,String>
String data="";
String dataParsed="";
String singleParsed="";
@Override
protected String doInBackground(Void... voids)
try
URL url=new URL("https://api.myjson.com/bins/baobi");
HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
String line="";
while (line!=null)
line=bufferedReader.readLine();
data =data + line;
JSONObject object = new JSONObject(data);
JSONArray JA=object.optJSONArray("Table1");
JSONArray Error=object.optJSONArray("Table");
for (int i=0; i<JA.length();i++)
JSONObject JO= (JSONObject) JA.get(i);
singleParsed="Fund Id:" + JO.get("Fund_Id") +"\n" +
"Fund Description:" + JO.get("FN_FundDescription") +"\n";
dataParsed = dataParsed + singleParsed + "\n";
for (int j=0; j<Error.length();j++)
JSONObject JO= (JSONObject) Error.get(j);
singleParsed="Error_code:" + JO.get("Error_code") +"\n" +
"Error_Message:" + JO.get("Error_Message") +"\n"+
"EncryptionFlag:" + JO.get("EncryptionFlag") +"\n";
dataParsed = dataParsed + singleParsed + "\n";
return dataParsed;
catch (MalformedURLException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
catch (JSONException e)
e.printStackTrace();
return null;
@Override
protected void onPostExecute(String aVoid)
super.onPostExecute(aVoid);
Log.e("data asyc",dataParsed+"");
onPostExecute 我有打印日志,在我的情况下输出是
数据异步:
Fund Id:128
Fund Description:ABCD FUND
Fund Id:178
Fund Description:XYZ FUND
Fund Id:116
Fund Description:Pritham FUND
Fund Id:118
Fund Description:Ram FUND
Fund Id:130
Fund Description:Mannu FUND
Error_code:0
Error_Message:Success
EncryptionFlag:N
【讨论】:
但在你的情况下,我得到相同的旧输出,我希望“表”数组对象也与表 2 一起显示 是的兄弟其实也是数据之一 请推荐我的答案以供审核以上是关于通过 URL 从 JSON 数据中获取数据并使用列表视图或回收器视图将其显示在 android 应用程序中的主要内容,如果未能解决你的问题,请参考以下文章
如何从 url 获取 json 数据并将其保存到 const 变量 [TypeScript]