Microsoft ProjectOxford Vision API错误

时间:2018-01-15 05:00:41

标签: java android microsoft-cognitive vision

我是android新手。我正在尝试开发Android应用程序我在youtube上寻找一个教程。我在youtube Analyze image上关注android教程中的每一步。但由于我是开发android的初学者,我在运行应用程序时遇到错误,它会自动停止。

有人愿意帮助吗?提前谢谢

这是我的代码

public class MainActivity extends AppCompatActivity {

public VisionServiceClient visionServiceClient = new VisionServiceRestClient("96a818770f0e45dfa09fc5cce0f1ce1e");

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

    Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.apple);
    ImageView imageView = (ImageView)findViewById(R.id.imageView);
    Button btnIdentify = (Button)findViewById(R.id.btnIdentify);

    imageView.setImageBitmap(mBitmap);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100,outputStream);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

    btnIdentify.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v){
           final AsyncTask<InputStream,String,String> visionTask = new AsyncTask<InputStream, String, String>() {
                ProgressDialog mDialog = new ProgressDialog(MainActivity.this);

                @Override
                protected String doInBackground(InputStream... params) {
                    try {
                        publishProgress("Recognizing...");
                        String[] features = {"Description"};
                        String[] details = {};

                        AnalysisResult result = visionServiceClient.analyzeImage(params[0], features, details);

                        String strResult = new Gson().toJson(result);
                        return strResult;
                    } catch (Exception e) {
                       return  null;
                    }
                }

                @Override
                protected void onPreExecute() {
                    mDialog.show();
                }

                @Override
                protected void onPostExecute(String s) {
                    mDialog.dismiss();

                    AnalysisResult result = new Gson().fromJson(s,AnalysisResult.class);
                    TextView textView = (TextView)findViewById(R.id.txtDescription);
                    StringBuilder stringBuilder = new StringBuilder();
                    for(Caption caption:result.description.captions)
                    {
                        stringBuilder.append(caption.text);
                    }
                    textView.setText(stringBuilder);
                }

                @Override
                protected void onProgressUpdate(String... values) {
                    mDialog.setMessage(values[0]);
                }
            };

            visionTask.execute(inputStream);
        }
    });

}

这是我遇到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.dev.sample, PID: 455
              java.lang.NullPointerException: Attempt to read from field 'com.microsoft.projectoxford.vision.contract.Description com.microsoft.projectoxford.vision.contract.AnalysisResult.description' on a null object reference
                  at com.dev.sample.MainActivity$1$1.onPostExecute(MainActivity.java:81)
                  at com.dev.sample.MainActivity$1$1.onPostExecute(MainActivity.java:50)
                  at android.os.AsyncTask.finish(AsyncTask.java:651)
                  at android.os.AsyncTask.access$500(AsyncTask.java:180)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7325)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

1 个答案:

答案 0 :(得分:0)

该错误意味着API调用失败,并且您正在尝试解析null json。

将您的代码更改为:

@Override
protected String doInBackground(InputStream...params) {
  try {
    publishProgress("Recognizing...");
    String[] features = {
      "Description"
    };
    String[] details = {};

    AnalysisResult result = visionServiceClient.analyzeImage(params[0], features, details);

    String strResult = new Gson().toJson(result);
    return strResult;
  } catch (Exception e) {
    return e.getMessage();
  }
}

@Override
protected void onPostExecute(String s) {
  mDialog.dismiss();
  StringBuilder stringBuilder = new StringBuilder();
  TextView textView = findViewById(R.id.txtDescription);
  try {
    AnalysisResult result = new Gson().fromJson(s, AnalysisResult.class);
    for (Caption caption: result.description.captions) {
      stringBuilder.append(caption.text);
    }
  } catch (Exception e) {
    stringBuilder.append(e.getCause());
  }
  textView.setText(stringBuilder);
}