在 Android studio 中将 API 密钥插入 POST 请求的位置

Where to insert API key into POST request in Android studio

只想知道我在下面的代码中为服务器插入 API 键的位置:

public class GetCurrentJob extends Job {

Context context;
GetFeedback feedback;


protected GetCurrentJob(Context context, GetFeedback fb) {
    super(new Params(PRIORITY.HIGH).requireNetwork());
    feedback = fb;
    this.context = context;
}

@Override
public void onAdded() {

}

@Override
public void onRun() throws Throwable {

    //POST feedback to server... require API key. How?
    Response<String> response = Ion.with(context)
            .load("POST", URLbuilder.getURL())
            .setStringBody(feedback.toJson())
            .asString()
            .withResponse()
            .get();


    //Toast.makeText(context, "post", Toast.LENGTH_SHORT).show();

    if (response.getHeaders().code() != HttpURLConnection.HTTP_OK) {
        Log.d("test", "error in request " + String.valueOf(response.getResult()));
        return;
    }
    else
    {
        Log.d("test", "success" + String.valueOf(response.getResult()));
    }


}

@Override
protected void onCancel(int cancelReason, @Nullable Throwable throwable) {

}

@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
    return null;
}
}

我的 URL 构建器 class:

public class URLbuilder {

private static final String SERVER = "http://jxapp-s-ticket.cloudapp.net/jxapp_ticket/upload/api/http.php/tickets.json";

public static String getURL(){
    return Uri.parse(SERVER).buildUpon().toString();
}
}

一点信息:

尽可能提供帮助!谢谢!

Response<String> response = Ion.with(context) 
.load("POST", URLbuilder.getURL()) 
.setHeader("x-api"," API KEY HERE ") 
.setStringBody(feedback.toJson()) 
.asString() 
.withResponse() 
.get();

如果有人遇到同样的问题,我就是这样解决的。 通过使用 API 键添加 header 来修改离子格式。 所有学分都归功于我的一位前辈。