4 개의 탭이있는 앱이 있는데 하나는 웹에서 입력 텍스트 파일을 업데이트하고 SD 카드에 쓰는 것입니다. try catch 블록 때문에 코드가 실행되지만 추적에 연결 오류가 표시됩니다. 파일은 sd 카드에 기록되지만 0 바이트입니다. 그래서 파일 쓰기는 좋지만 hhtp 연결 문제가 있다고 생각합니다. 나는 Windows 7 64 비트를 최신 SDK와 함께 사용하고 있습니다.Android : 웹에서 텍스트 파일을 다운로드하는 중 오류가 발생했습니다.
public void downloadFile (String urlfile) throws IllegalStateException, IOException
{
InputStream instream = null;
String result = null;
try {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
URL url = new URL(urlfile);
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(urlfile);
// Execute the request
HttpResponse response;
File file = new File(Environment.getExternalStorageDirectory(), "myfile.txt");
FileOutputStream fileOutput = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(
fileOutput);
try {
response = httpclient.execute(httpget);
// Examine the response status
Log.i("Praeda",response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
// A Simple JSON Response Read
instream = entity.getContent();
result= convertStreamToString(instream);
// now you have the string representation of the HTML request
}
myOutWriter.write(result);
instream.close();
myOutWriter.close();
fileOutput.close();
//catch some possible errors...
} catch (Exception e) {
e.printStackTrace();
} finally {
}
//catch some possible errors...
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
private static String convertStreamToString(InputStream is)
{
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
가 여기에 로그 캣 출력입니다 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gmail.tenglish7.lae"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gmail.tenglish7.lae.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
어떤 도움이나 제안을 주시면 감사하겠습니다 :
W/ActivityManager(278): Unbind failed: could not find connection for [email protected]
W/System.err(720): android.os.NetworkOnMainThreadException
W/System.err(720): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
W/System.err(720): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
W/System.err(720): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
W/System.err(720): at java.net.InetAddress.getAllByName(InetAddress.java:214)
W/System.err(720): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
W/System.err(720): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
W/System.err(720): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
W/System.err(720): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
W/System.err(720): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
W/System.err(720): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
W/System.err(720): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
W/System.err(720): at com.gmail.tenglish7.lae.MainActivity$UpdateFragment.downloadFile(MainActivity.java:506)
W/System.err(720): at com.gmail.tenglish7.lae.MainActivity$UpdateFragment.myClick(MainActivity.java:466)
W/System.err(720): at com.gmail.tenglish7.lae.MainActivity$UpdateFragment$1.onClick(MainActivity.java:448)
W/System.err(720): at android.view.View.performClick(View.java:4204)
W/System.err(720): at android.view.View$PerformClick.run(View.java:17355)
W/System.err(720): at android.os.Handler.handleCallback(Handler.java:725)
W/System.err(720): at android.os.Handler.dispatchMessage(Handler.java:92)
W/System.err(720): at android.os.Looper.loop(Looper.java:137)
W/System.err(720): at android.app.ActivityThread.main(ActivityThread.java:5041)
W/System.err(720): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(720): at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(720): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
W/System.err(720): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
W/System.err(720): at dalvik.system.NativeStart.main(Native Method)
가 여기 내 Manifest.xml 파일의
여기 내 코드입니다. 미리 감사드립니다.
또 하나의 사람은 그렇게 어떻게 AsyncTask를 ... http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Piovezan