Hallo Android-Developer-Community,
ich habe folgendes Problem, und zwar möchte ich per button klick einen xml-file aus dem internet downloaden. dazu habe ich ein codebeispiel aus dem stackoverflow-forum gefunden. habe davor schon andere methoden verscuht. jedoch bekomme ich immer eine NetworkOnMainThreadException-Fehlermeldung. Ich weiß leider nicht mehr weiter bzw. was ich falsch mache und bitte um Hilfe und ratschläge.
Hier ist mein jetztiger Code:
MainActivity:
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
static final String URL = "http://www.overpass-api.de/api/xapi?*[bbox=7.1,51.2,7.2,51.3]";
TextView parameter;
Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
parameter = (TextView)findViewById(R.id.textView_httpResult);
start = (Button)findViewById(R.id.btn_start);
}
public void getparam(View v) {
String fileName = "test";
String DownloadUrl = "http://www.overpass-api.de/api/xapi?*" +
"[key=value][bbox=11.58594,52.17035,11.63298,52.18888]";
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/xmls");
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining");
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager", "download ready in" +
((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Alles anzeigen
Main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:onClick="getparam"
android:text="GetOSMParameter" />
<TextView
android:id="@+id/textView_httpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/btn_start"
android:layout_marginTop="48dp"
android:text=" "
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
Alles anzeigen
folgende persmissions sind festgelegt:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Weiß jmd. einen rat? wäre für jede hilfe sehr dankbar.
viele grüße