有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么HTTP请求不起作用?机智人工智能

我想分析我的句子。当我发送字符串“where is the door”(门在哪里)时,wit.ai应该用一个JSON来回答,其中包括我的句子的意图:导航。但是不幸的是,wit.ai日志说没有请求进入。我做错了什么?参数是正确的,但它们的顺序可能是错误的

public class MainActivity extends AppCompatActivity {

    String addressM = "https://api.wit.ai/message";
    String accessToken = "xxxxxxxxxxxxxxxxxxxxccc";
    String header = "Authorization: Bearer ";
    String query = "q";
    String message = "where is the door";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final Button button = (Button) findViewById(R.id.button);
        final TextView textView = (TextView) findViewById(R.id.textView);


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new JSONTask().execute(addressM);
            }
        });

    }

    private class JSONTask extends AsyncTask<String, String, String >{
        @Override
        protected String doInBackground(String... params) {

            HttpURLConnection connection = null;
            BufferedReader reader = null;
            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestProperty("access_token", "xxxxxxxxxxxxxxxxxxxxxxx");
                connection.setRequestProperty("q", message);
                connection.setRequestMethod("POST");
                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();

                String line = "";
                while ((line = reader.readLine()) !=null){
                    buffer.append(line);
                }
                return buffer.toString();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null){
                    connection.disconnect();
                }
                try {
                    if (reader != null){
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            TextView textView = (TextView) findViewById(R.id.textView);
            textView.setText(result);
            Toast.makeText(MainActivity.this, result,
                    Toast.LENGTH_LONG).show();
        }
    }

}

下面是一个示例,说明响应应该是什么。与我相关的是,目的是导航

  [
    {
    "entities":
    {
    "intent":
    [
    {
    "confidence":
    0.92597581019421
    "value":
    {
    "value":
    "navigation"
    }
    "entity":
    "intent"
    }
    ]
    }
    "confidence":
    null
    "_text":
    "where is the door"
    "intent":
    "default_intent"
    "intent_id":
    "default_intent_id"
    }
    ]

共 (1) 个答案

  1. # 1 楼答案

    您应该将参数作为查询参数传递,而不是作为HTTP头传递。此外,访问令牌应该在authorizationHTTP头中传递

    试试这个:

    Uri uri = Uri.parse(addressM)
      .buildUpon()
      .appendQueryParameter("q", message)
      .build();
    URL url = new URL(uri.toString());
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Authorization", "Bearer " + myAccessToken);
    connection.setRequestMethod("POST");
    connection.connect();
    

    如果您想使用HTTP API使您的生活更轻松,请考虑使用改装(我还没有试过看看WITE的API是否对它友好),或者至少尝试OkHttp

    static final String MESSAGE_BASE_URL = "https://api.wit.ai/message";
    static final String MY_ACCESS_TOKEN = "...";
    
    private final OkHttpClient client = new OkHttpClient();
    
    ...
    HttpUrl url = HttpUrl.parse(MESSAGE_BASE_URL)
      .newBuilder()
      .addQueryParameter("q", message)
      .build();
    Request request = new Request.Builder()
      .url(url)
      .addHeader("authorization", "Bearer " + MY_ACCESS_TOKEN)
      .build();
    Response response = client.newCall(request).execute();
    return response.body().string();