将用户输入的editText添加到Android中的url休息调用中

将用户输入的editText添加到Android中的url休息调用中

问题描述:

我正在使用此代码来解析带有JSON的REST调用作为结果。 我想用一个提交按钮来添加一个editTestInput,这个提交按钮是正在被解析的url的一部分(基本上是为了让用户可以搜索他们想要的东西)。将用户输入的editText添加到Android中的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); 
     } 
    } 
} 

有人可以告诉我如何改变我目前的代码来完成? 谢谢!

我通过将editTextInput添加到onCreate方法来解决我的问题。 然后我把代码放入onClick方法中。 我的应用程序现在按预期工作。