Note that there are some explanatory texts on larger screens.

plurals
  1. POsaving a json file to internal memory
    primarykey
    data
    text
    <p>hey there guys and girls i have this code that should download a json object and then save it to internal memory, i keep getting stuck here</p> <pre><code> try{ //connects to mySQL HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://10.0.2.2/textures_story_list.php"); HttpResponse response = client.execute(post); //captures the response entity = response.getEntity(); }catch(Exception e) { Log.e("log_tag", "Error in http connection "+e.toString()); } try{ is = entity.getContent(); String FILENAME = "story.json"; //gives file name FileOutputStream output = openFileOutput(FILENAME, MODE_WORLD_READABLE); //creates new StreamWriter OutputStreamWriter writer = new OutputStreamWriter(output); //writes json with file name story.json writer.write(is); writer.flush(); //closes writer writer.close(); }catch(Exception e) { Log.e("log_tag", "Error saving string "+e.toString()); } </code></pre> <p>i keep getting erros on writer.write(is); eclipse asks me to change it to an int, what would be the best way to code from the line </p> <pre><code>entity = response.getEntity(); </code></pre> <p>???</p> <p>here is my full code</p> <pre><code> public class MainActivity extends Activity { String storyObj = ""; Object json = null; HttpEntity entity = null; InputStream is = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //button that saves the file from mySQL Button save = (Button) findViewById(R.id.downloadBtn); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { saveJson(); } }); //Button that opens the file from InternalMemory Button open = (Button) findViewById(R.id.showBtn); open.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openJson(); } }); //end of onCreate() } //saveJson pull a JSON file from mySQl server then saves that file in its JSON type eg .json public void saveJson(){ TextView test = (TextView) findViewById(R.id.showView); try{ //connects to mySQL HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://10.0.2.2/textures_story_list.php"); HttpResponse response = client.execute(post); //captures the response entity = response.getEntity(); }catch(Exception e) { Log.e("log_tag", "Error in http connection "+e.toString()); } try{ is = entity.getContent(); String FILENAME = "story.json"; //gives file name FileOutputStream output = openFileOutput(FILENAME, MODE_WORLD_READABLE); //creates new StreamWriter OutputStreamWriter writer = new OutputStreamWriter(output); //writes json with file name story.json writer.write(is); writer.flush(); //closes writer writer.close(); }catch(Exception e) { Log.e("log_tag", "Error saving string "+e.toString()); } //end of saveJson() } private char[] Object(Object json2) { // TODO Auto-generated method stub return null; } public void openJson(){ TextView test = (TextView) findViewById(R.id.showView); try{ FileInputStream fileInput = openFileInput("story.json"); BufferedReader inputReader = new BufferedReader(new InputStreamReader(fileInput, "UTF-8"), 8); StringBuilder strBuilder = new StringBuilder(); String line = null; while ((line = inputReader.readLine()) != null) { strBuilder.append(line + "\n"); } fileInput.close(); storyObj = strBuilder.toString(); }catch(IOException e){ Log.e("log_tag", "Error building string "+e.toString()); } try{ JSONArray jArray = new JSONArray(storyObj); String storyNames = ""; for(int i = 0;i&lt;jArray.length();i++){ storyNames += jArray.getJSONObject(i).getString("story_name") +"\n"; } test.setText(storyNames); }catch(JSONException e) { Log.e("log_tag", "Error returning string "+e.toString()); } return; //and of openJson() } //end of class body } </code></pre> <p>hope you can help me with this, been taking so long to get it to work!!!</p> <p>added extra code from boris... is this where i put the code ?</p> <pre><code> try{ //connects to mySQL HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://10.0.2.2/textures_story_list.php"); HttpResponse response = client.execute(post); //captures the response entity = response.getEntity(); InputStream entityStream = entity.getcontent(); StringBuilder entityStringBuilder = new StringBuilder(); byte [] buffer = new byte[1024]; int bytesReadCount; while ((bytesReadCount = entityStream(read(buffer)) &gt; 0)) { entityStringBuilder.append(new String(buffer, 0, bytesReadCount)); } String entityString = entityStringBuilder.toString(); Integer responseInteger = Integer.valueOf(entityString); }catch(Exception e) { Log.e("log_tag", "Error in http connection "+e.toString()); } </code></pre> <p>if it is i get an error on read</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload