2

Since Wit.AI API for android is deprecated i'm trying to use its http api to get a result in json from my wit.ai app. I don't know if it's possible but i'm trying to send an http request from my android app in this way:

public class MainActivity extends AppCompatActivity {

String addressM = "https://api.wit.ai/message?v=20160526";
String accessToken = "BODF5LOCAIPJZI37KTWQKELNUR26ZMDH";
String header = "Authorization: Bearer ";
String query = "q";
String message = "partita";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    CallAPI runner = new CallAPI();
    runner.execute(addressM);
}

public class CallAPI extends AsyncTask<String, String, String> {

    public CallAPI() {
        //set context variables if required
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }


    @Override
    protected String doInBackground(String... params) {
        System.out.println("Running........................");

        String urlString = params[0]; // URL to call
        String resultToDisplay = "";

        InputStream in = null;
        try {

            URL url = new URL(urlString);

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestProperty(header, accessToken);
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty(query, message);
            in = new BufferedInputStream(urlConnection.getInputStream());


        } catch (Exception e) {

            System.out.println(e.getMessage());

            return e.getMessage();

        }

        try {
            resultToDisplay = IOUtils.toString(in, "UTF-8");
            //to [convert][1] byte stream to a string
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultToDisplay;
    }


    @Override
    protected void onPostExecute(String result) {
        //Update the UI
    }
}

I'm following this doc and i'm trying to send a simple "message request". So, i have to put a header in my url containing my auth token and then put my request in it. When sending my request i should get back a json and also an inbox message in my wit.ai app, but i'm not gettin anything (at the moment i don't really care for the json, but just for the inbox message to be sure my request has been sent). I copied the above code from another stack overflow question. I set my manifest correctly with internet permissions. Can someone help me?

Lucas
  • 35
  • 1
  • 5
  • Have you tried to use something like Retrofit? Or just OkHttp? They make POST requests easier, plus no need for AsyncTask – OneCricketeer Jun 10 '16 at 13:59
  • If not, then your code is doing nothing with the potential result you are reading back from the server... So, maybe try putting some code into onPostExecute – OneCricketeer Jun 10 '16 at 14:01
  • So couldnt do it in the way you told me, but i found this http://stackoverflow.com/questions/35358232/wit-ai-how-to-send-request-in-java, and it should work, i'm trying to get a result now – Lucas Jun 10 '16 at 15:05
  • Don't focus on "wit.ai" so much. All you need to understand is how to [make a POST request](http://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily). Which is simpler with [OkHttp](https://github.com/square/okhttp/wiki/Recipes#posting-form-parameters) – OneCricketeer Jun 10 '16 at 15:10
  • 1
    Also, too late now, but you should have removed your access token from the code before you posted it – OneCricketeer Jun 10 '16 at 15:11

1 Answers1

0

Change lines 4,5 to these:

String accessToken = "Bearer BODF5LOCAIPJZI37KTWQKELNUR26ZMDH";
String header = "Authorization";

And it should work.

Ravi Teja
  • 377
  • 4
  • 15