I am trying to read a text file from my localhost (WAMP), but getting nothing back.
Could someone point out what I am doing wrong please.
This is how I am trying to achieve it.
try {
URL url = new URL("http://10.0.2.2/text.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
while ((str = in.readLine()) != null) {
tv.append(str);
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) { }
It works with a true URL, but not whenever I use localhost. I have tried using localhost and 127.0.0.1 in URL too.
Related
I am trying to read in a file using the fstream. I am writing in C++11, but interfacing it with Java via JNI in Android Studio. It doesn't open the file for some reason. I am using a relative file path and I don't understand why it can't open the file. The file is named proverbs.txt. There aren't any discrepancies within the name like proverbs.txt.txt or anything like that.
Here's the code:
void storeProverbs() {
string path = "/Users/tenealaspencer/Desktop/proverbs.txt";
std::ifstream provInput(path.c_str(), std::ios::in);
//provInput.open("/Users/tenealaspencer/Desktop/proverbs.txt");
// opens the proverbs text file
equivInput.open("/Users/tenealaspencer/AndroidStudioProjects/example/app/src/main/cpp/stored.txt"); // opens the stored (English) proverbs text file
if (!provInput.is_open()) {
cout << "error ";
}
while (!provInput.eof()) // while not at the end of the proverbs file
{
getline(provInput, phrase); // read proverbs in line by line
getline(equivInput, storedProv); // read english proverbs in line by line
Never mind I just imported the file via Java using the following code:
try {
InputStream is = getAssets().open("stopwords.txt");
String line1;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while((line1 = reader.readLine()) != null) //
{
try {
byte[] utf8Bytes = line1.getBytes("UTF8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
storeStopWords(line1);
}
}
catch (IOException e) {
e.printStackTrace();
}
try {
InputStream is = getAssets().open("proverbs.txt");
InputStream iz = getAssets().open("stored.txt");
String line;
String line2; //= new String ("");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(iz));
while((line = reader.readLine()) != null && (line2 = reader2.readLine()) != null ) //
{
try {
byte[] utf8Bytes = line.getBytes("UTF8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
storeProverbs(line,line2);
}
}
catch (IOException e) {
e.printStackTrace();
}
I read somewhere that JNA doesn't support the fstream library or something like that. In any case it works.
I've developed an application to parse point KML files and get their elevation from Google Elevation API. My problem is that when the application is running and then turn on the network of the device, it throws the Unable to resolve host "maps.googleapis.com", But when I turn on the network before running the application, it returns elevation of points from host service. Below is the snippet for connecting to URL and get data as string:
public class HttpManager{
public static String getData(String uri) {
BufferedReader reader = null;
try {
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
connection.disconnect();
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
}
}
The JSON result will be parsed in another class to get the elevation. What is the reason of this execption?
Some tips I want to mention:
1.The INTERNET permission is provided in Manifest.xml.
2.The API key is authorized.
I have a file in my devise called test.csv. when i click on that file is opened through my app.how to set default open with(dialog) option to my app in android?
above is the sample dailog.how to add my app to the dialog list?
I think you may want to read the csv file. you could get the csv file path. So see the following.
public static void readCSV(File file) {
BufferedReader reader = null;
StringBuilder stringBuilder = new StringBuilder();
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file));// your csv file
reader = new BufferedReader(isr);
String line = null; // every time read one line
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The stringBuilder.toString() is your csv file's content. Sorry for my English.
I want to know how to capture logs with in mobile phone
Does this network logs can be used to measure page load time ?
I tried capturing device logs using adb logcat in desktop but I want to capture those logs with in device
Assuming your logs are located in a String file, you can save them to a text file like this:
private void saveLogsToFile(String logs) {
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("saved_logs.txt", Context.MODE_PRIVATE));
out.write(logs);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
And then read them back from the file like this:
private String readSavedLogs() {
String logs = "";
try {
InputStream inputStream = openFileInput("saved_logs.txt");
if(inputStream != null) {
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(in);
String line;
StringBuilder stringBuilder = new StringBuilder();
while((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
inputStream.close();
logs = stringBuilder.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return logs;
}
I can't figure out how to make this code work in an AsyncTask, I searched for multiple examples but it keeps crashing. I found this simple code on the internet and I want to adapt it to get the URL from a textfield and get the HTML code. I found out it has to be in an AsyncTask otherwise it won't work but even in an AsyncTask I can't get it to work. Here's my code:
String ETURL = ETURLInput.getText().toString();
try {
URL TestURL = new URL(ETURL);
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(TestURL.openStream()));
String outputCode;
while ((outputCode = bufferReader.readLine()) != null)
TVCode.setText(outputCode);
bufferReader.close();
} catch (Exception e) {
TVCode.setText("Oops, something went wrong.")
}
}
This is the code which needs to be executed inside an ActionListener. So when I click the button it should execute this code in an AsyncTask.
Hopefully somebody could help me with this.
You forgot to add openConnection, add this: URLConnection conn = TestURL.openConnection(); after creating your URL object.
To make it work with an asynctask, what you can do is storing your string in a class variable, returning it in the doInBackGround and using it in your onPostExecute.
An example of method you can create in your asynctask:
protected String getContentUrl(String URL) {
String line=null;
String result="";
try {
try {
URL url;
// get URL content
url = new URL(URL);
URLConnection conn = url.openConnection();
// open the stream and put it into BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
line=br.readLine();
while (line!= null) {
result=result+line;
line=br.readLine();
}
//System.out.print(result);
br.close();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
Then you get your result this way on doInBackGround:
getContentUrl(YOUR URL HERE)
Store this value in a String, and return it. Then you can use it in your onPostExecute
Hope it helps :)