[英]Get response from the web service for HttpResponse
在我的應用程序中,用戶可以將圖像上載到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服務獲取響應,如下所示:...
byte [] responseBody = httppost.getMethod().getBytes();
Log.e("RESPONSE BODY",""+(new String(responseBody)));
...
如果要HTTP服務器返回的內容,則不應執行以下操作:
if (resEntity != null) {
resEntity.consumeContent();
}
...因為它說“扔掉內容”。
如果響應的類型為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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.