public static String[] makeRequest(String uri, String base64encodedImage) throws IOException {
final JSONObject[] json = {new JSONObject()};
String data ="";
final String[] result = new String[1];
//URL _url = new URL(uri);
final AsyncHttpClient[] httpClient = {new AsyncHttpClient()};
httpClient[0].addHeader("Content-type", "application/json");
httpClient[0].setUserAgent("GYUserAgentAndroid");
String jsonParamsString = "{\"key\":\"value\"}";
RequestParams requestParams = new RequestParams("request", jsonParamsString);
httpClient[0].put(uri, requestParams, new JsonHttpResponseHandler()
{
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
result[0] = response.toString();
//Toast toast = Toast.makeText(this.getBaseContext(), response.toString(), Toast.LENGTH_LONG);
//toast.show();
}
@Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
result[0] = "failed";
}
}
);
try {
json[0].put( "imageData", base64encodedImage);
} catch (JSONException e) {
e.printStackTrace();
}
//json.put( "caption", );
return result;
我正在尝试使用Android设备从名为Calorie Mama(https://dev.caloriemama.ai/docs)的API获取JSON数据。我必须将图像发送到API,我已经将图像转换为基本的64编码字符串。但是,我没有从API获取任何数据。这是应该接收数据的类,但它不起作用。如果你能告诉我为什么会很棒。我是android开发的新手。 :(
下面的活动实例化了makeRequest类。
package com.example.takeimage;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
public class Evaluate extends AppCompatActivity {
String base64encodedImage;
String baseURI = "https://api-2445582032290.production.gw.apicast.io/v1/foodrecognition?user_key=[my Api Key]";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_evaluate);
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
Bitmap bm = BitmapFactory.decodeFile(message);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
base64encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
makeRequest newRequest = new makeRequest();
String[] result= null;
try {
result = newRequest.makeRequest(baseURI, base64encodedImage);
} catch (IOException e) {
e.printStackTrace();
}
Toast toast = Toast.makeText(this.getApplicationContext(), result[0], Toast.LENGTH_LONG);
toast.show();
//TextView txtView = (TextView) findViewById(R.id.textEvaluate);
//txtView.setText(base64encodedImage);
}
protected void response(){
final String url = baseURI + "/postformdata";
}
}