IBM Watson 有一个 Visual Recognition 服务:http ://www.ibm.com/watson/developercloud/visual-recognition.html
这是用于测试对 API 的调用的 API 资源管理器:http: //www.ibm.com/watson/developercloud/visual-recognition/api/v3/
文档中提供了教程和 API 参考。
我正在尝试使用该服务来通过 android 检索 JSON 数据响应。但是,调试后,它一直显示错误 404。我完全不知道如何解决这个问题。
如果您要运行以下代码,请将api_key
Main_activity.java 中的替换为您自己的密钥。当您在 IBM Bluemix 上为 IBM Watson 的 Visual Recognition 服务创建自己的服务时,会生成此密钥。
这是我的 MainActivity.java 文件:
package com.innam.httpapptotestrestservice;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
TextView etResponse;
TextView tvIsConnected;
TextView imageRespone;
ImageView imgPreview;
// VideoView vidPreview;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private Uri fileUri;
static String IMAGE_DIRECTORY_NAME = "Watson";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get reference to the views
etResponse = (TextView) findViewById(R.id.etResponse);
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
imgPreview = (ImageView) findViewById(R.id.imgPreview);
imageRespone = (TextView) findViewById(R.id.ImgResponse);
MediaController mediaController = new MediaController(this);
// mediaController.setAnchorView(vidPreview);
// vidPreview.setMediaController(mediaController);
// check if you are connected or not
if (isConnected()) {
tvIsConnected.setBackgroundColor(0xFF00CC00);
tvIsConnected.setText("You are conncted");
} else {
tvIsConnected.setText("You are NOT conncted");
}
// call AsynTask to perform network operation on separate thread
new HttpAsyncTaskOne().execute();
}
public static String GET(String url) {
InputStream inputStream = null;
String result = "";
try {
// create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// make GET request to the given URL
HttpResponse httpResponse = httpclient.execute(new HttpGet(url));
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
public boolean isConnected() {
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
private class HttpAsyncTaskOne extends AsyncTask<String, Void, String>
{
ProgressDialog pd;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "Hi!", "Please wait your response is about to generate");
}
@Override
protected String doInBackground(String... urls) {
//return GET(urls[0]);
return getResponseOne();
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
pd.dismiss();
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
String formattedResult="";
String classifer_id="" ;
String name="";
String owner="";
String status="";
String created ="";
String classesobj="";
try{
JSONObject jObj = new JSONObject(result);
classifer_id = jObj.optString("classifier_id");
name = jObj.optString("name");
owner = jObj.optString("owner");
status = jObj.optString("status");
created = jObj.optString("created");
JSONArray jsonArray = jObj.optJSONArray("classes");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++)
{
classesobj = jsonArray.getJSONObject(i).optString("class");
//classesobj += jObj.optString("class");
}
formattedResult = "{ \n" + " classifer_id: "+ classifer_id
+ ",\n name: "+ name
+ ",\n owner: "+ owner
+ ",\n status: "+ status
+ ",\n created: "+ created
+ ",\n classes: [\n"+
"{ class: " + classesobj + "}\n" +
" ] \n }";
}
catch(JSONException e) {
e.printStackTrace();
}
etResponse.setText(formattedResult);
}
}
/**
* https://watson-api-explorer.mybluemix.net/visual-recognition/api/v3/classifiers?
* api_key=*****
* @return
*/
private String getResponseOne() {
//String endPointUrl = AppConstants.BASE_URL + "";
/*String params = "api_key=*********" +
"&url=http://thumbs.dreamstime.com/t/orange-apple-banana-isolated-white-background-51942284.jpg" +
"&classifier_ids=default,fruits_960559343" +
"&owners=me,IBM" +
"&threshold=0.2" +
"&version=2016-05-20";*/
String postParams = "api_key=**********" +
"&version=2016-05-19" +
"&negative_examples=negative_examples.zip" +
"&classname_positive_examples=baypay_positive_examples.zip&name=baypay";
//String url = "https://gateway-a.watsonplatform.net/visual-recognition/api/v3/classify?";
String url = "https://watson-api-explorer.mybluemix.net/visual-recognition/api/v3/classifiers?"+postParams;
// String params ="api_key=********" +
// "version=2016-05-19";
//
// String url = "https://watson-api-explorer.mybluemix.net/visual-recognition/api/v3/classifiers/baypay_309108553?" ;
ServiceHandler serviceHandler = new ServiceHandler(this);
try {
// return serviceHandler.doPost(url, params);
return serviceHandler.doGet(url);
} catch (IOException e) {
e.printStackTrace();
}
return "Response Not returned";
}
public void openCamera(View v)
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
/**
* Launching camera app to record video
*/
// public void openVideo(View v) {
// Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//
// fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
//
// // set video quality
// intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
//
// intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
// // name
//
// // start the video capture Intent
// startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
// }
String photoPath = "";
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) //what to do once the image has been captured
{
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// launching upload activity
//launchUploadActivity(true);
// bimatp factory
photoPath = fileUri.getPath();
BitmapFactory.Options options = new BitmapFactory.Options();
// down sizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
imgPreview.setVisibility(View.VISIBLE);
// vidPreview.setVisibility(View.GONE);
imgPreview.setImageBitmap(bitmap);
//next parse result string
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
}
// else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
// if (resultCode == RESULT_OK) {
//
// // video successfully recorded
// // launching upload activity
//
// imgPreview.setVisibility(View.GONE);
// vidPreview.setVisibility(View.VISIBLE);
// vidPreview.setVideoPath(fileUri.getPath());
// // start playing
// vidPreview.start();
//
//
// } else if (resultCode == RESULT_CANCELED) {
//
// // user cancelled recording
// Toast.makeText(getApplicationContext(),
// "User cancelled video recording", Toast.LENGTH_SHORT)
// .show();
//
// } else {
// // failed to record video
// Toast.makeText(getApplicationContext(),
// "Sorry! Failed to record video", Toast.LENGTH_SHORT)
// .show();
// }
// }
}
@Override
protected void onPause() {
super.onPause();
// if(vidPreview != null && vidPreview.isPlaying())
// {
// vidPreview.pause();
// }
}
@Override
protected void onDestroy() {
super.onDestroy();
// if(vidPreview != null && vidPreview.isPlaying())
// {
// vidPreview.stopPlayback();
// }
}
/**
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/**
* returning image / video
*/
private static File getOutputMediaFile(int type) {
// External sdcard location
File mediaStorageDir = new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("IMG Capture", "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
}
// else if (type == MEDIA_TYPE_VIDEO) {
// mediaFile = new File(mediaStorageDir.getPath() + File.separator
// + "VID_" + timeStamp + ".mp4");
// }
else {
return null;
}
return mediaFile;
}
public void uploadImage(View view)
{
new HttpAsyncTaskTwo().execute();
}
private class HttpAsyncTaskTwo extends AsyncTask<String, Void, String>
{
ProgressDialog pd;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "Uploading image!", "Please Wait..");
}
@Override
protected String doInBackground(String... urls)
{
return getResponseTwo();
}
@Override
protected void onPostExecute(String result)
{
pd.dismiss();
Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
imageRespone.setText(result);
}
}
// onPostExecute displays the results of the AsyncTask.
private String getResponseTwo()
{
String result;
String testName = fileUri.getPathSegments().get(fileUri.getPathSegments().size()-1);
//setup params
Map<String, String> params = new HashMap<>();
params.put("api_key", "******");
params.put("images_file", testName);
params.put("parameters", "{\"urls\": [], \"classifier_ids\": [\"baypay_1337105903\"], \"owners\": [\"0c9e0d07-7000-4b60-9181-b2e1d0596a30\", \"me\"], \"threshold\": 0.4}");
params.put("Accept-Language", "en");
params.put("version", "2016-05-19");
ServiceHandler serviceHandler = new ServiceHandler(this);
try
{
return serviceHandler.multipartRequest("https://watson-api-explorer.mybluemix.net/visual-recognition/api/v3/classify", params, fileUri.getPath(), "image", "image/jpg");
} catch (Exception e) {
e.printStackTrace();
}
return "Response Not returned";
}
}
这是 ServiceHandler.java 文件:
package com.innam.httpapptotestrestservice;
import android.content.Context;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
public class ServiceHandler {
private static final String USER_AGENT = "Mozilla/5.0";
String filename = "myJsonResponse.txt";
String outputString = "Hello world!";
File myDir;
static final int READ_BLOCK_SIZE = 100;
Context context;
public ServiceHandler(Context context) {
this.context = context;
myDir = context.getFilesDir();
}
public String doGet(String GET_URL) throws IOException {
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setConnectTimeout(150000);
int responseCode = con.getResponseCode();
System.out.println("GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
// success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
//System.out.println(response.toString());
return response.toString();
} else {
System.out.println("GET request not worked: Response Code: " + responseCode);
return "GET request not worked: Response Code: " + responseCode;
}
}
public String doPost(String POST_URL, String POST_PARAMS) throws IOException {
URL obj = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
// For POST only - START
con.setDoOutput(true);
con.setConnectTimeout(150000);
OutputStream os = con.getOutputStream();
os.write(POST_PARAMS.getBytes());
os.flush();
os.close();
// For POST only - END
int responseCode = con.getResponseCode();
System.out.println("POST Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
//success
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// print result
//System.out.println(response.toString());
/*try
{
File respFile = new File(myDir + "/text/", filename);
if (respFile.getParentFile().mkdirs())
{
outputString = response.toString();
respFile.createNewFile();
FileOutputStream fos = new FileOutputStream(respFile);
fos.write(outputString.getBytes());
System.out.println(myDir + " /" + filename);
fos.flush();
fos.close();
}
}
catch (Exception e)
{
e.printStackTrace();
} */
return response.toString();
} else {
System.out.println("POST request not worked: Response Code: " + responseCode);
return "POST request not worked: Response Code: " + responseCode;
}
}
public String multipartRequest(String urlTo, Map<String, String> parmas, String filepath,
String filefield, String fileMimeType) throws Exception {
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
InputStream inputStream = null;
String twoHyphens = "--";
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
String lineEnd = "\r\n";
String result = "";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String[] q = filepath.split("/");
int idx = q.length - 1;
try {
File file = new File(filepath);
FileInputStream fileInputStream = new FileInputStream(file);
URL url = new URL(urlTo);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\"" + q[idx] + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: " + fileMimeType + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
outputStream.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
outputStream.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
outputStream.writeBytes(lineEnd);
// Upload POST Data
Iterator<String> keys = parmas.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
String value = parmas.get(key);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd);
outputStream.writeBytes("Content-Type: application/json" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(value);
outputStream.writeBytes(lineEnd);
}
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
int respCode = connection.getResponseCode();
System.out.println("Response Code: " + respCode);
inputStream = connection.getInputStream();
result = this.convertStreamToString(inputStream);
fileInputStream.close();
inputStream.close();
outputStream.flush();
outputStream.close();
return result;
} catch (Exception e) {
//logger.error(e);
//throw new CustomException(e);
e.printStackTrace();
}
return result;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
这是 CustomException.java 文件:
package com.innam.httpapptotestrestservice;
import android.util.Log;
/**
* Created by Innam on 8/5/2016.
*/
public class CustomException extends Throwable {
public CustomException(String s) {
Log.e("CustomException", s);
}
}
这是 AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.innam.httpapptotestrestservice">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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>
这是 activity_main.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvIsConnected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#FF0000"
android:textColor="#FFF"
android:textSize="18dp"
android:layout_marginBottom="5dp"
android:text="is connected? " />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="JSON reult after the service has been trained"
android:textSize="18sp"
android:textColor="#000000"
android:layout_marginLeft="5dp" />
<TextView
android:id="@+id/etResponse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp">
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Camera"
android:id="@+id/button1"
android:onClick="openCamera"
android:layout_marginLeft="20dp"
/>
<ImageView
android:id="@+id/imgPreview"
android:layout_width="200dp"
android:layout_height="165dp"
android:src="@mipmap/ic_launcher"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPLOAD"
android:id="@+id/button2"
android:onClick="uploadImage"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="JSON reult after image has been uploaded"
android:textSize="18sp"
android:textColor="#000000"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"/>
<TextView
android:id="@+id/ImgResponse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="5dp">
</TextView>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>