简体   繁体   English

从Web服务获取对HttpResponse的响应

[英]Get response from the web service for HttpResponse

In my application, the User can upload images to a PHP server, the iOS version is working 100%, the Android version used for this tutorial to upload image: tutorial example 在我的应用程序中,用户可以将图像上载到PHP服务器,iOS版本可正常工作100%,本教程所使用的Android版本用于上载图像: 教程示例

And the function I'm using is this: 我正在使用的功能是这样的:

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();

}

return response.toString(); 返回response.toString(); get it in org.apache.http.message.BasicHttpResponse @ 406dc148 在org.apache.http.message.BasicHttpResponse @ 406dc148中获取它

But the return of the web service is the URL where the image was saved, I need to have a string in the return of the PHP server, rather than the return I mentioned above how can I have it? 但是Web服务的返回是保存图像的URL,我需要在PHP服务器的返回中包含一个字符串,而不是上面提到的返回如何拥有它?

I wanted something like this (HttpURLConnection): HttpURLConnection conn; 我想要这样的东西(HttpURLConnection):HttpURLConnection conn; ... ...

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

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

After one hour onsegui trying to get the response from the Web service as follows: ... 一小时后,onsegui试图从Web服务获取响应,如下所示:...

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

... ...

If you want the content returned by the HTTP server, you shouldn't do this: 如果要HTTP服务器返回的内容,则不应执行以下操作:

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

... because that says "throw away the content". ...因为它说“扔掉内容”。

Try this if the response is of type String 如果响应的类型为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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM