私のアプリケーションでは、ユーザーは画像を PHP サーバーにアップロードできます。iOS バージョンは 100% 動作しており、このチュートリアルで画像をアップロードするために Android バージョンを使用しています: チュートリアルの例
そして、私が使用している機能はこれです:
public static String sendPost(String url, String imagePath)
throws IOException, ClientProtocolException {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(url);
File file = new File(imagePath);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("userfile", cbFile);
httppost.setEntity(mpEntity);
//Log.e("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
//Log.e(""+response.getStatusLine());
if (resEntity != null) {
//Log.e(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
return response.toString();
}
response.toString(); を返します。org.apache.http.message.BasicHttpResponse @ 406dc148 で取得します
しかし、Web サービスの戻り値は、画像が保存された URL です。PHP サーバーの戻り値には、上記の戻り値ではなく、文字列が必要です。どうすれば取得できますか?
私はこのようなものが欲しかった (HttpURLConnection): HttpURLConnection conn; ...
String response= "";
Scanner inStream = new Scanner(conn.getInputStream());
while(inStream.hasNextLine())
response+=(inStream.nextLine());
Log.e("resp", response);
onsegui が次のように Web サービスからの応答を取得しようとして 1 時間後: ...
byte [] responseBody = httppost.getMethod().getBytes();
Log.e("RESPONSE BODY",""+(new String(responseBody)));
...