我正在尝试将JSONObject附加到驻留在res / raw文件夹中的JSON文件的JSONArray,但这样做会导致我收到IllegalArgumentException,如下面的logcat中所示:
请指导我。 在此先感谢
03-23 13:38:36.219 2180-2180/in.naushad.cctexample W/System.err: java.lang.IllegalArgumentException: File android.resource://in.naushad.cctexample/2131099648 contains a path separator
03-23 13:38:36.229 2180-2180/in.naushad.cctexample W/System.err: at android.app.ContextImpl.makeFilename(ContextImpl.java:2457)
03-23 13:38:36.229 2180-2180/in.naushad.cctexample W/System.err: at android.app.ContextImpl.openFileOutput(ContextImpl.java:999)
03-23 13:38:36.229 2180-2180/in.naushad.cctexample W/System.err: at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:188)
03-23 13:38:36.229 2180-2180/in.naushad.cctexample W/System.err: at in.naushad.cctexample.add_entry.AppendContentsToJSON(add_entry.java:88)
03-23 13:38:36.229 2180-2180/in.naushad.cctexample W/System.err: at in.naushad.cctexample.add_entry.access$000(add_entry.java:20)
03-23 13:38:36.229 2180-2180/in.naushad.cctexample W/System.err: at in.naushad.cctexample.add_entry$1.onClick(add_entry.java:38)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at android.view.View.performClick(View.java:4797)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at android.view.View$PerformClick.run(View.java:19899)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at android.os.Looper.loop(Looper.java:135)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5310)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at java.lang.reflect.Method.invoke(Native Method)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
03-23 13:38:36.230 2180-2180/in.naushad.cctexample W/System.err: at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
add_entry活动:
package in.naushad.cctexample;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class add_entry extends AppCompatActivity {
EditText etAddLen,etAddDen,etAddPersonName;
Button btAddEntry;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_entry);
initXML();
btAddEntry.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String StringetAddLen=etAddLen.getText().toString();
String StringetAddDen=etAddDen.getText().toString();
String StringetAddPersonName=etAddPersonName.getText().toString();
AppendContentsToJSON(StringetAddLen,StringetAddDen,StringetAddPersonName);
startActivity(new Intent(add_entry.this,MainActivity.class));
}
});
}
void initXML(){
etAddLen = (EditText) findViewById(R.id.etAddLen);
etAddDen = (EditText) findViewById(R.id.etAddDen);
etAddPersonName = (EditText) findViewById(R.id.etAddPersonName);
btAddEntry = (Button) findViewById(R.id.btAddEntry);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getResources().openRawResource(R.raw.data);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
private void AppendContentsToJSON(String StringetAddLen1,String StringetAddDen1,String StringetAddPersonName1)
{
try {
JSONObject jsonObjMain = new JSONObject(loadJSONFromAsset());
JSONObject jO = new JSONObject();
JSONArray jArry = jsonObjMain.getJSONArray("JSONLenDen");
//Add Data
jO.put("Len", StringetAddLen1);
jO.put("Den",StringetAddDen1);
jO.put("Name",StringetAddPersonName1);
//Append
jArry.put(jO);
try {
String path = "android.resource://" + getPackageName() + "/" + R.raw.data;
FileOutputStream fos = openFileOutput(path, Context.MODE_PRIVATE);
String test=jsonObjMain.toString();
fos.write(test.getBytes());
fos.close();
}catch (Exception e){
Toast.makeText(add_entry.this,"exception",Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
res / raw中的data.json:
{
"JSONLenDen": [
{
"Len": "125",
"Den": "685",
"Name":"A"
},
{
"Len": "385",
"Den": "3248",
"Name":"B"
},
{
"Len": "385",
"Den": "3248",
"Name":"C"
}
]
}