class 切换后 startActivity 不工作
startActivity not working after class switch
我想启动 activity 并在解析 JSON 响应后发出 Toast 消息。
public class Sign_Up extends AppCompatActivity {
....
register();
public void register()
{
final String url = "someURL";
new Json().checkJsonFile(url,getApplicationContext());
}
//Now in an non-activity
public class Json {
public void checkJsonFile(final String url, final Context context) {
new Thread(new Runnable() {
public void run() {
String result;
String line;
try {
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
boolean redirect = false;
// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
if (redirect) {
// get redirect url from "location" header field
String newUrl = conn.getHeaderField("Location");
// get the cookie if need, for login
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
}
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
result = sb.toString();
new Sign_Up().parseJSON(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
//Back in Sign_Up class
public void parseJSON(String JSON){
int error = -1;
try {
JSONObject jsonObject = new JSONObject(JSON);
error = Integer.parseInt(jsonObject.getString("error_code"));
} catch (JSONException e) {
//Retry
register();
e.printStackTrace();
}
progressDialog.dismiss();
switch (error) {
case 0: //Successful
finish();
startActivity(new Intent(Sign_Up.this, Loading.class));
break;
case 1:
Toast.makeText(getApplicationContext(), "blabla", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "other blabla", Toast.LENGTH_SHORT).show();
break;
default:
register();
break;
}
}
但是,当我将 Json 内容放入 Sign_Up class 时,至少 startActivity 有效,但 Toast 无效。同样,我想将 Json 的内容放在单独的 class.
中
非常感谢您!
您不能通过调用
创建 Activity
new MyActivity()
并期望一切正常。行
new Sign_Up().parseJSON(result);
正在错误地创建 Sign_Up
activity 的新实例,因此它未绑定到上下文,并且将执行其生命周期调用的 none。您无法在该状态下显示 Toast。
更好的解决方案是将接口从 Sign_Up
传递到 Json
class 以让它回调到原始 Sign_Up
实例,而不是尝试创建一个新实例。例如:
class Json {
public interface Callback {
public void run(String result);
}
private Callback callback
Json(Callback c) {
callback = c;
}
// later, replace new Sign_Up().parseJSON(result) with callback.run(result)
}
并创建它替换
new Json().checkJsonFile(url,getApplicationContext());
和
new Json(new Json.Callback() {
@Override
public void run(String result) {
parseJSON(result);
}
}).checkJsonFile(url,getApplicationContext());
您还需要确保所有 UI 操作(toast 生成)都在 UI 线程上 运行。
我想启动 activity 并在解析 JSON 响应后发出 Toast 消息。
public class Sign_Up extends AppCompatActivity {
....
register();
public void register()
{
final String url = "someURL";
new Json().checkJsonFile(url,getApplicationContext());
}
//Now in an non-activity
public class Json {
public void checkJsonFile(final String url, final Context context) {
new Thread(new Runnable() {
public void run() {
String result;
String line;
try {
URL obj = new URL(url);
HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
conn.setReadTimeout(5000);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
boolean redirect = false;
// normally, 3xx is redirect
int status = conn.getResponseCode();
if (status != HttpURLConnection.HTTP_OK) {
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER)
redirect = true;
}
if (redirect) {
// get redirect url from "location" header field
String newUrl = conn.getHeaderField("Location");
// get the cookie if need, for login
String cookies = conn.getHeaderField("Set-Cookie");
// open the new connnection again
conn = (HttpURLConnection) new URL(newUrl).openConnection();
conn.setRequestProperty("Cookie", cookies);
conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
conn.addRequestProperty("User-Agent", "Mozilla");
conn.addRequestProperty("Referer", "google.com");
}
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
while ((line = in.readLine()) != null) {
sb.append(line);
}
in.close();
result = sb.toString();
new Sign_Up().parseJSON(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
//Back in Sign_Up class
public void parseJSON(String JSON){
int error = -1;
try {
JSONObject jsonObject = new JSONObject(JSON);
error = Integer.parseInt(jsonObject.getString("error_code"));
} catch (JSONException e) {
//Retry
register();
e.printStackTrace();
}
progressDialog.dismiss();
switch (error) {
case 0: //Successful
finish();
startActivity(new Intent(Sign_Up.this, Loading.class));
break;
case 1:
Toast.makeText(getApplicationContext(), "blabla", Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(getApplicationContext(), "other blabla", Toast.LENGTH_SHORT).show();
break;
default:
register();
break;
}
}
但是,当我将 Json 内容放入 Sign_Up class 时,至少 startActivity 有效,但 Toast 无效。同样,我想将 Json 的内容放在单独的 class.
中非常感谢您!
您不能通过调用
创建 Activitynew MyActivity()
并期望一切正常。行
new Sign_Up().parseJSON(result);
正在错误地创建 Sign_Up
activity 的新实例,因此它未绑定到上下文,并且将执行其生命周期调用的 none。您无法在该状态下显示 Toast。
更好的解决方案是将接口从 Sign_Up
传递到 Json
class 以让它回调到原始 Sign_Up
实例,而不是尝试创建一个新实例。例如:
class Json {
public interface Callback {
public void run(String result);
}
private Callback callback
Json(Callback c) {
callback = c;
}
// later, replace new Sign_Up().parseJSON(result) with callback.run(result)
}
并创建它替换
new Json().checkJsonFile(url,getApplicationContext());
和
new Json(new Json.Callback() {
@Override
public void run(String result) {
parseJSON(result);
}
}).checkJsonFile(url,getApplicationContext());
您还需要确保所有 UI 操作(toast 生成)都在 UI 线程上 运行。