【发布时间】:2026-02-14 19:35:02
【问题描述】:
我正在使用这个代码来解析一个带有 JSON 的 REST 调用作为结果。 我想添加一个带有提交按钮的 editTestInput,该按钮是正在解析的 url 的一部分(基本上是为了让用户可以搜索他们想要的内容)。
这是我的代码,它只解析一个静态 url(注意:在 url 中有一个搜索词变量;这就是我想要输入的用户输入,它将开始解析 url 并返回结果JSON 格式):
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ListView lv;
public String term = "rabi";
public String url;
public EditText editTextInput;
ArrayList<HashMap<String, String>> itemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextInput = (EditText) findViewById(R.id.editTextInput);
itemList = new ArrayList<>();
lv = (ListView) findViewById(R.id.list);
new GetSearchItems().execute();
}
private class GetSearchItems extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(MainActivity.this,"SearchResults are downloading",Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String url = "https://www.googleapis.com/customsearch/v1?key=xxxxxxxxxxxxxxxxxxxxxxx&cx=xxxxxxxxxxxxxxxx:xxxxxxxxxx=" + term + "&gsc.sort=";
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray items = jsonObj.getJSONArray("items");
// looping through All results
for (int i = 0; i < items.length(); i++) {
JSONObject c = items.getJSONObject(i);
String title = c.getString("title");
String link = c.getString("link");
String displayLink = c.getString("displayLink");
String formattedUrl = c.getString("formattedUrl");
String snippet = c.getString("snippet");
// tmp hash map for single result
HashMap<String, String> item = new HashMap<>();
// adding each child node to HashMap key => value
item.put("title", title);
item.put("link", link);
item.put("displayLink", displayLink);
item.put("formattedUrl", formattedUrl);
item.put("snippet", snippet);
// adding contact to result list
itemList.add(item);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG).show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ListAdapter adapter = new SimpleAdapter(MainActivity.this, itemList,
R.layout.list_item, new String[]{ "title","link", "displayLink", "formattedUrl", "snippet"},
new int[]{R.id.title, R.id.link, R.id.displayLink, R.id.formattedUrl, R.id.snippet});
lv.setAdapter(adapter);
}
}
}
有人可以告诉我如何更改我当前的代码来实现这一点吗? 谢谢!
【问题讨论】:
标签: android json user-interface parsing