Hi guys,
I am new here; First of all I wish you all the best and hope I can learn something with you!
I am with a problem: I can not read html from a website with my Android app. I think the problem is because of Internet permissions.
HttpExample (who is launched)
Right here I tried to debugging and my textview get the exception from catch.
GetMethodEx
Manifest
Hope you guys can help me. Thank you.
Nelson.
I am new here; First of all I wish you all the best and hope I can learn something with you!
I am with a problem: I can not read html from a website with my Android app. I think the problem is because of Internet permissions.
HttpExample (who is launched)
Code:
package com.example.httpexample;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class HttpExample extends Activity {
TextView httpStuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http_example);
httpStuff = (TextView) findViewById(R.id.tvHttp);
GetMethodEx test = new GetMethodEx();
String returned;
httpStuff.setText("Debugging 1 - onCreate");
try {
returned = test.getInternetData();
httpStuff.setText(returned);
} catch (Exception e) {
httpStuff.setText("Debbuging 3 - catch");
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.http_example, menu);
return true;
}
}
GetMethodEx
Code:
package com.example.httpexample;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class GetMethodEx {
public String getInternetData() throws Exception{
BufferedReader in = null;
String data = null;
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.mybringback.com/");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
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();
}
}
}
}
}
Manifest
Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.httpexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.httpexample.HttpExample"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Hope you guys can help me. Thank you.
Nelson.