简体   繁体   中英

How to Use Azure Cognitive System?

I'm trying to connect my Android app to Azure Voice Identification Cognitive System.

I used many ways but every time I get null in "RUN".

First time, I used the code below.

Second time, I downloaded library from Github but I get the same output.

package com.example.api;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.net.URI;


public class MainActivity extends AppCompatActivity {
Button bb;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     bb=(Button)findViewById(R.id.bb);
     bb.setOnClickListener(
     new View.OnClickListener() 
     {
         @Override
         public void onClick(View v) {
            HttpClient httpclient = HttpClients.createDefault();

            try
            {
                URIBuilder builder = new URIBuilder("https://westus.api.cognitive.microsoft.com/spid/v1.0/identificationProfiles");
                /*AsyncHttpClient client = new AsyncHttpClient();
                client.post()
                "https://westus.api.cognitive.microsoft.com/spid/v1.0/identificationProfiles",
                new AsyncHttpResponseHandler() {}*/


                URI uri = builder.build();
                HttpPost request = new HttpPost(uri);
                request.setHeader("Content-Type", "application/json");
                request.setHeader("Ocp-Apim-Subscription-Key", getString(R.string.record));


                // Request body
                StringEntity reqEntity = new StringEntity(getString(R.string.loc));
                request.setEntity(reqEntity);


                HttpResponse response = httpclient.execute(request);
                HttpEntity entity = response.getEntity();

                if (entity != null)
                {
                    Log.i("",EntityUtils.toString(entity));
                }
            }
            catch (Exception e)
            {
                System.out.println(e.getMessage());
            }
        }
     });
    }
}

Manifests:

   <uses-permission
 android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

     <uses-permission android:name="android.permission.RECORD_AUDIO"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


     <application

         android:allowBackup="true"
         android:icon="@mipmap/ic_launcher"
         android:label="@string/app_name"
         android:roundIcon="@mipmap/ic_launcher_round"
         android:supportsRtl="true"
         android:theme="@style/AppTheme">
         <uses-library
             android:name="org.apache.http.legacy"
             android:required="false" />
         <activity android:name=".MainActivity">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />

                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
     </application>

 </manifest>

I expect the output response 200 or 500 ; however, but the actual output is:

I/System.out: null

EntityUtils.toString(entity) returns the entity.getContent() --which may be null/empty even if entity is not.

Check the reply from response.getStatusLine() first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM