有什么方法可以根据用户输入编辑URI吗?

时间:2012-04-08 05:18:31

标签: java android android-edittext uri

我有一个HTTP GET,它从URI接收信息。该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:

enter image description here

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中收到的代码:

enter image description here

代码:

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很棒,但它不会将文本从“加载项目”更改为网站代码。我之前有过工作,但之后我尝试编辑它搜索的内容(所有这些^),现在它没有改变。

4 个答案:

答案 0 :(得分: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();     
    }     
}     
}   

答案 1 :(得分:0)

是的...更改你的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();

    }
}

最后,您可能希望阅读有关运行异步任务的信息,以便查询在执行时不会冻结您的应用。

答案 2 :(得分:0)

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

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");

答案 3 :(得分:0)

签出http://androidforums.com/developer-101/528924-arduino-android-internet-garage-door-works-but-could-use-input.html我使用Asynctask在本地Arduino服务器上触发get命令。它附加了Arduino的引脚号,并根据是否需要,在URL的末尾添加一个端口号。我相信你可以用它来帮助你。

相关问题