HttpPost
为了安全起见,我改变了我所拥有的。
我可以让应用程序加载/拍照,但它不能与 API 对话,因为 API 使用了不同的检索方法。
我确实有一个多部分编码的示例,但不确定如何使用它来实现。可以在此处找到我正在做的/需要做的事情的示例。
我将如何使用多部分表单数据来实现?
protected Object doInBackground(Object... arg0) {
Bitmap bitmapOrg = ((Main) parentActivity).mPhoto;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// compress bitmap into the byte array output stream
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 75, baos);
// form byte array out of byte array output stream
byte[] ba = baos.toByteArray();
String encodedImage = Base64.encodeBytes(ba);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"-----------------------------------");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("image", encodedImage));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream inputstream = entity.getContent();
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputstream));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
results = sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}