gpt4 book ai didi

java - 将变量从 Android 客户端发布到 php 服务器时出错

转载 作者:行者123 更新时间:2023-12-02 04:21:26 26 4
gpt4 key购买 nike

我想开展一项更改用户个人资料的 Activity 。接收到的基于 JSON 的部分工作正常。我已经在 PHP 代码中手动设置变量进行了测试。但是,当我将变量从 android 发布到 php 时,它无法接收它。谁能告诉我问题所在吗?

public class ProfilActivity extends AppCompatActivity {

private EditText editTextNama, editTextEmail, editTextPassword, editTextNohp;
private Button Simpan;
private static final String PROFIL_URL = "http://vrai-dev.000webhostapp.com/koneksi_profil.php";

List<Profil> profilList;

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

Button back = (Button) findViewById(R.id.profil_back);
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(ProfilActivity.this, MenuActivity.class);
startActivity(i);
}
});

profilList = new ArrayList<>();

getEmail();

editTextNama = (EditText) findViewById(R.id.profil_username);
editTextEmail = (EditText) findViewById(R.id.profil_email);
editTextPassword = (EditText) findViewById(R.id.profil_password);
editTextNohp = (EditText) findViewById(R.id.profil_nohp);

Simpan = (Button) findViewById(R.id.profil_simpan);
Simpan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
updateProfil();
} catch (IOException e) {
e.printStackTrace();
}
}
});

}

private void getEmail(){
final String email = "a";
StringRequest stringRequest = new StringRequest(Request.Method.POST, PROFIL_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loadProfil();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("email", email);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}

private void loadProfil() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, PROFIL_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray profil = new JSONArray(response);

JSONObject profilObject = profil.getJSONObject(0);

String foto_user = profilObject.getString("foto_user");
String username = profilObject.getString("username");
String email = profilObject.getString("email");
String password = profilObject.getString("password");
String nohp = profilObject.getString("nohp");

Profil viewProfil = new Profil(foto_user, username, email, password, nohp);
profilList.add(viewProfil);

editTextNama.setText(username);
editTextEmail.setText(email);
editTextPassword.setText(password);
editTextNohp.setText(nohp);

} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ProfilActivity.this, error.getMessage() + "Error Load Profil", Toast.LENGTH_LONG).show();
}
});
Volley.newRequestQueue(this).add(stringRequest);
}

最佳答案

遇到同样的问题无法找出问题所在,但这有效,不要忘记在 gradle 文件中添加依赖项

        RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

StringRequest postRequest = new StringRequest(com.android.volley.Request.Method.POST, YOUR_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// do work here
} catch (JSONException e) {
e.printStackTrace();
Log.d("Response", "failed: " + response);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
}
) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
//add your parameters here as key-value pairs
params.put("title", title);
return params;
}
};
queue.add(postRequest);

** 实现 'com.he5ed.lib:volley:android-cts-5.1_r4'**

关于java - 将变量从 Android 客户端发布到 php 服务器时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56634461/

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