Javamail message content newline - android

I've made an E-Mail Client for my Android Phone using the JavaMail API, and I try to get the message Content with the following Code:
Object contentObject = p.getContent();
InputStream is = (InputStream) contentObject;
reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String everything = sb.toString();
System.out.println(everything);
return everything;
With this method, I get the messsage Content as a String, but without Newlines. How can I format this String that he has the newlines from the Message?
P.S.: This are German e- mails, so the problem may be the encoding!?

Add like that
sb.append(line);
sb.append(System.getProperty("line.separator"));

Related

How to download String file which contain special characters of slovenia

I am trying to download the json file which contains slovenian characters,While downloading json file as a string I am getting special character as specified below in json data
"send_mail": "Po�lji elektronsko sporocilo.",
"str_comments_likes": "Komentarji, v�ecki in mejniki",
Code which I am using
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
try {
InputStream input1 = new BufferedInputStream(url.openStream(), 300);
String myData = "";
BufferedReader r = new BufferedReader(new InputStreamReader(input1));
StringBuilder totalValue = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
totalValue.append(line).append('\n');
}
input1.close();
String value = totalValue.toString();
Log.v("To Check Problem from http paramers", value);
} catch (Exception e) {
Log.v("Exception Character Isssue", "" + e.getMessage());
}
I want to know how to get characters downloaded properly.
You need to encode string bytes to UTF-8. Please check following code :
String slovenianJSON = new String(value.getBytes([Original Code]),"utf-8");
JSONObject newJSON = new JSONObject(reconstitutedJSONString);
String javaStringValue = newJSON.getString("content");
I hope it will help you!
Decoding line in while loop can work. Also you should add your connection in try catch block in case of IOException
URL url = new URL(f_url[0]);
try {
URLConnection conection = url.openConnection();
conection.connect();
InputStream input1 = new BufferedInputStream(url.openStream(), 300);
String myData = "";
BufferedReader r = new BufferedReader(new InputStreamReader(input1));
StringBuilder totalValue = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
line = URLEncoder.encode(line, "UTF8");
totalValue.append(line).append('\n');
}
input1.close();
String value = totalValue.toString();
Log.v("To Check Problem from http paramers", value);
} catch (Exception e) {
Log.v("Exception Character Isssue", "" + e.getMessage());
}
It's not entirely clear why you're not using Android's JSONObject class (and related classes). You can try this, however:
String str = new String(value.getBytes("ISO-8859-1"), "UTF-8");
But you really should use the JSON libraries rather than parsing yourself
When creating the InputStreamReader at this line:
BufferedReader r = new BufferedReader(new InputStreamReader(input1));
send the charset to the constructor like this:
BufferedReader r = new BufferedReader(new InputStreamReader(input1), Charset.forName("UTF_8"));
problem is in character set
as per Wikipedia Slovene alphabet supported by UTF-8,UTF-16, ISO/IEC 8859-2 (Latin-2). find which character set used in server, and use the same character set for encoding.
if it is UTF-8 encode like this
BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(inputStream), Charset.forName("UTF_8"));
if you had deffrent character set use that.
I have faced same issue because of the swedish characters.
So i have used BufferedReader to resolved this issue. I have converted the Response using StandardCharsets.ISO_8859_1 and use that response. Please find my answer as below.
BufferedReader r = new BufferedReader(new InputStreamReader(response.body().byteStream(), StandardCharsets.ISO_8859_1));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
{
total.append(line).append('\n');
}
and use this total.toString() and assigned this response to my class.
I have used Retrofit for calling web service.
I finally found this way which worked for me
InputStream input1 = new BufferedInputStream(conection.getInputStream(), 300);
BufferedReader r = new BufferedReader(new InputStreamReader(input1, "Windows-1252"));
I figured out by this windows-1252, by putting json file in asset folder of the android application folder, where it showed same special characters like specified above,there it showed auto suggestion options to change encoding to UTF-8,ISO-8859-1,ASCII and Windows-1252, So I changed to windows-1252, which worked in android studio which i replicated the same in our code, which worked.

How to send Command prompt output to android

I am creating an app in which it will send some command to server and i want to get the output of that command on client (android).
Basically i am sending command "systeminfo" and the output is too big to handle, so is there any way to get that big output on android as text view or anything else?
Code is as below
Process process = Runtime.getRuntime().exec("systeminfo");
and for get the output i have used
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
I am confused how to use it as too much of string. Any reference material would be appreciated.
I'd recommend using a StringBuilder to reconstruct each line from the BufferedReader
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line).append("\n"); // Remove the \n if you don't want newlines
}
final String execOutput = builder.toString();

JAVAMAIL get Content with InputStream

I'm trying to get the content Text of a Message in my Android App with an InputStream, because there I can get a line Separator. I'm getting the following Exception when I'm trying it:
java.lang.ClassCastException: java.lang.String cannot be cast to java.io.InputStream
This is my Code:
Object o = message.getContent();
InputStream is = (InputStream)o;
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String everything = sb.toString();
Do you know what the problem is? In every Javamail - Thread you can read that this Method runs.
Use the Message.getInputStream method.

How to get data from html in android

I have get html data from webpage. But i want to get only data excluding html tags.
I have tried this:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(urlText.getText().toString());
// Get the response
BufferedReader rd = new BufferedReader(new InutStreamReader(response.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null)
{
textView.append(line);
sb.append(line+"\n");
}
This giving me whole html data. Tell me now i can get data only.
Have you tried using Html.fromHtml(source)? or use any Java HTML parser (If they work on android) for this.
Here source is your html formatted whole data.
EDIT:
while ((line = rd.readLine()) != null)
{
sb.append(line+"\n");
}
String source = sb.toString();
textView.setText(Html.fromHtml(source));
Look at this example Android Parsing HTML Content Containing Links.

Using a buffer to convert http request responses to string in Android - Not getting entire response

I'm developing an app that posts to a site and I'm trying to store the entity response as a string. However, the string only seems to contain a small portion of the response, roughly 35 lines or so. I'm wondering if it has something to do with buffer overflow but really I am not sure. My code is below:
static String getResponseBody(HttpResponse response) throws IllegalStateException, IOException{
String content = null;
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null)
{
if(isBlankString(line) == false)
{
sb.append(line + "\n");
}
}
br.close();
content = sb.toString();
}
return content;
isBlankString just notes if a line doesn't contain any characters, as there's alot of blank lines in the response that were bugging me. I have the issue of not getting the whole response with or without this. Any body know what's going on or how to fix this?
Thanks
In my application I use just single line to get response string from entity:
final String responseText = EntityUtils.toString(response.getEntity());

Categories

Resources