Ich bin neu bei Android und weiß nicht viel. Ich brauche Hilfe, um diesen Code zu ändern - ich möchte etwas Text von einer Website herunterladen. Wenn ich den folgenden Code verwende: new DownloadTextTask(). Execute ("http://www.test.com/file.xml");wie man Text aus dem Internet in Android herunterladen?
Es zeigt nicht den heruntergeladenen Text in TextView
, aber wenn ich die App wieder öffnen, kann ich den Text sehen. Ich denke, es ist asynchron, und es hat eine Verzögerung beim Herunterladen von Text.
Wie kann ich dieses Problem beheben, oder einfach nur diesen Code verwenden statt: (I DownloadText()
Funktion direkt meine Verwendung Es Arbeit Ball hielt, als ich versuchte!)
TextView txt = (TextView)findViewById(R.id.textTest);
txt.setText(DownloadText("http://www.test.com/"));
den gesamten Code:
public class MainActivity extends AppCompatActivity {
public InputStream OpenHttpConnection(String urlString) throws IOException{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
Log.d("MainActivity", ex.getLocalizedMessage());
throw new IOException("Error connecting");
}
return in;
}
public String DownloadText(String URL)
{
int BUFFER_SIZE = 2000;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
} catch (IOException e) {
Log.d("MainActivity", e.getLocalizedMessage());
return "";
}
InputStreamReader isr = new InputStreamReader(in);
int charRead;
String str = "";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while ((charRead = isr.read(inputBuffer))>0) {
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
in.close();
} catch (IOException e) {
Log.d("MainActivity", e.getLocalizedMessage());
return "";
}
return str;
}
private class DownloadTextTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
return DownloadText(urls[0]);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
}
}
public static class xmlClass
{
public static String xml ="";
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//new DownloadTextTask().execute("http://www.test.com/file.xml");
TextView txt = (TextView)findViewById(R.id.textTest);
txt.setText(DownloadText("http://www.test.com/file.xml"));
}
}
warum verwenden Sie nicht webview für das? –
Verwenden Sie txt.setText (Ergebnis) in onPostExecute-Methode – Muthu
@Muthu Ich muss es in String-Variable speichern, um es zu analysieren. – Saeid