android - 如何在 textview 中显示来自 json 的特定条目?
问题描述
我有一个包含一个编辑文本、按钮和 Textview 的活动。现在我想如果有人在编辑文本中输入 Java,那么它的信息将被填充到文本视图中。这是我的 JSON。我还包括了我的 .java 文件。
{
"compscience": [
{
"bookname": "java",
"row": "1",
"column": "1"
},
{
"bookname": "cns",
"row": "2",
"column": "2"
},
{
"bookname": "rdbms",
"row": "3",
"column": "3"
},
{
"bookname": "daa",
"row": "4",
"column": "4"
}
]
}
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class act1 extends AppCompatActivity {
EditText subname;
Button fetch;
static String ab,single="";
public static String c;
static TextView data;
fetchdata process;
StringBuffer response ;
void init()
{
subname = findViewById(R.id.subname);
ab = subname.toString();
fetch = findViewById(R.id.button);
data=findViewById(R.id.result);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act1);
init();
}
public void fetch(View view)
{
process = new fetchdata();
process.execute();//it will start do in background
}
class fetchdata extends AsyncTask {
@Override
protected Object doInBackground(Object[] objects) {
response = new StringBuffer();
try {
URL url = new URL("https://api.myjson.com/bins/c1054");
URLConnection connection =url.openConnection();
InputStream inputStream = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
BufferedReader buffer = new BufferedReader(reader);
String line = "";
while((line = buffer.readLine()) != null) {
response.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
try {
JSONArray array=new JSONArray("respnse");
for(int i=0;i<array.length();i++)
{
JSONObject obj=array.getJSONObject(i);
if(ab.equals(obj.getString("booknmae")))
{
single = single +obj.getString("row");
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
act1.data.setText(single);
}
}
}
解决方案
首先,您需要了解如何JSONObject
和JSONArray
工作,您的 json 响应是JSONObject
并且其中有一个名称为值的compscience
键JSONArray
。
尝试这个:
try {
JSONObject respJson = new JSONObject(response.toString());
JSONArray array = respJson.getJSONArray("compscience");
for(int i=0;i<array.length();i++)
{
JSONObject obj=array.getJSONObject(i);
if(ab.equals(obj.getString("booknmae")))
{
single = single +obj.getString("row");
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
推荐阅读
- c - 如何在 C 中使用用户输入创建字符串函数?
- django - 在Django Rest Api上按下POST按钮时如何调用函数
- listview - 如何在 Flutter ListView Builder 中移除或管理空间?
- regex - 在模式匹配之间提取值
- php - Laravel 注册表单不返回任何错误
- c# - 创建与类属性具有相同类型的列表
- r - r中的用户登录时间图
- typescript - React Navigation TypeScript:类型的参数......不可分配给“BottomTabNavigatorConfig”类型的参数
- curl - 使用外部 API 查找优化 BigQuery 表
- apache - .htaccess 重定向到除管理页面之外的另一个域