Java 安卓工作室:Can';无法从另一个类获取返回字符串

Java 安卓工作室:Can';无法从另一个类获取返回字符串,java,android,Java,Android,我知道这是一个愚蠢的问题,但我已经很久没有在Android Studio中编程Java了。所以实际上我有一个问题,我不能从一个类返回到另一个类。我尝试了这里提出的不同的解决方案,但没有一个有效。不知道为什么。我没有在xml文本视图中显示任何内容 以下是MainActivity.java: public class MainActivity extends ActionBarActivity { TextView txt_temp; @Override protected void onCreat

我知道这是一个愚蠢的问题,但我已经很久没有在Android Studio中编程Java了。所以实际上我有一个问题,我不能从一个类返回到另一个类。我尝试了这里提出的不同的解决方案,但没有一个有效。不知道为什么。我没有在xml文本视图中显示任何内容

以下是MainActivity.java:

public class MainActivity extends ActionBarActivity {
TextView txt_temp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    XMLreference();
    StringRequest();
}

public void XMLreference(){
    txt_temp = (TextView) findViewById(R.id.txt_temp);
}

public void StringRequest(){
    WebServiceRequest webService = new WebServiceRequest();
    String request = webService.main();

    txt_temp.setText(request);
}
}

下面是WebServiceRequest.java:

public class WebServiceRequest {
    private static final String username = "*********"; // put your Device Cloud username here
    private static final String password = "*********"; // put your Device Cloud password here
    public String responseContent;

    /**
     * Run the web service request
     */
    public String main() {
        HttpsURLConnection conn = null;

        try {
            // Create url to the Device Cloud server for a given web service request
            URL url = new URL("https://devicecloud.digi.com/ws/DataStream/00000000-00000000-********-********/xbee.serialIn");
            conn = (HttpsURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setRequestMethod("GET");

            // Build authentication string
            String userpassword = username + ":" + password;

            // can change this to use a different base64 encoder
            String encodedAuthorization = Base64.encodeToString(userpassword.getBytes(), Base64.DEFAULT).trim();

            // set request headers
            conn.setRequestProperty("Authorization", "Basic "
                    + encodedAuthorization);

            InputStream is = conn.getInputStream();

            Scanner isScanner = new Scanner(is);
            StringBuffer buf = new StringBuffer();
            while (isScanner.hasNextLine()) {
                buf.append(isScanner.nextLine() + "\n");
            }
            responseContent = buf.toString();

            // add line returns between tags to make it a bit more readable
            responseContent = responseContent.replaceAll("><", ">\n<");

            // Output response to standard out
            // System.out.println(responseContent);

        } catch (Exception e) {
            // Print any exceptions that occur
            e.printStackTrace();
        } finally {
            if (conn != null)
                conn.disconnect();
        }
        return responseContent;
    }
}
和它一起

WebServiceRequest webService = new WebServiceRequest();
String request = webService.getResponsteContent();

但它也不起作用。我不确定,但我在某个地方犯了一个愚蠢的错误,经过几个小时的编程,我找不到解决它的线索。

好的,我不太确定你是否想要一个构造函数,但是
main()
从未被调用,所以
responseContent
总是空的

为了匹配你的代码被剪断

WebServiceRequest webService = new WebServiceRequest();
String request = webService.getResponsteContent();
WebServiceRequest
中的
main
方法更改为如下构造函数

public WebServiceRequest() { .. } // it was String main() before

只要您实例化一个
WebServiceRequest

Web请求必须从第二个线程完成,它就会被调用

Runnable run = new Runnable() {
        @Override
        public void run() {
            try {
                //your main method here
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                            //update your textView here
                    }

                });

            } catch (Exception e) {

            }
        }
    };
    Thread thread = new Thread(run);
    thread.start();
此外,您需要移动响应代码并更新到web请求方法中的textView。对用户界面的更改必须发布到处理程序

另一个选项是使类扩展IntentService。看看这里,如果你想看看的话


main()
重命名为
getResponsteContent()
@MuratK。对不起,但也没用。你是说像这样吗?public void StringRequest(){webservicequest webService=new webservicequest();String request=webService.getResponseContent();txt_temp.setText(request);}您确定
responseContent
不是空的,即web服务调用成功了吗?我感觉,问题在于try/catch函数。即使我创建一个字符串“Hello World”并尝试返回它,我也会得到一个空行。代码应该可以工作,这是从digi设备导出的。我唯一改变的是Baser64编码。之前是
import org.apache.commons.codec.binary.Base64
字符串encodedAuthorization=Base64.encodeBase64String(userpassword.getBytes()).trim()。但是安卓工作室没有认出它,所以我改变了台词。我用Python尝试了导出,但它没有出现任何问题。或者Base64编码可能造成了故障。
Runnable run = new Runnable() {
        @Override
        public void run() {
            try {
                //your main method here
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                            //update your textView here
                    }

                });

            } catch (Exception e) {

            }
        }
    };
    Thread thread = new Thread(run);
    thread.start();