【问题标题】:Any way to edit a URI based on User Input?有什么方法可以根据用户输入编辑 URI?
【发布时间】:2012-04-08 05:18:31
【问题描述】:

我有一个从 URI 接收信息的 HTTP GET。该 URI 用于 Google 购物。

https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=digital+camera&alt=atom

(把我的钥匙放在外面)。

有什么办法可以改

q=digital+camera

对于用户放入 EditText 的任何内容?

所以基本上,我希望 EditText 更改在 Google 购物上搜索的内容。

第一个屏幕,带有 EditText 的 ProductSearchEntry 用于搜索查询:

ProductSearchEntry 代码

public class ProductSearchEntry extends Activity{
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.productsearchentry);

    Button search = (Button) findViewById(R.id.searchButton);

    search.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent searchIntent = new Intent(getApplicationContext(), ProductSearch.class);
                startActivity(searchIntent);
        }
    });
    }
}

然后,我有第二个课程,ProductSearch,没有图片,只有这段代码:

public class ProductSearch extends Activity{
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.productsearchresults);
     EditText searchQuery = (EditText) findViewById(R.id.searchQuery);

        ProductSearchMethod test = new ProductSearchMethod();
        String entry;
        TextView httpStuff = (TextView) findViewById(R.id.httpTextView);
        try {
            entry = test.getSearchData(searchQuery.getText().toString());
            httpStuff.setText(entry);
              } catch (Exception e) {
                        e.printStackTrace();
    }
}
}

它引用了 ProductSearchMethod 类,该类由一个 TextView 组成,该 TextView 更改为 HTTP GET 中收到的代码:

代码:

public class ProductSearchMethod {

public String getSearchData(String query) throws Exception{
    BufferedReader in = null;
    String data = null;
    try{
        HttpClient client = new DefaultHttpClient();
        URI site = new URI("https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q="+query.replace(" ","+")+"&alt=atom");
     HttpGet request = new HttpGet();
    request.setURI(site);
    HttpResponse response = client.execute(request);
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer sb = new StringBuffer("");
    String l = "";
    String nl = System.getProperty("line.seperator");
    while((l = in.readLine()) !=null){
        sb.append(l + nl);
    }
    in.close();
    data = sb.toString();
    return data;
    }finally{
        if (in != null){
            try{
                in.close();
                return data;
            }catch (Exception e){
                e.printStackTrace();
                }
            }
        } 
    }
}

ProductSearchMethod 很好,但它不会将文本从“加载项”更改为网站代码。我之前有它工作,但后来我尝试编辑它搜索的内容(所有这些 ^),现在它没有改变。

【问题讨论】:

    标签: java android android-edittext uri


    【解决方案1】:

    修改你的代码,比如

    public class ProductSearchEntry extends Activity{ 
    protected void onCreate(Bundle savedInstanceState){ 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.productsearchentry); 
        EditText etSearch = (EditText) findViewById(id of your edittext);
        Button search = (Button) findViewById(R.id.searchButton); 
    
        search.setOnClickListener(new View.OnClickListener() { 
    
            @Override 
            public void onClick(View v) { 
                //while calling intent 
    
                Intent searchIntent = new Intent(getApplicationContext(), ProductSearch.class); 
                searchIntent.putExtra("searchText",etSearch.getText().toString());
                startActivity(searchIntent); 
            } 
        }); 
        } 
    } 
    

    还有这样的活动,

    public class ProductSearch extends Activity{     
    protected void onCreate(Bundle savedInstanceState){     
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.productsearchresults);     
            String searchQuery = getIntent().getStringExtra("searchText");     
            ProductSearchMethod test = new ProductSearchMethod();     
            String entry;     
            TextView httpStuff = (TextView) findViewById(R.id.httpTextView);     
            try {     
                entry = test.getSearchData(searchQuery);     
                httpStuff.setText(entry);     
                  } catch (Exception e) {     
                            e.printStackTrace();     
        }     
    }     
    }   
    

    【讨论】:

    • 是的!我修好了它。非常感谢!
    【解决方案2】:

    是的...更改您的 getSearchData() 方法以包含一个字符串作为参数

    public String getSearchData(String query) throws Exception{

    然后,将该字符串插入查询 URL,用“+”替换空格。您可能希望对字符串进行进一步的调节,例如对其进行 URL 编码。

    URI site = new URI("https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q="+query.replace(" ","+")+"&alt=atom");

    在您的 XML 中,创建一个包含以下行的按钮:

    android:onClick="search"

    在您的 ProductSearch 活动中,添加以下方法,并将 onCreate 中的代码移动到其中。您还需要在 XML 中创建一个 EditText 以供输入。

    public void search(View v)
    {
        EditText searchQuery = (EditText) findViewById(R.id.searchQuery);
    
        ProductSearchMethod test = new ProductSearchMethod();
        String returned;
        try {
            returned = test.getSearchData(searchQuery.getText().toString());
            httpStuff.setText(returned);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
    
        }
    }
    

    最后,您可能需要阅读正在运行的异步任务,以便查询不会在执行时冻结您的应用。

    【讨论】:

    • 增加了赏金并彻底改变了 OP。
    【解决方案3】:

    可能是我弄错了,但你为什么不把它作为参数传递给

    getSearchData() => getSearchData(string query) 
    

    那你就可以换行了

    URI site = new URI("https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=digital+camera&alt=atom");
    

    URI site = new URI("https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=+ URLEncoder.encode(query, "UTF-8")+&alt=atom");
    

    【讨论】:

    • UTF-8 有什么作用?我的意思是我该如何使用它?
    【解决方案4】:

    查看http://androidforums.com/developer-101/528924-arduino-android-internet-garage-door-works-but-could-use-input.html 我使用 Asynctask 在本地 Arduino 服务器上触发 get 命令。它将 Arduino 的 pin 号附加到 URL 的末尾,具体取决于是否需要。我相信你可以用它来帮助你。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 2012-02-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多