修改密码(代码如下)
package person;
import static baseinfo.Connectinfo.xiugaimimaurl;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.login_register.Login;
import com.example.login_register.R;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class Change_pswd extends AppCompatActivity {
EditText et1,et2;
Button button;
OkHttpClient okHttpClient;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_change_pswd);
et1=findViewById(R.id.oldPassword);
et2=findViewById(R.id.newPassword);
button=findViewById(R.id.change_btn);
okHttpClient = new OkHttpClient.Builder().build();
handler = new Handler(Looper.getMainLooper());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String getmima1 = et1.getText().toString();
String getmima2 = et2.getText().toString();
Map<String,String> data = new HashMap<>();
data.put("oldPassword",getmima1);
data.put("newPassword",getmima2);
String json = new Gson().toJson(data);
SharedPreferences sp = getSharedPreferences("token数据",MODE_PRIVATE);
String token = sp.getString("token","");
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"),json);
Request request = new Request.Builder()
.put(requestBody)
.addHeader("Authorization",token)
.url(xiugaimimaurl)
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
final String data = response.body().string();
handler.post(new Runnable() {
@Override
public void run() {
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(data);
} catch (JSONException e) {
e.printStackTrace();
}
if (jsonObject.optString("code").equals("200")){
Toast.makeText(Change_pswd.this, "修改成功!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Change_pswd.this, Login.class));
}else {
Toast.makeText(Change_pswd.this, jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/oldPassword"
android:layout_margin="20dp"
android:hint="请输入原密码"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/newPassword"
android:layout_margin="20dp"
android:hint="请输入旧密码"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/change_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:background="@color/blue"
android:textColor="@color/white"
android:textSize="30dp"
android:text="修改"
/>
</LinearLayout>