Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何以字符串形式显示从(服务器)接收的图像?代码如下:_Java_Android_Android Asynctask - Fatal编程技术网

Java 如何以字符串形式显示从(服务器)接收的图像?代码如下:

Java 如何以字符串形式显示从(服务器)接收的图像?代码如下:,java,android,android-asynctask,Java,Android,Android Asynctask,如何以字符串形式显示从(服务器)接收的图像? 我正在使用AsyncTask 代码如下: 异步任务 public class sendRequest extends AsyncTask<String, Void,String> { String response=""; Context context; sendRequest(Context context) { this.context=context; // th

如何以字符串形式显示从(服务器)接收的图像? 我正在使用
AsyncTask

代码如下:

异步任务

public class sendRequest extends AsyncTask<String, Void,String> {

    String response="";
    Context context;


    sendRequest(Context context)
    {
        this.context=context;
       // this.list=list;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();


    }

    @Override
    protected String doInBackground(String... params)
    {



    URL url;
    BufferedReader reader = null;


    String link =<i> "example.com/example.php";</i>


    try {
        url = new URL(link);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setConnectTimeout(5 * 1000);
        connection.setReadTimeout(5 * 1000);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");

        connection.connect();

        String inputToFile = URLEncoder.encode("mName", "UTF-8") + "=" + URLEncoder.encode("name", "UTF-8");



        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(inputToFile);
        writer.flush();

        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String line = null;

        while ((line = reader.readLine()) != null) {

//Here i have add data to string how to display images 

//The result of Toast in onpostexecute is

//{"images":[{"image":"http:\/\/example.com\/example.jpg"},{"image":"http:\/\/example.com\/example.jpg"}]}

            response += line;


        }

        //Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        //Toast.makeText(context,bmp.toString(),Toast.LENGTH_SHORT).show();


    } catch (MalformedURLException e) {
        //   DonorList.setText("Url Error");
        e.printStackTrace();
        response = "Exception: " + e.getMessage();
    } catch (IOException e) {
        //   DonorList.setText("Connection could not be opened");
        e.printStackTrace();
        response += "Exception: " + e.getMessage();
    }

        return response;

    }// DO IN BACKGROUND



    @Override
    protected void onPostExecute(String response) {

        super.onPostExecute(response);

        Toast.makeText(context, "Recieved: "+response, Toast.LENGTH_LONG).show();
公共类sendRequest扩展了异步任务{
字符串响应=”;
语境;
sendRequest(上下文)
{
this.context=context;
//this.list=list;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的字符串doInBackground(字符串…参数)
{
网址;
BufferedReader reader=null;
String link=“example.com/example.php”;
试一试{
url=新url(链接);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
连接。设置连接超时(5*1000);
连接设置读取超时(5*1000);
connection.setDoOutput(真);
connection.setDoInput(true);
connection.setRequestMethod(“POST”);
connection.connect();
字符串inputOfile=URLEncoder.encode(“mName”、“UTF-8”)+“=”+URLEncoder.encode(“name”、“UTF-8”);
OutputStreamWriter writer=新的OutputStreamWriter(connection.getOutputStream());
writer.write(输入文件);
writer.flush();
reader=new BufferedReader(new InputStreamReader(connection.getInputStream());
字符串行=null;
而((line=reader.readLine())!=null){
//在这里,我将数据添加到如何显示图像的字符串中
//onpostexecute中Toast的结果是
//{“图像”:[{“图像”:“http:\/\/example.com\/example.jpg”},{“图像”:“http:\/\/example.com\/example.jpg”}]}
响应+=行;
}
//位图bmp=BitmapFactory.decodeStream(url.openConnection().getInputStream());
//Toast.makeText(上下文,bmp.toString(),Toast.LENGTH_SHORT.show();
}捕获(格式错误){
//DonorList.setText(“Url错误”);
e、 printStackTrace();
response=“Exception:”+e.getMessage();
}捕获(IOE异常){
//DonorList.setText(“无法打开连接”);
e、 printStackTrace();
响应+=“异常:”+e.getMessage();
}
返回响应;
}//在后台做什么
@凌驾
受保护的void onPostExecute(字符串响应){
super.onPostExecute(响应);
Toast.makeText(上下文,“received:+响应,Toast.LENGTH_LONG).show();

使用Glide库在ImageView中加载图像

Glide.with(context).load(image_url).into(imageView);

图像是二进制的-您希望如何显示为文本?请简要告诉我们您的问题。只有代码没有帮助?您希望实现什么?我希望在图像视图中显示从服务器接收到的图像。您的代码显示您有一个json数据作为响应,并且您希望从url中取出位图?是这样吗?如果是,则在哪里问题出在哪里?