Source not found error - android

i am new android developer
when ever i run this code i get this error "Source not found." just when it reaches the
url.openStream()
any idea how to fix this?
try {
URL url = new URL("http://pollsdb.com/test.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}

<uses-permission
android:name="android.permission.INTERNET" />
add that under your first manifest tag in your AndroidManifest.xml

I have the same problem too. But now, i have been solved this problem by reference http://developer.android.com/guide/components/processes-and-threads.html
about the thread use.
You need to create a worker thread to work for open the URL stream rather than using the main thread(UI thread).
Of course, you also need to add
<uses-permission android:name="android.permission.INTERNET" />
in your AndroidManifest.xml

Related

Issues when fetching data/Json/file from assets manager on Android 10

Below is my code for reading Json file from assets, It works on every other device except Pixel 3 XL which android version is 10.This device returning null from assets
StringBuilder builder = new StringBuilder();
BufferedReader reader = null;
try {
reader = new BufferedReader( newInputStreamReader( MyApp.getAppInstance().getAssets().open(fileName)));
// do reading, usually loop until end of file reading
String mLine;
builder = new StringBuilder();
while ((mLine = reader.readLine()) != null) {
builder.append(mLine);
}
} catch (IOException e) {
//log the exception
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {}
}
}
return builder.toString();
}
Make sure you have given the required permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and for Android 10 issue try using
android:requestLegacyExternalStorage="true"
inside your application tag

why process give null inputstream in test runner class?

In my android test project, I simply read the logcat using adb command
like,
public StringBuilder log=new StringBuilder();
public String line="";
public String temp="";
public void testSolo() throws Exception {
String baseCommand = "logcat -v time";
baseCommand += " ActivityManager:I "; // Info for my app
baseCommand += " *:S "; // Silence others
try {
Process logReaderProcess = Runtime.getRuntime().exec(baseCommand);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(logReaderProcess.getInputStream()));
while ((line =bufferedReader.readLine()) != null) {
log.append(line); // here readLine() returns null
}
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
but, here in string line I always get null value,
while the same thing always run in the android activity's onCreate() .
I don't understand why this happen?
Same thing runs in activity class and not in the android test project.
I also add use -permission for READ_LOGS and WRITE_EXTERNAL_STORAGE in test project's
manifest.xml file.
Is there anybody knows how it works or what happens?
Thanks in advance.
Try to add
<uses-permission android:name="android.permission.READ_LOGS" />
to your manifest.
String []baseCommand = new String[]{"logcat", "-v","time"};
Process logReaderProcess = Runtime.getRuntime().exec(baseCommand);
try this out

Android network errors connecting to an address

Snippet provided:
public void update(){
try {
Socket appSoc = new Socket( "XXX.XXX.XXX.X" ,XXXXX);
BufferedReader in = new BufferedReader(new
InputStreamReader(appSoc.getInputStream()));
for (int i = 0; i < 100; i++) {
String message = in.readLine();
add(message);}
}
catch (Exception e) {
add("ERROR" + e);
}
}
add(String text) adds text to a textview.
Using the domain name instead of the IP address says that the phone cannot find the domain, is this an android problem, because it runs fine on java on the desktop.
You are probably missing the Internet persmission in your manifest. Make sure it is located outside of the application tag, like this:
<manifest>
<application>
.
.
.
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
In your androidmanifest.xml, check to see if you have given proper internet permissions.
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Prevent Activity From Launching

Is there a good way of preventing an activity from launching? I'd like to build either a whitelist or blacklist of applications, then prevent those instances from being started.
One potential solution is to poll the running tasks every so often and shut them down, but this seems like it could eat through a lot of battery.
Sorry, this is not supported with the SDK.
Ok so this doesnt count as a "GOOD WAY", could very well be as draining if not more so on your battery, but in line with your polling method:
you could create a service that monitors the log cat (http://www.helloandroid.com/tutorials/reading-logs-programatically)
need permission in manifest:
<uses-permission android:name="android.permission.READ_LOGS" />
then in your service something like (very crude):
private void readLogAndKill(){
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("Starting Activity")){
if(line.contains("com.twitter.android"){
//kill twitter for example
}
}
}
} catch (IOException e) {
}
}
there may be someway to catch the "Starting Activity" events - or filter the log as you read it for just those msgs.

Reading Logcat within the app returns null

I read the other posts and can't figure out the "trick".
I looked at Log Collector but can't use a separate APK. I'm basically using the same approach and I consistently get nothing back on the processes inputstream.
I have READ_LOGS in the manifest.
From within my default activity, I'm able to get the log, but if I move the logic to another activity or utilize an asynctask, no output is returned.
this code is from my default activity... inline, i dump it to the log
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
Log.d(LOGTAG, "Logcat: " +log.toString());
} catch (IOException e) {}
if i wrap it in an asynctask or just inline it in another activity, it returns nothing
ArrayList<String> commandLine = new ArrayList<String>();
//terminate on completion and suppress everything except the filter
commandLine.add("logcat -d -s");
...
//replace asynctask with inline (could not get log in asynctask)
showProgressDialog(getString(R.string.acquiring_log_progress_dialog_message));
final StringBuilder log = new StringBuilder();
BufferedReader bufferedReader = null;
try{
Process process = Runtime.getRuntime().exec(commandLine.toArray(new String[0]));
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null){
log.append(line);
log.append(MangoApp.LINE_SEPARATOR);
}
sendIntent.putExtra(Intent.EXTRA_TEXT, log.toString());
startActivity(Intent.createChooser(sendIntent, getString(R.string.chooser_title)));
dismissProgressDialog();
dismissMainDialog();
finish();
}
catch (IOException e){
dismissProgressDialog();
showErrorDialog(getString(R.string.failed_to_get_log_message));
Log.e(LOGTAG, "Log collection failed: ", e);//$NON-NLS-1$
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ignore) {}
}
}
Can anyone spot the diff or explain the magic? I'm pretty sure the commandline is right in the second version so scratching my head. I'm using 2.1 SDK 7 on the emulator.
Thanks
Hope this will be helpful, you don't have to create file by your self just execute the below command, to get the error info.
Runtime.getRuntime().exec("logcat -v time -r 100 -f /sdcard/log.txt *:E");
Logcat parameters options:
-r <size in kilobytes> -> for specifying the size of file
-f <filename> -> file to which you want to write the logs.
Can you try it without the ArrayList. Just pass the command String
I have implemented it in the following way (without the ArrayList). It works for me.
String baseCommand = "logcat -v time";
baseCommand += " MyApp:I "; // Info for my app
baseCommand += " *:S "; // Silence others
ServicesController.logReaderProcess = Runtime.getRuntime().exec(baseCommand);

Categories

Resources