0
私はアンドロイドでWebサービスを呼び出すとイメージを取得しますが、イメージはストリームオブジェクトであるため、グライドはそれを処理しません。Webサービスから受信しているイメージを解析します。そのストリームオブジェクト。どのように私はアンドロイドでそれを取得できますか?
私のウェブサービスコードです。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace ImageTransferService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public Stream GetImage(int i)
{
if (i == 5)
{
FileStream imageFilePath = File.OpenRead(@"E:\Images\h1.jpg");
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
return imageFilePath;
}
else
return null;
}
}
}
サービス
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace ImageTransferService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
Stream GetImage(int i);
}
}
マイアンドロイドコード:
package com.example.unais.myapplication;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import org.json.JSONStringer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
/**
* Created by Unais on 7/26/2016.
*/
public class WebService {
private final static String URL = "http://192.168.1.21:1313/Service1.svc/";
private static final String REGISTER_METHOD = "GetImage";
public InputStream CallService(String methodName, JSONStringer json) {
try {
HttpPost request = new HttpPost(URL + methodName);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
StringEntity entity = new StringEntity(json.toString());
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity responseEntity = response.getEntity();
InputStream stream = responseEntity.getContent();
return stream;
// // Read response data into buffer
// BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "iso-8859-1"), 8);
//
// StringBuilder sb = new StringBuilder();
// String line = null;
// while ((line = reader.readLine()) != null) {
// sb.append(line + "\n");
// }
// stream.close();
// Log.d("asgasgasgasgaga", sb.toString());
// return sb.toString();
} catch (UnsupportedEncodingException e) {
Log.d("Exception", "Exception");
e.printStackTrace();
} catch (IOException e) {
Log.d("Exception", "Exception");
e.printStackTrace();
}
return null;
}
public InputStream GetImages()
{
Log.d("Function","In Function Get Images");
try
{
JSONStringer registerJson = new JSONStringer()
.object()
.key("i").value(5)
.endObject();
return (CallService(REGISTER_METHOD, registerJson));
} catch (Exception ex)
{
ex.printStackTrace();
}
return null;
}
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position)
{
Image image = images.get(position);
Glide.with(mContext).load(image.getIota())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.thumbnail);
}
あなたが他のコードをしたい場合、私はprovide.Iこの上stucked午前ことができますので、私を助けてください:/