I'm trying to save a webpage to my external storage with the following method:
public void saveWebPage(String url, String fileName){
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response;
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
String resString = sb.toString();
is.close();
File file = new File(Environment.getExternalStorageDirectory().toString() + "/Android/data/com.whizzapps.stpsurniki/" + fileName + ".html");
file.createNewFile();
FileOutputStream f1 = new FileOutputStream(file, false);
PrintStream p = new PrintStream(f1);
p.print(resString);
p.close();
f1.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I call the method, the app crashes and this is the logcat:
09-09 17:26:22.304: E/AndroidRuntime(14507): FATAL EXCEPTION: main
09-09 17:26:22.304: E/AndroidRuntime(14507): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.whizzapps.stpsurniki/com.whizzapps.stpsurniki.Scheudele}: android.os.NetworkOnMainThreadException
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.os.Looper.loop(Looper.java:137)
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-09 17:26:22.304: E/AndroidRuntime(14507): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 17:26:22.304: E/AndroidRuntime(14507): at java.lang.reflect.Method.invoke(Method.java:525)
09-09 17:26:22.304: E/AndroidRuntime(14507): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-09 17:26:22.304: E/AndroidRuntime(14507): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-09 17:26:22.304: E/AndroidRuntime(14507): at dalvik.system.NativeStart.main(Native Method)
09-09 17:26:22.304: E/AndroidRuntime(14507): Caused by: android.os.NetworkOnMainThreadException
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
09-09 17:26:22.304: E/AndroidRuntime(14507): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-09 17:26:22.304: E/AndroidRuntime(14507): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-09 17:26:22.304: E/AndroidRuntime(14507): at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-09 17:26:22.304: E/AndroidRuntime(14507): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
09-09 17:26:22.304: E/AndroidRuntime(14507): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
09-09 17:26:22.304: E/AndroidRuntime(14507): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
09-09 17:26:22.304: E/AndroidRuntime(14507): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
09-09 17:26:22.304: E/AndroidRuntime(14507): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-09 17:26:22.304: E/AndroidRuntime(14507): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-09 17:26:22.304: E/AndroidRuntime(14507): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
09-09 17:26:22.304: E/AndroidRuntime(14507): at com.whizzapps.stpsurniki.Scheudele.saveWebPage(Scheudele.java:135)
09-09 17:26:22.304: E/AndroidRuntime(14507): at com.whizzapps.stpsurniki.Scheudele.getWebView(Scheudele.java:66)
09-09 17:26:22.304: E/AndroidRuntime(14507): at com.whizzapps.stpsurniki.Scheudele.onCreate(Scheudele.java:50)
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.app.Activity.performCreate(Activity.java:5133)
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-09 17:26:22.304: E/AndroidRuntime(14507): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-09 17:26:22.304: E/AndroidRuntime(14507): ... 11 more
I was told to put this in UI Thread but I don't know in which line of code the ui thread is. I simply put it next to other methods in the activity class file.
New code:
private class downloadWebPage extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response;
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
String resString = sb.toString();
is.close();
File file = new File(Environment.getExternalStorageDirectory().toString() + "/Android/data/com.whizzapps.stpsurniki/" + fileName + ".html");
file.createNewFile();
FileOutputStream f1 = new FileOutputStream(file, false);
PrintStream p = new PrintStream(f1);
p.print(resString);
p.close();
f1.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
The exception clearly indicates android.os.NetworkOnMainThreadException, which means you should always make Network call only in a seperate thread.
Solution: Create a new thread, and do Network call.
You can use AsyncTask, The AsyncTask class provides one of the simplest ways to fire off a new task from the UI thread from Android documentation
See the android link for more information.
Read the documentation about AsyncTask carefully.
The three types used by an asynchronous task are the following:
Params, the type of the parameters sent to the task upon execution.
Progress, the type of the progress units published during the background computation.
Result, the type of the result of the background computation
In your case, you should use Params to send parameter to the method (e.g url and filename)
Related
I try to create a socket client connection to a python server like this, but it throwss me an error. This is triggered via a button, which works. The server is running on a RPi, but that's not the problem since a Python client works on my PC.
try {
Socket s = new Socket("192.168.178.43", 24069);
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String advice = reader.readLine();
reader.close();
} catch (UnknownHostException e) {
System.err.println("Unknown Host.");
// System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection.");
System.err.println(e);
// System.exit(1);
}
Stacktrace:
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3823)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.Socket.startupSocket(Socket.java:567)
at java.net.Socket.tryAllAddresses(Socket.java:128)
at java.net.Socket.<init>(Socket.java:178)
at java.net.Socket.<init>(Socket.java:150)
at com.example.app.MainActivity.sendMessage(MainActivity.java:65)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3818)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
you can't do any long time process on the Main UI thread the reason for that to avoid the crash of your App if your operation failed
try to create new thread and do your work inside of it and if u want to pass any result to the UI u cant use
View.Post()
or
context.runOnUiThread()
this is an example of what you can do or you can use asynctask instead
new Thread(new Runnable() {
public void run() {
try {
Socket s = new Socket("192.168.178.43", 24069);
InputStreamReader streamReader = new InputStreamReader(s.getInputStream());
BufferedReader reader = new BufferedReader(streamReader);
String advice = reader.readLine();
reader.close();
} catch (UnknownHostException e) {
System.err.println("Unknown Host.");
// System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection.");
System.err.println(e);
// System.exit(1);
}
}
}).start();
This question already has answers here:
How can I fix 'android.os.NetworkOnMainThreadException'?
(66 answers)
Closed 9 years ago.
I have a problem.
I want to know where did the android.os.NetworkOnMainThreadException and
java.lang.reflect.InvocationTargetException Exception come from !
Please, I'm new to Android and I need your help guys!
My LogCat:
12-19 13:00:27.049: E/AndroidRuntime(1170): FATAL EXCEPTION: main
12-19 13:00:27.049: E/AndroidRuntime(1170): java.lang.IllegalStateException: Could not execute method of the activity
12-19 13:00:27.049: E/AndroidRuntime(1170): at android.view.View$1.onClick(View.java:3633)
12-19 13:00:27.049: E/AndroidRuntime(1170): at android.view.View.performClick(View.java:4240)
12-19 13:00:27.049: E/AndroidRuntime(1170): at android.view.View$PerformClick.run(View.java:17721)
12-19 13:00:27.049: E/AndroidRuntime(1170): at android.os.Handler.handleCallback(Handler.java:730)
12-19 13:00:27.049: E/AndroidRuntime(1170): at android.os.Handler.dispatchMessage(Handler.java:92)
12-19 13:00:27.049: E/AndroidRuntime(1170): at android.os.Looper.loop(Looper.java:137)
12-19 13:00:27.049: E/AndroidRuntime(1170): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-19 13:00:27.049: E/AndroidRuntime(1170): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 13:00:27.049: E/AndroidRuntime(1170): at java.lang.reflect.Method.invoke(Method.java:525)
12-19 13:00:27.049: E/AndroidRuntime(1170): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-19 13:00:27.049: E/AndroidRuntime(1170): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-19 13:00:27.049: E/AndroidRuntime(1170): at dalvik.system.NativeStart.main(Native Method)
12-19 13:00:27.049: E/AndroidRuntime(1170): Caused by: java.lang.reflect.InvocationTargetException
12-19 13:00:27.049: E/AndroidRuntime(1170): at java.lang.reflect.Method.invokeNative(Native Method)
12-19 13:00:27.049: E/AndroidRuntime(1170): at java.lang.reflect.Method.invoke(Method.java:525)
12-19 13:00:27.049: E/AndroidRuntime(1170): at android.view.View$1.onClick(View.java:3628)
12-19 13:00:27.049: E/AndroidRuntime(1170): ... 11 more
12-19 13:00:27.049: E/AndroidRuntime(1170): Caused by: android.os.NetworkOnMainThreadException
12-19 13:00:27.049: E/AndroidRuntime(1170): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
12-19 13:00:27.049: E/AndroidRuntime(1170): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
12-19 13:00:27.049: E/AndroidRuntime(1170): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-19 13:00:27.049: E/AndroidRuntime(1170): at libcore.io.IoBridge.connect(IoBridge.java:112)
12-19 13:00:27.049: E/AndroidRuntime(1170): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-19 13:00:27.049: E/AndroidRuntime(1170): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
12-19 13:00:27.049: E/AndroidRuntime(1170): at java.net.Socket.connect(Socket.java:842)
12-19 13:00:27.049: E/AndroidRuntime(1170): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
12-19 13:00:27.049: E/AndroidRuntime(1170): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
12-19 13:00:27.049: E/AndroidRuntime(1170): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-19 13:00:27.049: E/AndroidRuntime(1170): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-19 13:00:27.049: E/AndroidRuntime(1170): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
12-19 13:00:27.049: E/AndroidRuntime(1170): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-19 13:00:27.049: E/AndroidRuntime(1170): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-19 13:00:27.049: E/AndroidRuntime(1170): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-19 13:00:27.049: E/AndroidRuntime(1170): at com.ensem.sehaty.MainActivity.recupererListMed(MainActivity.java:58)
12-19 13:00:27.049: E/AndroidRuntime(1170): ... 14 more
12-19 13:05:27.366: I/Process(1170): Sending signal. PID: 1170 SIG: 9
MainActivity.java
public class MainActivity extends Activity {
Button btnRecupListMed = null;
ListView listeMed = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRecupListMed = (Button) findViewById(R.id.btnListMed);
listeMed = (ListView) findViewById(R.id.listMed);
}
public void recupererListMed(View v){
BufferedReader br = null;
StringBuffer sb = new StringBuffer("");
try {
HttpClient client = new DefaultHttpClient();
HttpProtocolParams.setUseExpectContinue(client.getParams(), false);
HttpGet get = new HttpGet();
URI uri = new URI("http://105.153.20.252");
get.setURI(uri);
HttpResponse reponse = client.execute(get);
InputStream is = reponse.getEntity().getContent();
br = new BufferedReader(new InputStreamReader(is));
String str = br.readLine();
while(str != null){
sb.append(str);
sb.append("\n");
str = br.readLine();
}
} catch (URISyntaxException e) {
e.printStackTrace();
System.out.println("Erreur 1");
} catch (ClientProtocolException e) {
e.printStackTrace();
System.out.println("Erreur 2");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Erreur 3");
}
try {
//ArrayList<HashMap<String, String>> medecins = new ArrayList<HashMap<String, String>>();
JSONArray js = new JSONArray(sb.toString());
List<String> listM = new ArrayList<String>();
for(int i=0; i<js.length(); i++){
JSONObject jsObj = js.getJSONObject(i);
String nom = jsObj.getString("NOMMED");
listM.add(nom);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, R.id.listMed, listM);
listeMed.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
System.out.println("Erreur 4");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<TextView
android:text="#string/lbl_sehaty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/btnListMed"
android:text="#string/lbl_btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:onClick="recupererListMed"
/>
<ListView
android:id="#+id/listMed"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="60dp">
</ListView>
And my Manifest contains the following permission
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Thanks.
This error comes when you try to make a server call on UI thread.
You should create an AsyncTask and put your server call in it.
And on click of button call execute on the object of asynctask.
Ex:
public class ServiceTask extends AsyncTask<String, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected String doInBackground(String... params) {
BufferedReader br = null;
StringBuffer sb = new StringBuffer("");
try {
HttpClient client = new DefaultHttpClient();
HttpProtocolParams.setUseExpectContinue(client.getParams(), false);
HttpGet get = new HttpGet();
URI uri = new URI("http://105.153.20.252");
get.setURI(uri);
HttpResponse reponse = client.execute(get);
InputStream is = reponse.getEntity().getContent();
br = new BufferedReader(new InputStreamReader(is));
String str = br.readLine();
while(str != null){
sb.append(str);
sb.append("\n");
str = br.readLine();
}
} catch (URISyntaxException e) {
e.printStackTrace();
System.out.println("Erreur 1");
} catch (ClientProtocolException e) {
e.printStackTrace();
System.out.println("Erreur 2");
} catch (IOException e) {
e.printStackTrace();
System.out.println("Erreur 3");
}
return sb;
}
#Override
protected void onPostExecute(String result) {
try {
//ArrayList<HashMap<String, String>> medecins = new ArrayList<HashMap<String, String>>();
JSONArray js = new JSONArray(result.toString());
List<String> listM = new ArrayList<String>();
for(int i=0; i<js.length(); i++){
JSONObject jsObj = js.getJSONObject(i);
String nom = jsObj.getString("NOMMED");
listM.add(nom);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, R.id.listMed, listM);
listeMed.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
System.out.println("Erreur 4");
}
}
}
And inside your onClick event:
public void recupererListMed(View v){
new ServiceTask().execute();
}
This will execute doInBackground method on a different thread and once result is fetched onPostExecute is called on UI thread.
I'm trying to create a android app.
I use InputStream inputStream=getAssets().open("book.txt"); to read the book.txt ;
I just want get the MappedByteBuffer object from InputStream ;
who konws?
========= ===========
I use FileInputStream fis = (FileInputStream) inputStream; cause an error!
here is the error log:
10-27 10:45:55.830: D/dalvikvm(9243): GC_EXTERNAL_ALLOC freed 22K, 53% free 2577K/5379K, external 1916K/2428K, paused 62ms
10-27 10:45:55.940: D/dalvikvm(9243): GC_EXTERNAL_ALLOC freed 2K, 53% free 2577K/5379K, external 2516K/2516K, paused 69ms
10-27 10:45:56.230: I/fileName(9243): sahala.txt
10-27 10:45:56.240: D/szipinf(9243): Initializing inflate state
10-27 10:45:56.240: D/AndroidRuntime(9243): Shutting down VM
10-27 10:45:56.240: W/dalvikvm(9243): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-27 10:45:56.260: E/AndroidRuntime(9243): FATAL EXCEPTION: main
10-27 10:45:56.260: E/AndroidRuntime(9243): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.silenceper.bookdemo/com.silenceper.book.shl.activity.ReadBookActivity}: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream
10-27 10:45:56.260: E/AndroidRuntime(9243): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-27 10:45:56.260: E/AndroidRuntime(9243): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-27 10:45:56.260: E/AndroidRuntime(9243): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-27 10:45:56.260: E/AndroidRuntime(9243): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-27 10:45:56.260: E/AndroidRuntime(9243): at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 10:45:56.260: E/AndroidRuntime(9243): at android.os.Looper.loop(Looper.java:123)
10-27 10:45:56.260: E/AndroidRuntime(9243): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-27 10:45:56.260: E/AndroidRuntime(9243): at java.lang.reflect.Method.invokeNative(Native Method)
10-27 10:45:56.260: E/AndroidRuntime(9243): at java.lang.reflect.Method.invoke(Method.java:507)
10-27 10:45:56.260: E/AndroidRuntime(9243): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-27 10:45:56.260: E/AndroidRuntime(9243): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-27 10:45:56.260: E/AndroidRuntime(9243): at dalvik.system.NativeStart.main(Native Method)
10-27 10:45:56.260: E/AndroidRuntime(9243): Caused by: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream
10-27 10:45:56.260: E/AndroidRuntime(9243): at com.silenceper.book.shl.utils.BookPageFactory.openBookFromInputStream(BookPageFactory.java:83)
10-27 10:45:56.260: E/AndroidRuntime(9243): at com.silenceper.book.shl.activity.ReadBookActivity.initBookData(ReadBookActivity.java:71)
10-27 10:45:56.260: E/AndroidRuntime(9243): at com.silenceper.book.shl.activity.ReadBookActivity.onCreate(ReadBookActivity.java:58)
10-27 10:45:56.260: E/AndroidRuntime(9243): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-27 10:45:56.260: E/AndroidRuntime(9243): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-27 10:45:56.260: E/AndroidRuntime(9243): ... 11 more
I had the similar need of doing this. Here is what I did:
1) Open resource file with asset manager and write a regular file to /data/data/com.yourpackage/files directory. Sample code:
private void prepareModelFile() throws IOException {
AssetManager assetManager = getAssets();
String configDir = getFilesDir().getAbsolutePath();
InputStream stream = assetManager.open("mytestfile_in_assets.data");
mTFLiteModelFile = configDir +"/mytestfilename.data";
OutputStream output = new BufferedOutputStream(new FileOutputStream(mTFLiteModelFile));
copyFile(stream, output);
}
public static void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();;
out.close();
}
2) Now you have the file copied out of assets, you can use the following way to create a MappedByteBuffer. Example:
FileInputStream inputStream = new FileInputStream(mTFLiteModelFile);
FileChannel fileChannel = inputStream.getChannel();
MappedByteBuffer myMappedBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
Hope this helps.
David
Updated
To do this you need to first copy your asset to a file and then apply the technique below to a file. If your asset is more than 1MB you will require to split it - Load files bigger than 1M from assets folder
You can't do this with an InputStream. You should instead get a FileInputStream for your book.txt or cast your inputStream and then get a FileChannel from it:
try {
FileInputStream fis = (FileInputStream) inputStream;
FileChannel channel = fis.getChannel();
ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
byte[] bytes = new byte[102];
buffer.get(bytes);
System.out.println(new String(bytes));
} catch (IOException e) {
e.printStackTrace();
}
You can't do that, for many reasons, most of which imply that the question doesn't make sense.
I have 2 devices. Droid x3 w/ Gingerbread and a Nexus 7 w/ Jellybean 4.2.2.
I compile and run this code on my Droid x3 and it runs perfectly.
Yet the exact same code I run on the Nexus 7 and it breaks.
Code and Stack trace included.
Thanks for the help!
package com.jacobschellenbergflickrsearch;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
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 DataDownloader {
String apiKey = "bc370c6386192bf6e2f950cdfddfda48";
//String url = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=bc370c6386192bf6e2f950cdfddfda48&nojsoncallback=1&text=monkey&format=json&per_page=10";
public String buildUrl(String searchMethod, String searchQuery, int perpage){
String url = String.format("http://api.flickr.com/services/rest/?method=%s&api_key=%s&nojsoncallback=1&text=%s&format=json&per_page=%s", searchMethod, this.apiKey, searchQuery, perpage);
String result = null;
try{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse response = client.execute(request);
InputStream ips = response.getEntity().getContent();
BufferedReader buf = new BufferedReader(new InputStreamReader(ips,"UTF-8"));
StringBuilder sb = new StringBuilder();
String s;
while(true)
{
s = buf.readLine();
if(s==null || s.length()==0){
break;
}
sb.append(s);
}
buf.close();
ips.close();
result = sb.toString();
}
catch (IllegalStateException e) {
e.printStackTrace();
result = "IllegalStateException";
}
catch (IOException e) {
e.printStackTrace();
result = "IOException";
} catch (URISyntaxException e) {
e.printStackTrace();
result = "URISyntaxException";
}
return "Query: " + url + " : Result: " + result;
}
}
05-01 13:47:04.742: D/AndroidRuntime(4526): Shutting down VM 05-01
13:47:04.742: W/dalvikvm(4526): threadid=1: thread exiting with
uncaught exception (group=0x41377930) 05-01 13:47:04.752:
E/AndroidRuntime(4526): FATAL EXCEPTION: main 05-01 13:47:04.752:
E/AndroidRuntime(4526): java.lang.IllegalStateException: Could not
execute method of the activity 05-01 13:47:04.752:
E/AndroidRuntime(4526): at
android.view.View$1.onClick(View.java:3599) 05-01 13:47:04.752:
E/AndroidRuntime(4526): at
android.view.View.performClick(View.java:4204) 05-01 13:47:04.752:
E/AndroidRuntime(4526): at
android.view.View$PerformClick.run(View.java:17355) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
android.os.Handler.handleCallback(Handler.java:725) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
android.os.Handler.dispatchMessage(Handler.java:92) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
android.os.Looper.loop(Looper.java:137) 05-01 13:47:04.752:
E/AndroidRuntime(4526): at
android.app.ActivityThread.main(ActivityThread.java:5041) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
java.lang.reflect.Method.invokeNative(Native Method) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
java.lang.reflect.Method.invoke(Method.java:511) 05-01 13:47:04.752:
E/AndroidRuntime(4526): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
dalvik.system.NativeStart.main(Native Method) 05-01 13:47:04.752:
E/AndroidRuntime(4526): Caused by:
java.lang.reflect.InvocationTargetException 05-01 13:47:04.752:
E/AndroidRuntime(4526): at
java.lang.reflect.Method.invokeNative(Native Method) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
java.lang.reflect.Method.invoke(Method.java:511) 05-01 13:47:04.752:
E/AndroidRuntime(4526): at
android.view.View$1.onClick(View.java:3594) 05-01 13:47:04.752:
E/AndroidRuntime(4526): ... 11 more 05-01 13:47:04.752:
E/AndroidRuntime(4526): Caused by:
android.os.NetworkOnMainThreadException 05-01 13:47:04.752:
E/AndroidRuntime(4526): at
android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
java.net.InetAddress.lookupHostByName(InetAddress.java:385) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
java.net.InetAddress.getAllByNameImpl(InetAddress.java:236) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
java.net.InetAddress.getAllByName(InetAddress.java:214) 05-01
13:47:04.752: E/AndroidRuntime(4526): at
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
com.jacobschellenbergflickrsearch.DataDownloader.buildUrl(DataDownloader.java:31)
05-01 13:47:04.752: E/AndroidRuntime(4526): at
com.jacobschellenbergflickrsearch.MainActivity.searchImage(MainActivity.java:47)
05-01 13:47:04.752: E/AndroidRuntime(4526): ... 14 more 05-01
13:47:04.802: D/dalvikvm(4526): GC_CONCURRENT freed 210K, 5% free
7474K/7812K, paused 5ms+2ms, total 46ms
Caused by: android.os.NetworkOnMainThreadException
You are doing network I/O on the main application thread. This is an exceptionally bad idea on any version of Android, and it raises an actual exception on Android 4.0+.
Please perform your network I/O on a background thread, such as by using an AsyncTask.
HttpResponse response = client.execute(request);
That is your problem. Do not call network actions on the UI thread. You should not do that ever. Since ICS it is forbidden.
Use a AsyncTask and call the client.execute() inside doInBackground(). That will fix your problem.
As the error says you are doing a newtwork call on the main thread...you cannot do that, it must be in a seperate thread
I've looked at countless projects and tutorials and haven't been able to figure out why my app is crashing as soon as it is ran. It seems to run fine if i comment out the first line in the try catch "JSONObject dataObject = json.getJSONObject("data");" The JSON that i'm trying to parse is formatted as such:
{"recordcount":3,
"columnlist":"department,emailaddress,firstname,has_photos,job_full_title,job_title,kid,lastname,location,middlename,no_show_photo,phone,school",
"data":
{"department":["Public Safety","Information Technology","Information Technology"],
"emailaddress":["todd.mongeon#wne.edu","amutti#wne.edu","fatemeh.shams#wne.edu"],
"firstname":["Todd","Tony","Toffee"],
"has_photos":["Y","Y","N"],
"job_full_title":["Officer","Director, Administrative Information Systems","Sr.Technical Programmer/Analyst and Integ Specialist"],
"job_title":["Officer","Director, Administrative Information Systems","Sr.Technical Programmer/Analyst and Integ Specialist"],
"kid":[286899,2497,297411],
"lastname":["Mongeon","Mutti","Shams"],
"location":["PS","12 V","12 V"],
"middlename":["A.","M.",""],
"no_show_photo":["","",""],
"phone":["413-782-1207","413-782-1212","413-796-2398"],
"school":["Public Safety Group","Information Technology Group","Information Technology Group"]}}
My program looks like this (please excuse formatting / things that don't need to be there... it needs to be cleaned up from a lot of the things i've tried to get this to work..)
public class Directory_finalActivity extends ListActivity {
public static String url = "http://www1.wne.edu/webservices/directorydemo.cfc?wsdl&method=getEmployees&limit=25&firstname=to&outputtype=JSON";
// JSON array names
private static final String TAG_FIRST = "firstname";
private static final String TAG_LAST = "lastname";
private static final String TAG_SCHOOL = "school";
private static final String TAG_DEPARTMENT = "department";
private static final String TAG_EMAIL = "emailaddress";
private static final String TAG_PHONE = "phone";
private static final String TAG_JOB = "job_title";
private static final String TAG_LOCATION = "location";
//create JSON array
JSONArray firstNames = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
String[] firstNames = null;
try {
// Getting the data object
JSONObject dataObject = json.getJSONObject("data"); //This is the line that seems to be causing the error.
int numResults = json.getInt("recordcount");
/**
JSONArray firstNameArray = dataObject.getJSONArray("firstname");
JSONArray lastNameArray = dataObject.getJSONArray("lastname");
JSONArray schoolArray = dataObject.getJSONArray("school");
JSONArray departmentArray = dataObject.getJSONArray("department");
JSONArray eMailArray = dataObject.getJSONArray("emailaddress");
JSONArray phoneNumberArray = dataObject.getJSONArray("phone");
JSONArray jobTitleArray = dataObject.getJSONArray("job_title");
JSONArray locationArray = dataObject.getJSONArray("location");
//Loop through index of array(s)
for(int i = 0; i <= numResults; i++)
{
//Store value of JSONArray at index "i" into string variables.
String firstName = firstNameArray.getString(i);
String eMail = eMailArray.getString(i);
String phone = phoneNumberArray.getString(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_FIRST, firstName);
map.put(TAG_EMAIL, eMail);
map.put(TAG_PHONE, phone);
//add the hashmap to array list.
contactList.add(map);
}*/
}
catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
//ListAdapter adapter = new SimpleAdapter(this, contactList,
// R.layout.list_item,
// new String[] { TAG_FIRST, TAG_EMAIL, TAG_PHONE }, new int[] {
// R.id.name, R.id.email, R.id.mobile });
//setListAdapter(adapter);
}
}
My JSONParser class
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
I'm probably doing something incredibly stupid here, i'm new to this so give me a break please haha. I appreciate any info that you guys may have for me.
For curiosity sake the end goal is to search the database (first name, last name, school, and department) and put the results into a list view, when clicked on a window will pop up displaying other relevant information with a couple other features.
It seems every example i see the JSON output they receive from the url is formatted much differently, so i'm having trouble getting this done even with the amount of examples available (sorry for re-post).
Once again thank you for any assistance.
EDIT 1:
LOGCAT:
04-17 00:45:35.828: W/System.err(305): java.net.UnknownHostException: api.androidhive.info
04-17 00:45:35.828: W/System.err(305): at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
04-17 00:45:35.838: W/System.err(305): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
04-17 00:45:35.838: W/System.err(305): at java.net.InetAddress.getAllByName(InetAddress.java:242)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-17 00:45:35.838: W/System.err(305): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-17 00:45:35.838: W/System.err(305): at directory.plus.JSONParser.getJSONFromUrl(JSONParser.java:39)
04-17 00:45:35.838: W/System.err(305): at directory.plus.Directory_finalActivity.onCreate(Directory_finalActivity.java:53)
04-17 00:45:35.849: W/System.err(305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-17 00:45:35.849: W/System.err(305): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 00:45:35.849: W/System.err(305): at android.os.Looper.loop(Looper.java:123)
04-17 00:45:35.849: W/System.err(305): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-17 00:45:35.849: W/System.err(305): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 00:45:35.849: W/System.err(305): at java.lang.reflect.Method.invoke(Method.java:521)
04-17 00:45:35.858: W/System.err(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-17 00:45:35.858: W/System.err(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-17 00:45:35.858: W/System.err(305): at dalvik.system.NativeStart.main(Native Method)
04-17 00:45:35.858: E/Buffer Error(305): Error converting result java.lang.NullPointerException
04-17 00:45:35.868: E/JSON Parser(305): Error parsing data org.json.JSONException: End of input at character 0 of
04-17 00:45:35.868: D/AndroidRuntime(305): Shutting down VM
04-17 00:45:35.868: W/dalvikvm(305): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-17 00:45:35.888: E/AndroidRuntime(305): FATAL EXCEPTION: main
04-17 00:45:35.888: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity ComponentInfo{directory.plus/directory.plus.Directory_finalActivity}: java.lang.NullPointerException
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.os.Looper.loop(Looper.java:123)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-17 00:45:35.888: E/AndroidRuntime(305): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 00:45:35.888: E/AndroidRuntime(305): at java.lang.reflect.Method.invoke(Method.java:521)
04-17 00:45:35.888: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-17 00:45:35.888: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-17 00:45:35.888: E/AndroidRuntime(305): at dalvik.system.NativeStart.main(Native Method)
04-17 00:45:35.888: E/AndroidRuntime(305): Caused by: java.lang.NullPointerException
04-17 00:45:35.888: E/AndroidRuntime(305): at directory.plus.Directory_finalActivity.onCreate(Directory_finalActivity.java:59)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-17 00:45:35.888: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
EDIT 2:
NEW LOGCAT OUTPUT:
04-17 01:26:12.899: E/JSON Parser(548): Error parsing data in JSONParser org.json.JSONException: Value <wddxPacket of type java.lang.String cannot be converted to JSONObject
04-17 01:26:12.899: D/AndroidRuntime(548): Shutting down VM
04-17 01:26:12.899: W/dalvikvm(548): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-17 01:26:12.919: E/AndroidRuntime(548): FATAL EXCEPTION: main
04-17 01:26:12.919: E/AndroidRuntime(548): java.lang.RuntimeException: Unable to start activity ComponentInfo{directory.plus/directory.plus.Directory_finalActivity}: java.lang.NullPointerException
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.os.Handler.dispatchMessage(Handler.java:99)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.os.Looper.loop(Looper.java:123)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-17 01:26:12.919: E/AndroidRuntime(548): at java.lang.reflect.Method.invokeNative(Native Method)
04-17 01:26:12.919: E/AndroidRuntime(548): at java.lang.reflect.Method.invoke(Method.java:521)
04-17 01:26:12.919: E/AndroidRuntime(548): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-17 01:26:12.919: E/AndroidRuntime(548): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-17 01:26:12.919: E/AndroidRuntime(548): at dalvik.system.NativeStart.main(Native Method)
04-17 01:26:12.919: E/AndroidRuntime(548): Caused by: java.lang.NullPointerException
04-17 01:26:12.919: E/AndroidRuntime(548): at directory.plus.Directory_finalActivity.onCreate(Directory_finalActivity.java:59)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-17 01:26:12.919: E/AndroidRuntime(548): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-17 01:26:12.919: E/AndroidRuntime(548): ... 11 more
04-17 01:26:33.279: I/Process(548): Sending signal. PID: 548 SIG: 9
EDIT FINAL: Problem solved:
Needed to take a sub string of the result returned from the html and pass that into a json object
java.net.UnknownHostException: api.androidhive.info
It looks like your app can't access the network. Have you added:
<uses-permission
android:name="android.permission.INTERNET" />
to your code AndroidManifest.xml?