在我的應用程序,用戶可以將圖片上傳到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)));
...
看到的Javadoc:http://developer.android.com/reference/org/apache/http/message/BasicHttpResponse.html – 2012-12-17 03:51:30
我看着但是沒有找到文件,我可以得到我的web服務的返回,返回類似於<?php echo $ urlImage?>,編輯後,尋找一個可用的例子 – jucajl