0

私のアプリケーションでは、ユーザーは画像を 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)));

...

4

2 に答える 2

0

HTTPサーバーからコンテンツを返す場合は、次のことを行わないでください。

if (resEntity != null) {
    resEntity.consumeContent();
}

...それは「コンテンツを捨てる」と言っているからです。

于 2012-12-17T04:36:26.153 に答える
0

応答が文字列型の場合はこれを試してください

  ResponseHandler<String> responseHandler = new BasicResponseHandler();
  HttpResponse httpResponse = httpClient.execute(post, new BasicHttpContext()); // new BasicHttpContext() not necessary 
// verify connection response status using httpResponse.getStatusLine().getStatusCode() then

  String response =  responseHandler.handleResponse(httpResponse);
  if(response != mull){
     Log.e("Response : "+response);
  }else{
   // Handle exception 
  }
 return response;
于 2012-12-17T06:30:05.533 に答える