私はAndroidプログラミングにかなり慣れていません。私の問題は、アセットフォルダ内のファイルに情報を書き込むことです。しかし、Eclipseは私が理解できない例外をスローします:不正なファイル番号の例外..これが私のコードです:
package com.example.marport;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import com.example.marport.R;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Plakasorgulama extends Activity {
final static String NAMESPACE = "http://webservice.marport.dcat.com/";
final static String METHOD_NAME = "truckLicenceDate";
final static String SOAP_ACTION = "http://webservice.marport.dcat.com/truckLicenceDate";
final static String URL = "http://192.168.1.164:8070/test/services/MarportWebServicePort";
private EditText plaka;
private Button sorgu;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.plakasorgulama);
plaka = (EditText) findViewById(R.id.etPlaka);
sorgu = (Button) findViewById(R.id.bPlakaSorgulama);
final SoapObject Request=new SoapObject(NAMESPACE,METHOD_NAME);
sorgu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String plakainfo = plaka.getText().toString();
SoapObject request = new SoapObject("licence", plakainfo);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet=false;
soapEnvelope.setOutputSoapObject(Request);
HttpTransportSE aht=new HttpTransportSE(URL);
try {
AssetFileDescriptor descriptor = getAssets().openFd("htmlcode.txt");
BufferedWriter out = new BufferedWriter(new FileWriter(descriptor.getFileDescriptor()));
out.write("tryyyyyyyyyyyyyyyyyyyyyy");
out.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
aht.call(SOAP_ACTION, soapEnvelope);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
SoapPrimitive result=(SoapPrimitive) soapEnvelope.getResponse();
} catch (SoapFault e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
また、アセットフォルダのダイレクトパスを指定してみました。そうすると、今回はFileNotFound例外が発生します。この問題で気分が悪くなります。
そして、これらはログです:
08-27 07:38:36.749: W/System.err(740): java.io.IOException: Bad file number
08-27 07:38:36.770: W/System.err(740): at org.apache.harmony.luni.platform.OSFileSystem.writeImpl(Native Method)
08-27 07:38:36.770: W/System.err(740): at org.apache.harmony.luni.platform.OSFileSystem.write(OSFileSystem.java:129)
08-27 07:38:36.800: W/System.err(740): at java.io.FileOutputStream.write(FileOutputStream.java:297)
08-27 07:38:36.800: W/System.err(740): at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:175)
08-27 07:38:36.800: W/System.err(740): at java.io.OutputStreamWriter.close(OutputStreamWriter.java:151)
08-27 07:38:36.800: W/System.err(740): at java.io.BufferedWriter.close(BufferedWriter.java:130)
08-27 07:38:36.800: W/System.err(740): at com.example.marport.Plakasorgulama$1.onClick(Plakasorgulama.java:61)
08-27 07:38:36.800: W/System.err(740): at android.view.View.performClick(View.java:2408)
08-27 07:38:36.800: W/System.err(740): at android.view.View$PerformClick.run(View.java:8816)
08-27 07:38:36.800: W/System.err(740): at android.os.Handler.handleCallback(Handler.java:587)
08-27 07:38:36.800: W/System.err(740): at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 07:38:36.810: W/System.err(740): at android.os.Looper.loop(Looper.java:123)
08-27 07:38:36.820: W/System.err(740): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-27 07:38:36.820: W/System.err(740): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 07:38:36.820: W/System.err(740): at java.lang.reflect.Method.invoke(Method.java:521)
08-27 07:38:36.820: W/System.err(740): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-27 07:38:36.820: W/System.err(740): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-27 07:38:36.820: W/System.err(740): at dalvik.system.NativeStart.main(Native Method)
どうもありがとうございます。