0

Just want to know where do I insert the API key for the server in my code below:

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

My URLbuilder 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();
}
}

Just a little information:

  • My app takes user feedback and returns the feedback to the server, then the server will generate a ticket to the user.

  • I am able to generate a response from the server, namely from the log. But the log generates: "error in request Valid API key required".

  • I need a way to insert the API key but I do not know how (am quite new to Android Studio as well as POST and GET operations)!

Do help if possible! Thanks!

Positive-One
  • 89
  • 1
  • 12
  • How does your server takes the API key? usually if it's an API Key you just send in query paramater – Coder Feb 24 '17 at 06:45
  • hey hi! thanks for the reply. ive got it answer from a fellow senior of mine. the way i solved it is yes, by a query parameter by editing ion like this: `Response response = Ion.with(context) .load("POST", URLbuilder.getURL()) .setHeader("x-api"," API KEY HERE ") .setStringBody(feedback.toJson()) .asString() .withResponse() .get();` – Positive-One Feb 24 '17 at 08:06

1 Answers1

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

Should anyone encounter the same problem this is how i solved this. Modified the ion formatting by adding a header with the API key. All credits to a senior of mine.

Positive-One
  • 89
  • 1
  • 12