2013-10-21 45 views
0

在我的應用程序中,我有兩個從我的網站解析json的視圖,它工作的很棒。我最後一個問題是我在編輯文本框中輸入數字並將其附加到我的URL以將用戶添加到數據庫。這有效,但是當這個事件啓動時,我想解析這個json。現在它只是啓動網站。從onClick事件解析json

我的問題是我如何開始意圖而不是啓動網站的權利。我有一個JSONParser和兩個jason活動,效果很好。這是我的代碼。我還將編輯文本字段保存到我的SD卡上,以便用戶在回到該視圖時再次調用它。我知道如何解析json我只是不知道如何從onClick事件調用它到另一個視圖。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    SharedPreferences mysettings2 = PreferenceManager.getDefaultSharedPreferences(this); 
    String st1 = mysettings2.getString("THEME_PREF", "Blue"); 
    if(st1.equals("Blue")) 
     {setTheme(R.style.Theme_Holo_blue); } 
    if(st1.equals("Red")) 
     {setTheme(R.style.Theme_Holo_red); } 
    if(st1.equals("Green")) 
     {setTheme(R.style.Theme_Holo_green); } 
    if(st1.equals("Wallpaper")) 
     {setTheme(R.style.Theme_Transparent); } 
    if(st1.equals("Holo")) 
     {setTheme(R.style.Theme_Holo); } 

    setContentView(R.layout.getscore); 
    getActionBar().setBackgroundDrawable(
      getResources().getDrawable(R.drawable.divider)); 

     edttext= (EditText)findViewById(R.id.editText1); 
     Button tutorial2 = (Button) findViewById(R.id.button1); 
     tutorial2.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setData(Uri.parse("http://example.com/user/"+edttext.getText() +"?token=dYG8hW5TY4LBU8jfPb10D3IcsSx8RTo6")); 
      startActivity(intent); 

       try { 
        File myFile = new File("/sdcard/my_IdNumber_file.txt"); 
        myFile.createNewFile(); 
        FileOutputStream fOut = new FileOutputStream(myFile); 
        OutputStreamWriter myOutWriter = 
              new OutputStreamWriter(fOut); 
        myOutWriter.append(edttext.getText()); 
        myOutWriter.close(); 
        fOut.close(); 

       } catch (Exception e) { 

       } 
     } 
     }); 

      btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile); 
      btnReadSDFile.setOnClickListener(new OnClickListener() { 

public void onClick(View v) { 
    // write on SD card file data in the text box 
try { 
    File myFile = new File("/sdcard/my_IdNumber_file.txt"); 
    FileInputStream fIn = new FileInputStream(myFile); 
    BufferedReader myReader = new BufferedReader(
      new InputStreamReader(fIn)); 
    String aDataRow = ""; 
    String aBuffer = ""; 
    while ((aDataRow = myReader.readLine()) != null) { 
     aBuffer += aDataRow + "\n"; 
    } 
    edttext.setText(aBuffer); 
    myReader.close(); 

} catch (Exception e) { 

} 
    } 

這是我JSONParser活動

public class JSONParser { 

static InputStream is = null; 
static JSONArray jarray = null; 
static String json = ""; 

// constructor 
public JSONParser() { 

} 

public JSONArray getJSONFromUrl(String url) { 

     StringBuilder builder = new StringBuilder(); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     try { 
      HttpResponse response = client.execute(httpGet); 
      StatusLine statusLine = response.getStatusLine(); 
      int statusCode = statusLine.getStatusCode(); 

      if (statusCode == 200) { 
      HttpEntity entity = response.getEntity(); 
      InputStream content = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
      } 
      } else { 
      Log.e("==>", "Failed to download file"); 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    // try parse the string to a JSON object 
    try { 
     jarray = new JSONArray(builder.toString()); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jarray; 

}} 

回答

0

你想在開展活動的時間解析JSON然後解析它的onCreate();

+0

是的,這是我如何解析我的其他活動中的json。他們只是在活動啓動時啓動。此活動需要使用onClick將用戶添加到我的數據庫。 onClick是啓動它,現在只是訪問我的json代碼webview ..當這個事件啓動時,我該怎麼做? –

+0

意味着你想解析onClick?如果是的話點擊什麼? –

+0

做一件事情做一個setter和getter類,然後在發送onClick意圖之前解析你的json,並在那個時候在setter和getter類中設置所有這些值,並在啓動該活動時獲取所有這些數據。 –