2013-10-21 2 views
0

내 응용 프로그램에서 내 웹 사이트에서 json을 구문 분석하는 두 가지보기가 있고 그것은 잘 작동합니다. 마지막 문제는 편집 텍스트 상자에 숫자를 입력하고 URL에 연결하여 사용자를 데이터베이스에 추가하는 것입니다. 그건 작동하지만 그 이벤트가 시작되면 그 json을 구문 분석하고 싶습니다. 지금은 그냥 웹 사이트를 시작합니다.onson 이벤트에서 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) { 

} 
    } 

이 당신이 다음()를에서 onCreate 분석 활동을 시작하는 시점에서 JSON을 구문 분석 할 내 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

이다;

+0

예. 다른 활동에서 내 json을 구문 분석하는 방법입니다. 활동이 시작될 때 막 시작됩니다. 이 액티비티에서는 데이터베이스에 사용자를 추가하려면 onClick이 필요합니다. onClick은 무엇을 시작하고 지금은 내 json 코드 webview에 액세스하고 있습니다. 그 이벤트가 시작될 때 어떻게해야합니까? –

+0

은 당신이 구문 분석 onClick을하고 싶어 의미 ?? 그리고 그렇다면 무엇을 클릭하면 ?? –

+0

한 가지 일은 setter 및 getter 클래스를 만들고 onClick에서 intention을 보내기 전에 json을 구문 분석하고 그 시간에 setter 및 getter 클래스의 모든 값을 설정하고 해당 활동을 시작하면 모든 데이터를 가져옵니다. –