gpt4 book ai didi

php - 连接错误的 json 异常

转载 作者:行者123 更新时间:2023-11-29 11:21:03 25 4
gpt4 key购买 nike

我已经使用 php 和 json 创建了一个登录页面,但是当我离线并使用该程序而不是显示错误时,该程序被关闭。我如何显示此消息:“请检查您的网络连接”?现在它说不幸的是start1已停止。

Android 代码:

public class Login extends Activity {

EditText user,pass;
Button btnReg;
private static String url = "http://akosamanus.xzn.ir/Login.php";
private ProgressDialog pd;
JSONParser jparser = new JSONParser();
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);


user = (EditText)findViewById(R.id.etgetuser);
pass = (EditText)findViewById(R.id.etgetpass);
btnReg = (Button)findViewById(R.id.btnReg);

btnReg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

new loadperson().execute();

}
});



}

class loadperson extends AsyncTask<String, String, String> {




@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(Login.this);
pd.setMessage("login");

pd.show();
}

@Override
protected String doInBackground(String... strings) {


int success;
String username = user.getText().toString();
String password = pass.getText().toString();

try {

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username",username));
params.add(new BasicNameValuePair("password",password));

Log.d("request!", "starting");

JSONObject json = jparser.makeHttpRequest(url, "POST", params);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Successfully Login!", json.toString());



Intent ii = new Intent(Login.this,com.example.eagle.start1.SelectPage.class);
ii.putExtra("UserName", username);
startActivity(ii);
return json.getString(TAG_MESSAGE);
}else{

return json.getString(TAG_MESSAGE);

}
} catch (JSONException e) {
e.printStackTrace();

}



return null;
}

@Override
protected void onPostExecute(String message) {

pd.dismiss();
if (message != null){
Toast.makeText(Login.this, message, Toast.LENGTH_SHORT).show();
}
}
}

最佳答案

public class ConnectionDetector {

public boolean isConnectingToInternet(Context context) {

boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;

ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}

if (haveConnectedWifi || haveConnectedMobile) {
try {
URL url = new URL("https://www.google.com");
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "delete");
urlc.setConnectTimeout(1000 * 5); // mTimeout is in seconds
urlc.connect();
if (urlc.getResponseCode() == 200) {
return true;
} else {
return false;
}
} catch (Exception e) {
Log.e("internet error", "" + e);
return false;
}
} else {
return false;
}
}
}

并在 doInBackground 方法中使用 isConnectingToInternet(context) 如果为 true,则继续后台任务,否则返回一些有意义的内容,但未定义网络。

关于php - 连接错误的 json 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38930818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com