[英]Android Studio: Can't get return string from another class
I know it's kind of stupid question, but it's been a long time since I programmed Java in Android Studio. 我知道这是一个愚蠢的问题,但是自从我在Android Studio中编写Java以来已经有很长时间了。 So actually I have the problem that I can't get a return from a class to another.
所以实际上我有一个问题,我无法从一个班级返回另一个班级。 I tried different kind of solutions suggested here, but none of them worked.
我尝试了此处建议的其他解决方案,但没有一个起作用。 Not sure why.
不知道为什么。 I'm getting nothing displayed TextView in xml.
我什么也没显示XML中的TextView。
Here is the MainActivity.java: 这是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);
}
} }
And here is the WebServiceRequest.java: 这是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;
}
}
I already tried to create 我已经尝试创建
public void getResponseContent() {
return responseContent;
}
And getting it with 并获得它
WebServiceRequest webService = new WebServiceRequest();
String request = webService.getResponsteContent();
But it didn't work either. 但这也不起作用。 I'm not sure but I'm making somewhere a stupid mistake and after hours of programming I can't find the clue to solve it.
我不确定,但是我在某个地方犯了一个愚蠢的错误,经过数小时的编程,我找不到解决该问题的线索。
Okay I am not so sure if you want to have a constructor, but the main()
never gets called so responseContent
is always empty. 好的,我不确定是否要构造函数,但是
main()
从未调用,因此responseContent
始终为空。
To match your code snipped 为了匹配您的代码被截断
WebServiceRequest webService = new WebServiceRequest();
String request = webService.getResponsteContent();
Change your method main
in WebServiceRequest
to a constructor like this 将您的
WebServiceRequest
中的方法main
更改为这样的构造函数
public WebServiceRequest() { .. } // it was String main() before
It will get called as soon as you instantiate a WebServiceRequest
实例化
WebServiceRequest
将立即调用它
Web requests must be done from a second thread. 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();
Also, you need to move the response code and update to the textView inside your web request method. 另外,您需要移动响应代码并更新到Web请求方法中的textView。 Changes to the user interface must be posted to a handler.
用户界面的更改必须发布到处理程序。
Another option is to make your class extend IntentService. 另一种选择是使您的类扩展IntentService。 See here if you want to check that out.
如果要查看,请参见此处。
https://developer.android.com/training/run-background-service/create-service.html https://developer.android.com/training/run-background-service/create-service.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.