在我的应用程序中,我有两个视图从我的网站解析json,它的效果很好。我的最后一个问题是我在编辑文本框中输入一个数字并将其附加到我的URL以将用户添加到数据库。这有效,但当该事件启动时,我想解析那个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 :(得分:0)
你想在启动活动时解析json然后解析onCreate();