Any way to edit the URI based on user input?

I have an HTTP GET that gets information from a URI. The URI is for Google Shopping.

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

      

(Left my key.)

Is there a way to change it from

q=digital+camera

      

on anything the user puts in the EditText?

So, basically, I want the EditText to change the Google Shopping search.

First screen, ProductSearchEntry with EditText for search query:

enter image description here

Code for 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);
        }
    });
    }
}

      

Then I have a second ProductSearch class with no image, but only this code:

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();
    }
}
}

      

Here's a reference to the ProductSearchMethod class, which consists of a TextView that has been changed to the code received in an HTTP GET:

enter image description here

code:

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 works fine, but doesn't change the text from "Loading Items" to website code. I've worked on this before, but then tried to edit what he was looking for (all this ^) and now it doesn't change.

+3


source to share


4 answers


Make changes to your code like

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

      



and other actions like this,

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


source


Yes ... Modify getSearchData () method to include string as parameter

public String getSearchData(String query) throws Exception{

Then paste this string into the request url, replacing spaces with "+". You might want to continue with customization per line like url encoding.

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

In your XML, create a button containing the following line:



android:onClick="search"

In your ProductSearch activity add the following method and move your onCreate code into it. You also need to create an EditText in your XML for input.

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

    }
}

      

Finally, you probably want to familiarize yourself with running asynchronous tasks so that the request does not slow down your application at runtime.

0


source


Maybe I was wrong, but why don't you just pass it as a parameter to

getSearchData() => getSearchData(string query) 

      

Then you can change the line

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

      

to

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

      

0


source


Check out http://androidforums.com/developer-101/528924-arduino-android-internet-garage-door-works-but-could-use-input.html I am using Asynctask to run a get command on my local Arduino server. It adds the Arduino pin number and, as needed, the port number to the end of the url. I'm sure you can use it to help you.

0


source







All Articles