gpt4 book ai didi

java - 安卓。如何使用改造发布原始数据或帮助使用 asynctask

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:27 26 4
gpt4 key购买 nike

我想发送 {"userid": "userid","pass":"1222"} 的 POST RAW 数据作为用户名和密码。我有一种由用户名和密码组成的布局,它将作为用户名和密码获取。我需要帮助来尝试改造

   // 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
}
}

请帮忙,因为我搜索了很多,但没有找到任何东西

最佳答案

您需要执行以下步骤:

  1. 网络API接口(interface)
 public interface NetworkAPI {
@GET("authentication.php")
@Headers({"Content-Type:application/json; charset=UTF-8"})
Call<JsonElement> loginRequest(@Body LoginRequest body);
}
  • 登录请求模型类
  • public class LoginRequest {
    String userid;
    String password;
    public LoginRequest(String userid, String password) {
    this. userid = userid;
    this. pass = pass;
    }
    }
  • 在您的 Activity 中进行改造调用
  • String baseUrl ="http://mark.journeytech.com.ph/mobile_api/";

    NetworkAPI networkAPI;

       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
    }
    });
    }

    关于java - 安卓。如何使用改造发布原始数据或帮助使用 asynctask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45409902/

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