2012-02-19 56 views
0

我有我想要一个BTN当点击保存JSON文件,然后另一个BTN将在一个TextView显示解码JSON文件保存JSON文件到内存然后调用该文件后

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 

String result = ""; 
InputStream is = null; 
TextView one = (TextView) findViewById(R.id.showView); 
HttpEntity entity = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    //pulls a file from my server then saves it 
    Button btn1 = (Button) findViewById(R.id.downloadBtn); 
    btn1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 


      saveJson(); 

     //end of onClick  
     } 
    //end of onClickListener 
    }); 

    //should show the saved file 
    Button btn2 = (Button) findViewById(R.id.showBtn); 
    btn2.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      showJson(); 

     //end of onClick  
     } 
    //end of onClickListener 
    }); 

//end of oncreate()  
} 

public void saveJson(){ 

    TextView one = (TextView) findViewById(R.id.showView); 

    try{ 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_list.php"); 

      HttpResponse response = httpClient.execute(httpPost); 
      entity = response.getEntity(); 
//from here what do i need to add to save the file as .json 
    }catch(Exception e) { 
     one.setText("error3"); 
    } 


//end of returnJson() 
} 

public void showJson(){ 

    //from here what should i have to show the file story_list.json 

try{ 



     is = entity.getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);      
     StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 

    }catch(Exception e) { 
     } 

    try{ 
     JSONArray jArray = new JSONArray(result); 
     String storyNames = ""; 
     for(int i = 0;i<jArray.length();i++){ 
       storyNames += jArray.getJSONObject(i).getString("story_name") + "\n"; 
     } 
     one.setText(storyNames); 
    } 
    catch(JSONException e) { 
    } 
     return; 


//end of returnJson() 
} 



//end of class body  
} 

它崩溃的startup_

runtimeExecption unable to instatiate activity ComponentInfo nullpointer 

确定在这里固定的问题是我加入到保存,然后显示该文件的代码

public class MainActivity extends Activity { 
/** Called when the activity is first created. */ 

String result = ""; 
InputStream is = null; 
HttpEntity entity = null; 
HttpEntity readString = null; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    //pulls a file from my server then saves it 
    Button btn1 = (Button) findViewById(R.id.downloadBtn); 
    btn1.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 


      saveJson(); 

     //end of onClick  
     } 
    //end of onClickListener 
    }); 

    //should show the saved file 
    Button btn2 = (Button) findViewById(R.id.showBtn); 
    btn2.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      showJson(); 

     //end of onClick  
     } 
    //end of onClickListener 
    }); 

//end of oncreate()  
} 

public void saveJson(){ 

    TextView one = (TextView) findViewById(R.id.showView); 

    try{ 
      HttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_list.php"); 

      HttpResponse response = httpClient.execute(httpPost); 
      entity = response.getEntity(); 
//from here what do i need to add to save the file as .json 
    }catch(Exception e) { 
     one.setText("error3"); 
    } 
    try{ 

     HttpEntity text = entity; 
     FileOutputStream fOut = openFileOutput("story.json",MODE_WORLD_READABLE); 
     OutputStreamWriter osw = new OutputStreamWriter(fOut); 
     osw.write(text); 
     osw.flush(); 
     osw.close(); 
     }catch(IOException ioe){ 
     } 

//end of returnJson() 
} 

public void showJson(){ 

    //from here what should i have to show the file story_list.json 

    try{ 
     FileInputStream fIn = openFileInput("story.json"); 
     InputStreamReader isr = new InputStreamReader(fIn); 
     char[] inputBuffer = new char[11]; 
     //len is the length of that saved string in the file 

     isr.read(inputBuffer); 

     // String readString = new String(inputBuffer); 





     isr = readString.getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);      
     StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 

    }catch(Exception e) { 
     } 

    try{ 
     JSONArray jArray = new JSONArray(result); 
     String storyNames = ""; 
     for(int i = 0;i<jArray.length();i++){ 
       storyNames += jArray.getJSONObject(i).getString("story_name") + "\n"; 
     } 
     TextView one = (TextView) findViewById(R.id.showView); 

     one.setText(storyNames); 
    } 
    catch(JSONException e) { 
    } 
     return; 


//end of returnJson() 
} 



//end of class body  
} 

有人可以帮助我解决错误或告诉我我做错了什么吗?

+0

嗨 - 你的第一个问题是访问冲突 - 你说你修复它。酷:)是否一切正常?如果不是,请特别说明出了什么问题。如果可能,剪切/粘贴错误消息。告诉我们什么具体行失败 - 以及它如何失败 - 当你通过调试器。如果你有一个问题 - 请不要让我们猜测。指定究竟是什么/在哪里。好? – paulsm4 2012-02-19 23:09:59

回答

1

两个建议:

1)将这种分配到您的onCreate()方法:

one = (TextView) findViewById(R.id.showView); 

2)如果你仍然有问题,然后一步步跟踪代码,以确定究竟在何处你”重试解引用空指针。我猜这可能是你的“findViewById()”调用之一。

+0

iv修复了这个问题,但仍然需要知道如何将代码放入以保存并显示json文件 – daniel 2012-02-19 06:31:24

相关问题