我想将{"userid": "userid","pass":"1222"}
的POST RAW数据作为用户名和密码发送。我有一个由用户名和密码组成的布局,将作为用户ID和密码获取。我需要帮助才能尝试改装
// Triggers when LOGIN Button clicked
public void checkLogin(View arg0) {
// Initialize AsyncLogin() class with userid and password
new REST().execute();
}
public class REST extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// The Username & Password
final EditText usr = (EditText) findViewById(R.id.username);
userid = (String) usr.getText().toString();
final EditText pw = (EditText) findViewById(R.id.password);
password = (String) pw.getText().toString();
}
@Override
protected Void doInBackground(Void... params) {
HttpURLConnection urlConnection=null;
String json = null;
// -----------------------
try {
HttpResponse response;
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("username", usr);
jsonObject.accumulate("password", password);
json = jsonObject.toString();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://mark.journeytech.com.ph/mobile_api/authentication.php");
httpPost.setEntity(new StringEntity(json, "UTF-8"));
httpPost.setHeader("Content-Type", "application/json");
httpPost.setHeader("Accept-Encoding", "application/json");
httpPost.setHeader("Accept-Language", "en-US");
response = httpClient.execute(httpPost);
String sresponse = response.getEntity().toString();
Log.w("QueingSystem", sresponse);
Log.w("QueingSystem", EntityUtils.toString(response.getEntity()));
}
catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
} finally {
// nothing to do here
}
return null;
}
@Override
protected void onPostExecute(Void result) {
Toast.makeText(getApplicationContext(), email + " "+ password, Toast.LENGTH_SHORT).show();
if (result != null) {
// do something
} else {
// error occured
}
}
请任何帮助,因为我经常搜索并且没有达成任何帮助
答案 0 :(得分:0)
首先:你应该创建你的api界面
public interface Api
{
@Headers({"Accept: application/json"})
@FormUrlEncoded
@POST("authentication.php")
Call<Void> Login(@Field("[email]") String email,
@Field("[password]") String password);
}
第二:在您的活动中,您应该调用您的函数
void Login(String email, final String password)
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://mark.journeytech.com.ph/mobile_api/")
.addConverterFactory(GsonConverterFactory.create())
.build();
Apiservice = retrofit.create(Api.class);
Call<Void> call = service.Login(email, password);
call.enqueue(new Callback<Void>()
{
@Override
public void onResponse(Call<Void> call, Response<Void> response)
{
if (response.isSuccess())
{
}
else
{
}
}
@Override
public void onFailure(Call<Void> call, Throwable t)
{
}
});
}
答案 1 :(得分:0)
您需要按照以下步骤操作:
public class LoginRequest { String userid; String password; public LoginRequest(String userid, String password) { this. userid = userid; this. pass = pass; } }
public void loginRequest(){ Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .build(); networkAPI = retrofit.create(NetworkAPI.class); LoginRequest loginRequest = new LoginRequest(yourusername,yourpassword); Call<JsonElement> call = networkAPI.loginRequest(loginRequest); call.enqueue(new Callback<JsonElement>() { @Override public void onResponse(Call<JsonElement> call, Response<JsonElement> response) { // success response } @Override public void onFailure(Call<JsonElement> call, Throwable t) { // failure response } }); }
String baseUrl =&#34; http://mark.journeytech.com.ph/mobile_api/" ;;
NetworkAPI networkAPI;
{{1}}