Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android StringBody(字符串)已弃用_Java_Android_Apache_Http_Post - Fatal编程技术网

Java Android StringBody(字符串)已弃用

Java Android StringBody(字符串)已弃用,java,android,apache,http,post,Java,Android,Apache,Http,Post,我正在尝试向我的API发送图像。但当使用MultipartEntity StringBody时,我得到了一个错误,因为StringBody(String)已被弃用 我不工作。我想买一个样品 代码如下: try { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("api_key", api_key)); p

我正在尝试向我的API发送图像。但当使用MultipartEntity StringBody时,我得到了一个错误,因为StringBody(String)已被弃用

我不工作。我想买一个样品

代码如下:

try {

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("api_key", api_key));
    params.add(new BasicNameValuePair("access_token", access_token));

    API api = new API(mApiKey, mApiSecret);        


    HttpClient client = new DefaultHttpClient();
    HttpPost postMethod = new HttpPost("MY API URL");
    File file = new File(imagePath);
    MultipartEntity entity = new MultipartEntity();
    FileBody contentFile = new FileBody(file);

    StringBody api_key       = new StringBody(mApiKey);
    StringBody access_token  = new StringBody(access_token);

    entity.addPart("api_key", api_key);
    entity.addPart("hash", access_token);
    entity.addPart("image", contentFile);

    postMethod.setEntity(entity);
    client.execute(postMethod);



} catch (Exception e) {
    e.printStackTrace();
}
试试看{
List params=new ArrayList();
添加(新的BasicNameValuePair(“api_键”,api_键));
添加(新的BasicNameValuePair(“访问令牌”,访问令牌));
API API=新API(mApiKey,MAPISECURE);
HttpClient=new DefaultHttpClient();
HttpPost postMethod=新的HttpPost(“我的API URL”);
文件文件=新文件(imagePath);
多方实体=新多方实体();
FileBody contentFile=新的FileBody(文件);
StringBody api_key=新的StringBody(mApiKey);
StringBody访问\ U令牌=新的StringBody(访问\ U令牌);
实体.addPart(“api_键”,api_键);
entity.addPart(“散列”,访问令牌);
entity.addPart(“图像”,contentFile);
方法后。集合实体(实体);
客户。执行(方法后);
}捕获(例外e){
e、 printStackTrace();
}

您用来创建
StringBody
新实例的构造函数已被弃用

而不是

new StringBody(mApiKey);
您应该将
StringBody
构造函数与另一个类似
ContentType的参数一起使用

new StringBody(mApiKey, ContentType.TEXT_PLAIN);
有关更多信息,请参阅:


字符串正文:

 entity.addPart("api_key", new StringBody(mApiKey,ContentType.TEXT_PLAIN));
多方当事人:


检查:

此外,MultipartEntity类型已被弃用。是的,可以通过Google轻松找到文档: