I have an application that needs to connect to the Internet to perform some actions but when no Internet available it will crash. I read that I need to use try catch bracket in case no Internet. I tried to use it as you can see in AsyncTask but it doesn't work. I don't know why. The app crashes. How to deal with try catch where to put it in my code?
One more thing what about if the app lost Internet connection while process is going on. How supposed I to deal with this thing so my app doesn't crash. Thank you very much.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
lv = (ListView) findViewById(R.id.mybookslistview);
new connectToServer().execute();
}
class connectToServer extends AsyncTask<Void, Void, Void>{
CustomListViewAdapter adapter;
HttpResponse response;
#Override
protected Void doInBackground(Void... params) {
ids_list.clear();
names_list.clear();
writers_list.clear();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(link);
ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair(word, connectionPurpose));
try {
post.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
response = client.execute(post);
BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
sb = new StringBuffer();
String tempVar = "";
while((tempVar = br.readLine()) != null){
sb.append(tempVar);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Get data from stringbuffer and put it in array list
if(!sb.toString().trim().contentEquals("null")){
content_array = sb.toString().split(",");
for(int s = 0; s < content_array.length; s++){
if(content_array[s].contains("-")){
String temp[] = content_array[s].split("-");
ids_list.add(temp[0].trim());
names_list.add(temp[1].trim());
writers_list.add(temp[2].trim());
}
}
}
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(!mWifi.isConnected()){
adb = new AlertDialog.Builder(Home.this);
adb.setMessage("لا يوجد إنترنت. قم بتفعيل الإنترنت ثم حاول مرة أخرى.");
adb.setPositiveButton("حاول مجددا", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
new connectToServer().execute();
}
});
adb.setNegativeButton("إغلاق التطبيق", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
//It shows dialog if no connection
adb.create().show();
}else{
list = new ArrayList<Home.ListViewItem>();
for(x = 0; x < ids_list.size(); x++){
list.add(new ListViewItem(){{bookName = names_list.get(x); writerName = writers_list.get(x);}});
}
adapter = new CustomListViewAdapter(Home.this, list);
lv.setAdapter(adapter);
if(sb.toString().trim().contentEquals("null")){
Toast.makeText(Home.this, "لا توجد نتائج.", Toast.LENGTH_LONG).show();
}
}
This is my logcat:
java.net.UnknownHostException: Unable to resolve host "globalmall.ca": No address associated with hostname
at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at readit.Mansour.inc.Home$connectToServer.doInBackground(Home.java:106)
at readit.Mansour.inc.Home$connectToServer.doInBackground(Home.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
at libcore.io.Posix.getaddrinfo(Native Method)
at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
at java.net.InetAddress.lookupHostByName(InetAddress.java:405)
... 18 more
Caused by: libcore.io.ErrnoException: getaddrinfo failed: ENETUNREACH (Network is unreachable)
... 21 more
threadid=13: thread exiting with uncaught exception (group=0x40e582a0)
You can either create method or some class may be where you can instantiate method as static.
Here is a method named isConnectedToInternet() which checks whether internet is connected or not. Return boolean on the basis of connection back to the calling function.
Snippet:
public boolean isConnectedToInternet(){
ConnectivityManager connectivity = (ConnectivityManager)getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
You can decide on the basis of return value of isConnectedToInternet() whether to execute AysncTask or Throw some pop up. Here i've been added user to brought in his Data Settings.
Something like these:
if(isConnectedToInternet())
{
// Run AsyncTask
}
else
{
// Here I've been added intent to open up data settings
Intent intent=new Intent(Settings.ACTION_MAIN);
ComponentName cName = new ComponentName("com.android.phone","com.android.phone.NetworkSetting");
intent.setComponent(cName);
}
As you mentioned what if you looses connection in between. You can check the status code as per the reponse of httpclient and pop up relevant information to user.
You can integrate these snippet under AysncTask.
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = null;
response = httpclient.execute(httpget);
int code = response.getStatusLine().getStatusCode();
public class CheckNetClass {
public static Boolean checknetwork(Context mContext) {
NetworkInfo info = ((ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option, you can change it if you want to
// disable internet while roaming, just return false
return true;
}
return true;
}
}
Use this class to check internet availability like:
if (CheckNetClass.checknetwork(getApplicationContext()))
{
new GetCounterTask().execute();
}
else
{
Toast.makeText(getApplicationContext(),"Sorry,no internet connectivty",1).show();
}
Hope this helps..
Interesting. You have in your stack trace these lines:
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
readit.Mansour.inc.Home$connectToServer.doInBackground(Home.java:106)
Which mean that the offending line is
response = client.execute(post);
Which is different from the line that you mentioned. Verify the stack trace & the line that it mentions. Also, see that if you fix it by catching Exception. If you don't, then you have a bigger problem, because UnknownHostException is a subclass of IOException, which you already catch.
Related
I'm having trouble with the case where a SocketChannel.connect times out. The code bellow works fine in my old Android 2.3 phone and my old Android 3.2 tablet but crashes in Android 4.2.2:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView) findViewById(R.id.tvResult);
btnTest = (Button) this.findViewById(R.id.btnTest);
btnTest.setOnClickListener(doTest);
}
protected View.OnClickListener doTest = new View.OnClickListener() {
#Override
public void onClick(View v) {
new AsyncTest().execute();
}
};
private class AsyncTest extends AsyncTask {
private String result = "";
#Override
protected Void doInBackground(Void... params) {
InetSocketAddress addr = new InetSocketAddress("192.168.0.13", 4040);
boolean bConnect = false;
try {
SocketChannel clntChan = SocketChannel.open();
bConnect = clntChan.connect(addr);
if (bConnect) {
clntChan.close();
result = "Connected";
} else {
result = "Did not connected";
}
} catch (Exception e) {
Log.e(TAG, "Exception " + e.getMessage());
result = "Exception " + e.getMessage();
}
return null;
}
#Override
protected void onPostExecute(Void ret) {
tvResult.setText (result);
}
}
I am testing the case where there is no one is listening on port 4040 at 192.168.0.13. In Android 2.3 the try/catch will catch a "Connection timeout" exception (as expected). In Android 4.x I get the following:
FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
at java.util.concurrent.FutureTask.run(FutureTask.java:239)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:838)
Caused by: java.lang.AssertionError: java.net.SocketTimeoutException: failed to connect to /192.168.0.13 (port 4040) after 90000ms
at libcore.io.IoBridge.connect(IoBridge.java:102)
at java.nio.SocketChannelImpl.connect(SocketChannelImpl.java:177)
at br.com.dqsoft.testeconnect.MainActivity$AsyncTest.doInBackground(MainActivity.java:63)
at br.com.dqsoft.testeconnect.MainActivity$AsyncTest.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
... 4 more
Caused by: java.net.SocketTimeoutException: failed to connect to /192.168.0.13 (port 4040) after 90000ms
at libcore.io.IoBridge.connectErrno(IoBridge.java:176)
at libcore.io.IoBridge.connect(IoBridge.java:112)
at libcore.io.IoBridge.connect(IoBridge.java:100)
... 9 more
A could not find a way to catch this exception, the app is closed by the OS. While a timeout is not a normal condition, I should be able to detect that and present an error message to the user instead of just crashing.
The case where the connect succeeds works fine in all the devices.
Edit: The initial paragraph erroneously mentioned Android 2.4 instead of 4.2.2.
Edit: Simplified the code as suggest by Jarred and added additional information.
Update: Tested on a Android 4.4.4 device and it worked. I will close this as a bug of Android 4.2.2 (maybe on this specific device).
Remove most of this:
You are starting a Thread from a Thread form another Thread. Just start a single Thread from your Main Thread, using your AsyncTask.
protected View.OnClickListener doTest = new View.OnClickListener() {
#Override
public void onClick(View v) {
new Thread(new threadTest()).start();
}
};
private class threadTest implements Runnable {
#Override
public void run() {
try {
runOnUiThread(new Runnable() {
#Override
public void run() {
new AsyncTest().execute();
}
});
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}
Change it to:
protected View.OnClickListener doTest = new View.OnClickListener() {
#Override
public void onClick(View v) {
new AsyncTest().execute();
}
};
For the Exception and Timeout:
IOException is the parent class of SocketTimeoutException, so your AsyncTask code should work. When in doubt, you can catch everything by using Exception.
Other things to check:
Can you ping 192.168.0.13?
How do you know that port is open? Try telnet or netcat.
Do you have a service running and configured correctly on the port?
Do you have the correct Android Permissions?
I am uploading an image to server. There is some error in Asynctask while I do .addpart with the fileupload. I am new to this so please help me.
The variable filepath has some issues in Asynctask according to me.
My manifest.xml file has the required permissions.
public class UploadActivity extends Activity{
private String filePath = null;
private Button btnUpload;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
btnUpload = (Button) findViewById(R.id.btnUpload);
// Receiving the data from previous activity
Intent i = getIntent();
// image or video path that is captured in previous activity
filePath = i.getStringExtra("filePath");
btnUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// uploading the file to server
new Upload().execute();
}
});
}
/**
* Uploading the file to server
* */
private class Upload extends AsyncTask<Void, Integer, String> {
#Override
protected String doInBackground(Void... params) {
return uploadFile();
}
#SuppressWarnings("deprecation")
private String uploadFile() {
String responseString = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.102/fileUpload.php");
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
File sourceFile = new File(filePath);
// Adding file data to http body
entity.addPart("image", new FileBody(sourceFile));
totalSize = entity.getContentLength();
httppost.setEntity(entity);
// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
}
Error log:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:278)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:864)
Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
at com.example.apurwa.camera.UploadActivity$Upload.uploadFile(UploadActivity.java:104)
at com.example.apurwa.camera.UploadActivity$Upload.doInBackground(UploadActivity.java:91)
at com.example.apurwa.camera.UploadActivity$Upload.doInBackground(UploadActivity.java:83)
at android.os.AsyncTask$2.call(AsyncTask.java:264)
Your problem most probably is that you are not importing org.apache.http library correctly. That's why your crash giving you java.lang.NoClassDefFoundError
What you have to do is to make sure that you are importing the library correctly. So if you are using Eclipse, then download the JAR file form this link
If you are using Android Studio, then make sure of your dependencies in the Gradle file from this link
Also have a look on this question, it discusses similar issue to yours
Add library containing org.apache.http.entity.ContentType to build path.
The Bluetooth device I am trying to connect has always the same pincode. This should make it possible to pair the device by setting the pin programmatically.
After trying to search how this could be done, I ended up with the code below:
BluetoothDevice device = getDevice();
//To avoid the popup notification:
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device, true);
byte[] pin = ByteBuffer.allocate(4).putInt(1234).array();
//int pinn = 1234;
//Entering pin programmatically:
Method ms = device.getClass().getMethod("setPin", byte[].class);
//Method ms = device.getClass().getMethod("setPasskey", int.class);
ms.invoke(device, pin);
//Bonding the device:
Method mm = device.getClass().getMethod("createBond", (Class[]) null);
mm.invoke(device, (Object[]) null);
cancelPairingUserInput gives me a NoSuchMethodException, which is weird because the method does exist in BluetoothDevice class.
Is looks like Setpin or SetPasskey doesn't do anything. The device just wont pair. It only pairs after manually entering the pin.
So the only line of code that works is:
//Bonding the device:
Method mm = device.getClass().getMethod("createBond", (Class[]) null);
mm.invoke(device, (Object[]) null);
Logcat output:
09-27 12:34:46.408: ERROR/App(11671): cancelPairingUserInput [boolean]
java.lang.NoSuchMethodException: cancelPairingUserInput [boolean]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getMethod(Class.java:915)
at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.pair(BluetoothDiscoveryAndPairing.java:97)
at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.access$000(BluetoothDiscoveryAndPairing.java:25)
at test.app.bluetooth.model.BluetoothDiscoveryAndPairing$1.onReceive(BluetoothDiscoveryAndPairing.java:79)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:756)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
So what am I doing wrong?
The hidden method cancelPairingUserInput does not exist in your device. Don't use it.
You should register BroadcastReceiver for android.bluetooth.device.action.PAIRING_REQUEST
Call createBond()
Wait for BroadcastReceiver to trigger
In BroadcastReceiver if action is android.bluetooth.device.action.PAIRING_REQUEST
call this method
public void setBluetoothPairingPin(BluetoothDevice device)
{
byte[] pinBytes = convertPinToBytes("0000");
try {
Log.d(TAG, "Try to set the PIN");
Method m = device.getClass().getMethod("setPin", byte[].class);
m.invoke(device, pinBytes);
Log.d(TAG, "Success to add the PIN.");
try {
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
Log.d(TAG, "Success to setPairingConfirmation.");
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}
}
It also works on a device with Jelly Bean version (4.1.2) of Android.
this is works for me:
IntentFilter filter2 = new IntentFilter(
"android.bluetooth.device.action.PAIRING_REQUEST");
mActivity.registerReceiver(
pairingRequest, filter2);
private final BroadcastReceiver pairingRequest = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
mBluetoothDevice = needed;
try {
byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "1234");
Method m = mBluetoothDevice.getClass().getMethod("setPin", byte[].class);
m.invoke(mBluetoothDevice, pin);
mBluetoothDevice.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(mBluetoothDevice, true);
}
catch(Exception e)
{
e.printStackTrace();
}
I have to do a big task while a button click in my android app so i am using runnable to do background work. My works fine without the use of runnable but big task freezes it for a while. After the use of runnable my app is getting crashed on button click.
Here is my code for button onClick function:
public void doSearch(View v)
{
EditText et1 = (EditText) findViewById(R.id.searchText);
String query = et1.getText().toString();
Toast.makeText(this, "Searching Lyrics for "+query, Toast.LENGTH_LONG).show();
final String query1 = query.replaceAll(" ", "+");
Runnable myRunnable = new Runnable() {
#Override
public void run(){
try{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://example.com/search.php?q="+query1);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
String line = null;
int count=0,counter=0, disCount=0;
String[] name = new String[20];
String[] link = new String[20];
String[] dis = new String[20];
while ((line = reader.readLine()) != null && counter<20){
if(count == 1){
if(line.contains("href=\"")){
line = line.substring(line.indexOf('=')+2);
link[counter] = line.substring(0, line.indexOf('"'));
line = line.substring(line.indexOf('"')+2);
name[counter] = line.substring(0, line.indexOf('<'));
}
if(disCount==1){
if(line.contains("<b>")){
line = line.replaceAll("<b>", "");
}
if(line.contains("</b>")){
line = line.replaceAll("</b>", "");
}
dis[counter] = line+"...";
counter++;
disCount=0;
}
}
if(line.equals("<div class=\"sen\">")){
count = 1;
}
if(line.equals("<div>")){
disCount=1;
}
if(line.equals("</div>")){
count = 0;
}
}
is.close();
line 82: searchResult(name, link, dis);
}catch(IOException e){} catch(IllegalStateException e){}
}
};
Thread myThread = new Thread(myRunnable);
myThread.start();
}
public void searchResult(String[] name, String[] link, String[] dis)
{
line 91: setContentView(R.layout.results);
nameTemp = name;
linkTemp = link;
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < name.length; i++) {
if(name[i]==null) break;
if(name[0]==null){
Toast.makeText(this, "Sorry! No results matched your query. \n Try again! ", Toast.LENGTH_LONG).show();
}
RowItem item = new RowItem(name[i], dis[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.result);
CustomListViewAdapter adapter = new CustomListViewAdapter(this, R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, "Loading Lyrics for "+nameTemp[position]+"!", Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, ShowLyricsActivity.class);
i.putExtra("link", linkTemp[position]);
startActivity(i);
}
Here is my logcat:
I/ActivityManager(15444): Starting activity: Intent { act=android.intent.action.MAIN flg=0x10100000 cmp=com.BoxscapeInc.SongLyrics/.MainActivity }
V/ActivityThread(28108): Reporting idle of ActivityRecord{4a2ccbe0 token=android.os.BinderProxy#4a2cc658 {com.BoxscapeInc.SongLyrics/com.BoxscapeInc.SongLyrics.MainActivity}} finished=false
I/WindowManager(15444): Delivering pointer 0 > Window{4a5e2508 com.BoxscapeInc.SongLyrics/com.BoxscapeInc.SongLyrics.MainActivity paused=false}
I/WindowManager(15444): Delivering pointer 1 > Window{4a5e2508 com.BoxscapeInc.SongLyrics/com.BoxscapeInc.SongLyrics.MainActivity paused=false}
I/WindowManager(15444): Dispatching key to Window{4a5e2508 com.BoxscapeInc.SongLyrics/com.BoxscapeInc.SongLyrics.MainActivity paused=false}
I/WindowManager(15444): Dispatching key to Window{4a5e2508 com.BoxscapeInc.SongLyrics/com.BoxscapeInc.SongLyrics.MainActivity paused=false}
I/WindowManager(15444): Delivering pointer 0 > Window{4a5e2508 com.BoxscapeInc.SongLyrics/com.BoxscapeInc.SongLyrics.MainActivity paused=false}
I/WindowManager(15444): Delivering pointer 1 > Window{4a5e2508 com.BoxscapeInc.SongLyrics/com.BoxscapeInc.SongLyrics.MainActivity paused=false}
E/AndroidRuntime(28108): at com.BoxscapeInc.SongLyrics.MainActivity.searchResult(MainActivity.java:92)
E/AndroidRuntime(28108): at com.BoxscapeInc.SongLyrics.MainActivity$100000000.run(MainActivity.java:82)
W/ActivityManager(15444): Force finishing activity com.BoxscapeInc.SongLyrics/.MainActivity
I/ActivityManager(15444): Process com.BoxscapeInc.SongLyrics (pid 28108) has died.
Somebody please help me with this error.
Any help or suggestions would be appreciated.
In Android you can modify UI components only from main thread. You are accessing components in searchResult method called from other thread.
You can do it that way:
et1.post(new Runnable() {
#Override
public void run() {
searchResult(...);
}
});
It will force you to make some variables final, so you can use them in anonymous object. It is OK. See post method.
Personally I would recommend you using AsyncTask class instead of raw Java thread. It composes running one method in background thread and running another method in main thread.
Instead of doing network calls like you have done , I suggest you use this library
you can have a look at here
http://loopj.com/android-async-http/
your code will become very very less , instead of declaring so may asynctask seperately writing bulk of code , you can just use 4 lines of code
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
System.out.println(response);
}
});
It is very efficient in getting the response very quickly in 2 secs.
you don't need to use buffer reader or stuff to format the response. You can directly use the response recieved in string "response" of onSucces method.
I hope this will help you out. :)
This is my code to access Google Drive, taken largely from ArtOfWarfare in this post:
public class MainActivity extends Activity {
class OnTokenAcquired implements AccountManagerCallback<Bundle> {
boolean alreadyTriedAgain;
public OnTokenAcquired() {
// TODO Auto-generated constructor stub
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 3025) {
switch (resultCode) {
case RESULT_OK:
AccountManager am = AccountManager.get(getApplicationContext());
am.getAuthToken(am.getAccounts()[0],
"ouath2:" + DriveScopes.DRIVE,
new Bundle(),
true,
new OnTokenAcquired(),
null);
break;
case RESULT_CANCELED:
// This probably means the user refused to log in. Explain to them why they need to log in.
break;
default:
// This isn't expected... maybe just log whatever code was returned.
break;
}
} else {
// Your application has other intents that it fires off besides the one for Drive's log in if it ever reaches this spot. Handle it here however you'd like.
}
}
#Override
public void run(AccountManagerFuture<Bundle> result) {
try {
final String token = result.getResult().getString(AccountManager.KEY_AUTHTOKEN);
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
Drive.Builder b = new Drive.Builder(httpTransport, jsonFactory, null);
b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() {
#Override
public void initialize(JsonHttpRequest request) throws IOException {
DriveRequest driveRequest = (DriveRequest) request;
driveRequest.setPrettyPrint(true);
driveRequest.setKey("my number here");
driveRequest.setOauthToken(token);
}
});
final Drive drive = b.build();
final com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
body.setTitle("My Test File");
body.setDescription("A Test File");
body.setMimeType("text/plain");
File newFile = new File("this");
final FileContent mediaContent = new FileContent("text/plain", newFile);
new Thread(new Runnable() {
public void run() {
try {
com.google.api.services.drive.model.File file = drive.files().insert(body, mediaContent).execute();
alreadyTriedAgain = false; // Global boolean to make sure you don't repeatedly try too many times when the server is down or your code is faulty... they'll block requests until the next day if you make 10 bad requests, I found.
} catch (IOException e) {
if (!alreadyTriedAgain) {
alreadyTriedAgain = true;
AccountManager am = AccountManager.get(getApplicationContext());
am.invalidateAuthToken(am.getAccounts()[0].type, null); // Requires the permissions MANAGE_ACCOUNTS & USE_CREDENTIALS in the Manifest
am.getAuthToken(am.getAccounts()[0],
"ouath2:" + DriveScopes.DRIVE,
new Bundle(),
true,
new OnTokenAcquired(),
null);
} else {
// Give up. Crash or log an error or whatever you want.
}
}
}
}).start();
Intent launch = (Intent)result.getResult().get(AccountManager.KEY_INTENT);
if (launch != null) {
startActivityForResult(launch, 3025);
return; // Not sure why... I wrote it here for some reason. Might not actually be necessary.
}
} catch (OperationCanceledException e) {
// Handle it...
} catch (AuthenticatorException e) {
// Handle it...
} catch (IOException e) {
// Handle it...
}
}
}
private java.io.File downloadGFileToJFolder(Drive drive, String token, File gFile, java.io.File jFolder) throws IOException {
if (gFile.toURI() != null && gFile.toURI().toString().length() > 0 ) {
if (jFolder == null) {
jFolder = Environment.getExternalStorageDirectory();
jFolder.mkdirs();
}
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(gFile.toURI());
get.setHeader("Authorization", "Bearer " + token);
org.apache.http.HttpResponse response = client.execute(get);
InputStream inputStream = response.getEntity().getContent();
jFolder.mkdirs();
java.io.File jFile = new java.io.File(jFolder.getAbsolutePath() + "/" + gFile.getName()); // getGFileName() is my own method... it just grabs originalFilename if it exists or title if it doesn't.
FileOutputStream fileStream = new FileOutputStream(jFile);
byte buffer[] = new byte[1024];
int length;
while ((length=inputStream.read(buffer))>0) {
fileStream.write(buffer, 0, length);
}
fileStream.close();
inputStream.close();
return jFile;
} catch (IOException e) {
// Handle IOExceptions here...
return null;
}
} else {
// Handle the case where the file on Google Drive has no length here.
return null;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getApplicationContext();
AccountManager am = AccountManager.get(this);
am.getAuthToken(am.getAccounts()[0],
"ouath2:" + DriveScopes.DRIVE,
new Bundle(),
true,
new OnTokenAcquired(),
null);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
I get the following error when I launch the app (Android System also stops momentarily):
11-26 22:31:03.093: E/AndroidRuntime(4288): FATAL EXCEPTION: main
11-26 22:31:03.093: E/AndroidRuntime(4288): java.lang.RuntimeException: Unable to start activity ComponentInfo{android/android.accounts.GrantCredentialsPermissionActivity}: java.lang.NullPointerException
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2260)
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.app.ActivityThread.access$600(ActivityThread.java:139)
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.os.Looper.loop(Looper.java:156)
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.app.ActivityThread.main(ActivityThread.java:5045)
11-26 22:31:03.093: E/AndroidRuntime(4288): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 22:31:03.093: E/AndroidRuntime(4288): at java.lang.reflect.Method.invoke(Method.java:511)
11-26 22:31:03.093: E/AndroidRuntime(4288): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-26 22:31:03.093: E/AndroidRuntime(4288): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-26 22:31:03.093: E/AndroidRuntime(4288): at dalvik.system.NativeStart.main(Native Method)
11-26 22:31:03.093: E/AndroidRuntime(4288): Caused by: java.lang.NullPointerException
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.accounts.GrantCredentialsPermissionActivity.onCreate(GrantCredentialsPermissionActivity.java:84)
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.app.Activity.performCreate(Activity.java:4543)
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
11-26 22:31:03.093: E/AndroidRuntime(4288): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2181)
11-26 22:31:03.093: E/AndroidRuntime(4288): ... 11 more
In addition, my phone shows a strange notification: "Permission Requested for account Weather". Anyone have any idea what's causing this?
Try replacing this:
am.getAccounts()[0],
with this:
am.getAccountsByType("com.google")[0],
My code in the other topic was over simplified to assume that the first account it found would be a Google Account (and so have a Google Drive). The code we actually used in the app checked to make sure it was a Google Account (and then performed further checks to make sure it was a company account, which is why I simplified the code to what I shared.)