я работаю над проектом Laravel & Android. Я уже сделал api с laravel и определил свой маршрут обновления и хорошо работаю с почтальоном. Route::put('/users/{id}','Api\UserController@update');
функция обновления
public function update (Request $req, $id)
{
try {
$user=User::find($id);
$user->name= $req->name;
$user->prenom= $req->prenom;
$user->adresse= $req->adresse;
$user->email= $req->email;
$user->numtel= $req->numtel;
$user->save();
return response()->json([
'status' => 'success',
'data' => $user
]);
} catch (Exception $e) {
return response()->json([
'status' => 'error',
'data' => 'error'
]);
}
}
В Android Studio я создаю интерфейс
String UPDPRO ="http://IP/PFE/public/api/";
@FormUrlEncoded
@PUT("users/{id}")
Call<String> UpdateUsers(
@Path("id") int id,
@Field("name") String name,
@Field("prenom") String prenom,
@Field("email") String email,
@Field("numtel") String numtel
);
}
и фрагмент профиля
public class Profile_Fragment extends Fragment implements View.OnClickListener {
private Button buttonupdate;
private EditText editTextName, editTextEmail, editTextPrenom, editTextTel;
private static View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.profile_fragment, container, false);
}
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
editTextName = (EditText) view.findViewById(R.id.editTextName);
editTextEmail = (EditText) view.findViewById(R.id.editTextEmail);
editTextPrenom = (EditText) view.findViewById(R.id.editTextPrenom);
editTextTel = (EditText) view.findViewById(R.id.editTextTel);
view.findViewById(R.id.buttonupdate).setOnClickListener(this);
}
private void updateUsers() {
String name = editTextName.getText().toString().trim();
String email = editTextEmail.getText().toString().trim();
String prenom = editTextPrenom.getText().toString().trim();
String numtel = editTextTel.getText().toString().trim();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(UpdateProfile.UPDPRO)
.addConverterFactory(ScalarsConverterFactory.create())
.build();
UpdateProfile api = retrofit.create(UpdateProfile.class);
Call<String> call = api.UpdateUsers(getId(), name, email, prenom, numtel);
call.enqueue(new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
if (response.isSuccessful()) {
if (response.body() != null) {
Log.i("Responsestring", response.body().toString());
Log.i("onSuccess", response.body().toString());
String jsonresponse = response.body().toString();
try {
JSONObject jsonObject = new JSONObject(jsonresponse);
if (jsonObject.getString("status").equals("success")) {
Toast.makeText(getActivity(), "Update Successfully!", Toast.LENGTH_SHORT)
.show();
} else if (jsonObject.getString("status").equals("error")) {
/* new CustomToast().Show_Toast(getActivity(), view,
" " + jsonObject.getString("message"));*/
Toast.makeText(getActivity(), "Update failed!", Toast.LENGTH_SHORT)
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
@Override
public void onFailure(Call<String> call, Throwable t) {
Toast.makeText(getContext(), "Server invalid \n" + t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
@Override
public void onClick(View view) {
if (view == buttonupdate) {
updateUsers();
}
}
}
Макет профиля
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Nom"
android:inputType="text" />
<EditText
android:id="@+id/editTextPrenom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Prénom"
android:inputType="text" />
<EditText
android:id="@+id/editTextEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editTextTel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Téléphone"
android:inputType="text" />
<Button
android:id="@+id/buttonupdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Update" />
</LinearLayout>
</RelativeLayout>
Когда я устанавливаю новую информацию, ничего не меняется в mysql базу данных. любое решение?