【问题标题】:need help to pass string value from try catch需要帮助从 try catch 传递字符串值
【发布时间】:2014-10-05 17:53:21
【问题描述】:

我正在尝试将所有文​​本从 url 文本文件读取到字符串,但出现以下错误:

Description Resource    Path    Location    Type
currentUrl cannot be resolved to a variable MainActivity.java   /Copy of ImageDownloadSample/src/com/example/imagedownloadsample    line 51 Java Problem
currentUrl cannot be resolved   MainActivity.java   /Copy of ImageDownloadSample/src/com/example/imagedownloadsample    line 62 Java Problem
currentUrl cannot be resolved   MainActivity.java   /Copy of ImageDownloadSample/src/com/example/imagedownloadsample    line 64 Java Problem
currentUrl cannot be resolved   MainActivity.java   /Copy of ImageDownloadSample/src/com/example/imagedownloadsample    line 63 Java Problem

这是我的代码:

package com.example.imagedownloadsample;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_download);

        //
        try {

            URL url = new URL(
                    "https://www.dropbox.com/s/s70xckuxjh5sbtw/pop.txt");

            // read text returned by server
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    url.openStream()));

            String currentUrl;
            while ((currentUrl = in.readLine()) != null) {
                System.out.println(currentUrl);
            }
            in.close();

        } catch (MalformedURLException e) {
            System.out.println("Malformed URL: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("I/O Error: " + e.getMessage());
        }
        //

        Picasso.with(this).load(currentUrl).into(target);

    }

    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    String fileName = currentUrl.substring(
                            currentUrl.lastIndexOf('/') + 1,
                            currentUrl.length());

                    String fname = "/image-" + fileName + ".jpg";

                    File file = new File(Environment
                            .getExternalStorageDirectory().getPath() + fname);
                    if (file.exists())
                        file.delete();
                    try {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(CompressFormat.JPEG, 100, ostream);
                        ostream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            if (placeHolderDrawable != null) {
            }
        }
    };

}

.................................................. ..................................................... ..................................................... .....

【问题讨论】:

  • 也许开始查看第 51、62、63、64 行?

标签: android string inputstream android-internet


【解决方案1】:

据我所知,currentUrl 实际上并没有在 Target 中定义。

 String fileName = currentUrl.substring(
                            currentUrl.lastIndexOf('/') + 1,
                            currentUrl.length());

currentUrl 在哪里定义?它似乎在 onCreate 中定义,但在其他地方没有。

【讨论】:

    【解决方案2】:

    这是一个简单的 Java 范围问题。您在onCreate() 中的try 内声明了变量,因此在try 块内是唯一可以访问它的地方。

    如果您想在其他领域使用它,则需要将其声明为成员变量。

    //declare it here
    String currentUrl = "";
    
    public class MainActivity extends ActionBarActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_download);
    
        //
        try {
    
            URL url = new URL(
                    "https://www.dropbox.com/s/s70xckuxjh5sbtw/pop.txt");
    
            // read text returned by server
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    url.openStream()));
    

    Java docs

    【讨论】:

    • 你是对的,但是我如何将值从 try 块发送到成员变量。我想要的是我想将从 url 文本文件中获取的值发送到成员变量
    • 你可以使用StringBuilder
    猜你喜欢
    • 2015-04-07
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多