2012-12-17 110 views
0

在我的應用程序,用戶可以將圖片上傳到PHP服務器,iOS版本的工作100%,使用的Android版本本教程上傳圖片: tutorial example從Web服務響應的HttpResponse對象

而我使用的功能是這樣的:

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服務的回報是圖像保存的網址,我需要有一個字符串在PHP服務器的回報,而不是我上面提到的回報,我怎麼能擁有它?

我想要這樣的東西(HttpURLConnection): HttpURLConnection conn; ...

String response= ""; 
Scanner inStream = new Scanner(conn.getInputStream()); 

while(inStream.hasNextLine()) 
    response+=(inStream.nextLine()); 
Log.e("resp", response); 

一個小時後onsegui試圖從Web服務的響應如下: ...

byte [] responseBody = httppost.getMethod().getBytes(); 
Log.e("RESPONSE BODY",""+(new String(responseBody))); 

...

+0

看到的Javadoc:http://developer.android.com/reference/org/apache/http/message/BasicHttpResponse.html – 2012-12-17 03:51:30

+0

我看着但是沒有找到文件,我可以得到我的web服務的返回,返回類似於<?php echo $ urlImage?>,編輯後,尋找一個可用的例子 – jucajl

回答

0

如果你想要的內容由HTTP服務器返回,你不應該這樣做:

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

。 ..因爲那說「扔掉內容」。

+0

它沒有返回響應WebService的情況下它是上傳時圖像移動到的路徑(URL),情況如下解決:byte [] responseBody = httppost.getMethod()。getBytes(); (「RESPONSE BODY」,「」+(new String(responseBody))); – jucajl

0

試試這個,如果反應是String類型的

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;