Reputation: 1130
I'm extremely new to android development, this is my first day. I'm having problems understanding what is wrong with my code because I cannot comprehend the error messages that I'm getting. I'm just looking for somoene to shed some light on what is going on.
I'm trying to send an http request with a url, and I should get a response with a body full of text, which I try to extract this body onto a String, and send it through the Android text to speech api while also displaying a webview of said page.
Here it is:
package com.example.webview;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.EditText;
import android.widget.Toast;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
public class MainActivity extends Activity implements TextToSpeech.OnInitListener{
private WebView mWebview ;
EditText txtText;
TextToSpeech tts = new TextToSpeech(this, (OnInitListener) this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enables javascript
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/");
setContentView(mWebview);
try {
speakOut(getText("http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/"));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void speakOut(String text) {
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
public String getText(String webPage) throws ParseException, IOException{
HttpResponse response = null;
try {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("http://supersecretwebsite.net:8080/" + "http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/"));
response = client.execute(request);
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String responseBody = "No text found on webpage.";
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode) {
case 200:
HttpEntity entity = response.getEntity();
if(entity != null) {
responseBody = EntityUtils.toString(entity);
}
}
return responseBody;
}
@Override
public void onInit(int status) {
// TODO Auto-generated method stub
}
}
Errors:
Well there are quite a lot. It starts with
(Application)com.example.webview (Tag)Trace (Text)Error opening trace file: No such file or directory (2)
Then everything seems to just shutdown. The program doesn't even run. When I had a basic webview and nothing else it worked perfectly so the added Httprequest or text to speech is probably the cause of these errors, but I honestly can't tell how, what, or why.
Upvotes: 0
Views: 258
Reputation: 1009
You need to have any networking on another thread. Use the async task to achieve this. Then in the OnPostExecute() method you can display the result.
You also need to declare internet permission in the manifest.
Upvotes: 1