Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6836969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:25:12+00:00 2026-05-26T23:25:12+00:00

I´m trying to use the VoiceRecognition sample for sending voice recognized text to an

  • 0

I´m trying to use the VoiceRecognition sample for sending voice recognized text to an internet bot. All I need is to send info to an URL and get the html code.
I found a problem trying to start httpclient inside onActivityResult, and I don´t know how to solve it.
This is the code:

public class BkVRMobileActivity extends Activity
{

    private static final int REQUEST_CODE = 1234;
    private ListView wordsList;
    private TextView texto1;
    private TextView texto2;

    /**
     * Called with the activity is first created.
     */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.voice_recog);

    ImageButton speakButton = (ImageButton) findViewById(R.id.speakButton);
    wordsList = (ListView) findViewById(R.id.list);
    texto1 = (TextView) findViewById(R.id.textView1);
    texto2 = (TextView) findViewById(R.id.textView2);

    // Disable button if no recognition service is present
    PackageManager pm = getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(
            new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
    if (activities.size() == 0)
    {
        speakButton.setEnabled(false);
    }

}

/**
 * Handle the action of the button being clicked
 */
public void speakButtonClicked(View v)
{
    startVoiceRecognitionActivity();
    System.out.println("--al lio --");
   }

/**
 * Fire an intent to start the voice recognition activity.
 */
private void startVoiceRecognitionActivity()
{
    Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
    intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
            RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
    intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Reconocimiento de Voz activado...");
    intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
    startActivityForResult(intent, REQUEST_CODE);
}

/**
 * Handle the results from the voice recognition activity.
 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK)

    {
        ArrayList<String> matches = data.getStringArrayListExtra(
        RecognizerIntent.EXTRA_RESULTS);
        //wordsList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,matches));
        texto1.setText(matches.get(0));

        String laurl = "http://www.pandorabots.com/pandora/talk-xml?input=" + matches.get(0) + "&botid=9cd68de58e342fb8";

        //open the url using httpclient for reading html source
        getXML(laurl);

        //System.out.println(laurl);



      }

   //super.onActivityResult(requestCode, resultCode, data);
}

public String getXML(String url){
    String log = null;
    try {

        HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
        HttpGet httpget = new HttpGet(url); // Set the action you want to do
        HttpResponse response = httpclient.execute(httpget); // Executeit
        HttpEntity entity = response.getEntity(); 
        InputStream is = entity.getContent(); // Create an InputStream with the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) // Read line by line
            sb.append(line + "\n");

        String resString = sb.toString(); // Result is here

        is.close(); // Close the stream
        texto2.setText(resString);


    } catch (UnsupportedEncodingException e) {
        log = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
    } catch (MalformedURLException e) {
        log = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
    } catch (IOException e) {
        log = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
    }

    return log;

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:25:12+00:00Added an answer on May 26, 2026 at 11:25 pm
    android.os.NetworkOnMainThreadException
    

    getXML needs to run on a separate thread as it does network requests (which can take a long time) which, on the UI thread will cause ANRs

    Something like:

    public String getXML(String url){
        new AsyncTask<String, Void, String>() {
                    private String doInBackgroundThread(String... params)
                    {
                        try {
    
                            HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
                            HttpGet httpget = new HttpGet(params[0]); // Set the action you want to do
                            HttpResponse response = httpclient.execute(httpget); // Executeit
                            HttpEntity entity = response.getEntity(); 
                            InputStream is = entity.getContent(); // Create an InputStream with the response
                            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                            StringBuilder sb = new StringBuilder();
                            String line = null;
                            while ((line = reader.readLine()) != null) // Read line by line
                                sb.append(line + "\n");
    
                            String resString = sb.toString(); // Result is here
    
                            is.close(); // Close the stream
                            return resString;
                        } catch (UnsupportedEncodingException e) {
                        } catch (MalformedURLException e) {
                        } catch (IOException e) {
                        }
                    }
    
                    @Override
                    protected void onPostExecute(String result)
                    {
                        texto2.setText(result);
                    }
                }.execute(url);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to use a guid as a resource id in a rest url but
Trying to use GnuPG with Delphi (Win32). I need to sign some file with
I have been trying use the edit_post_link() function to contain an image. All of
I'm trying use regular expression to convert special characters in an url. Here's my
I'm still very new to Java. I'm trying to use CMU's Sphinx4 voice recognition.
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use a Java Uploader in a ROR app (for its ease
Trying to use an excpetion class which could provide location reference for XML parsing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.